On 10Jun2019 1429, Ethan Furman wrote:
I saw my first annotation mix-up with regards to Enum today:
https://stackoverflow.com/q/56532591/208880
#enum import:
from enum import Enum
# enum definition:
class Status(Enum):
on: 1
off: 2
For those who don't follow links, the end result is that you get an
AttributeError the first time you do "Status.on" or "Status.off".
My question for the group:
Is this worth "fixing" or should I leave it alone? On the one hand it
seems like an obvious error, but on the other I hate getting in the way
of needs I haven't thought of.
I'd expect people coming from other languages to get it wrong this way too:
class Status(Enum):
on
off
Which will of course raise NameError and be just as opaque to the naive
user as the AttributeError later on. I'm not sure we can do as much to
help this case, but perhaps we can update __getattr__ to check
__annotations__ on the class before failing and provide a clearer
message? e.g. "AttributeError: 'on' was specified without assigning a
value, did you use ':' instead of '='?" Or we could do this on
construction, but that may rule out some interesting uses in the future
if you have a need to delay specifying enum values.
I'm not particularly a fan of how it currently is.
Cheers,
Steve
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/python-dev@python.org/message/VV77CSOYTO2BQKT5E2PSDDWYUAB4EPPX/