-
-
Notifications
You must be signed in to change notification settings - Fork 628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merge EnumSymbol and EnumValue into Enum field #2044
Conversation
i agree that i'm lukewarm on merging the two together and using a parameter to determine the internal type, as this makes it more difficult to add correct typings. wdyt? |
The only difference between these two use cases is how the members are accessed from the enum, which seems congruent with the foo = Enum()
bar = Enum(from_symbol=True) |
Thanks for the feedback.
Yes, exactly, the object is always the same (an
In object world it is always an
I understand that. OTOH, the parameter seems redundant with the field and accepting both means we'd need to manage all cases.
From my perspective (usage), default would be symbol, but YMMV. Symbol is generally a snake-case Python variable name which suits an API (some like them camel-case but oh well), while value may be anything. Typically, I'll have class Enum:
item_one = "The first item"
item_two = "The second item" Although both are strings, I serialize as symbol (code) rather than value (human readable string). I can add an Or we state explicitly that |
|
Yeah, I wondered if we should support (i.e. test)
|
As far as I can tell the current code handles exotic enums fine. I am suggesting that strict value type validation does not need to be the default, because enum membership validation works regardless of value type. The error message providing the member choices seems like a more useful default than an error message telling me the expected value type. Actually, even if the value field is specialized, it might be more useful show the member choices error message. |
I just pushed an updated version introducing Tests are incomplete (I didn't test the exception and the default value of field) but at least it gives us an idea of what it would look like. I'm not really fan of the signature, but I'm afraid it is either this or the two classes. def __init__(
self,
enum: type[EnumType],
by_value: bool = False,
field: Field | type | None = None,
**kwargs,
): # Call using kwargs
field = fields.Enum(HairColorEnum, by_value=True, field=fields.String)
# Call using args. I don't like True arguments in function calls. Not explicit enough.
# (Yet, I didn't prevent it in the signature by making it kwargs only.)
field = fields.Enum(HairColorEnum, True, fields.String)
# Field would be explicit enough to be passed as positional but we can't do this.
# And we don't want to put field first because one may pass only by_value and rely on default Field.
field = fields.Enum(HairColorEnum, by_value=True, fields.String) |
My main concern was that def __init__(
self,
enum: type[EnumType],
*,
by_value: bool | Field | type = False,
**kwargs,
): # Default: by_value=False, field=String
field = fields.Enum(HairColorEnum)
# Convenience: field=Field
field = fields.Enum(HairColorEnum, by_value=True)
# Custom: field=by_value
field = fields.Enum(HairColorEnum, by_value=fields.Int) |
This is technically better as it makes one less argument and avoids the forbidden case of by_symbol + field. I'm not a huge fan of arguments that can be multiple types (strings or list, boolean or field) but I still think it is better. Yet, I also understand the argument about the name conveying the meaning, and OK, I just tested and now I get your point. If I set Note that there may be slight differences. For instance, for enum integer values, OK, lemme try to do that. |
eb1b77d
to
8359b1f
Compare
Thanks for the help. I like current implementation. I modified the serialization of value and choices to always use the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overall looks good to me. just a few small things
eb69b5b
to
e619018
Compare
Thanks for the review. Good catches. Fixed! |
I'll merge, update CHANGELOG and release soonish (this week). |
e619018
to
db37b73
Compare
Good! I just fixed a typo and merged. Now releasing. |
On second thought, this fits more with our fields naming rationale. Generally, the field name represents the type in object world and the serialization type is defined at init (with an argument such as format, etc.).
I'd like to merge this before publishing 3.18. If this needs more thought and we want to ship a bugfix before, we can revert the merge of #2017.