-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
Enum functional syntax allows creation of invalid member names #105906
Comments
If your code all uses So why bother? For example, you cannot use the normal syntax I think disallowing it will restrict too much on users. This is not usually what Python would do. I would be slightly against this change. |
You can also do this with regular classes, by assigning to |
Interesting. Makes sense to me if argued in that manner.
The behavior is not consistent. Whereas
Fair point. But this would not restrict the users. It would introduce a breaking API change for some. I think it comes down to the point of what the purpose of an Enum is. I see Enum as
Based on these parameters (which are somewhat my personal view of course), I would argue that using the suggested "workarounds" |
I've now read the referenced SC discussion. Their decision makes sense, but Enum is a strange beast and I don't mind being more restrictive (as Enum already is in other areas). Having said that, I need to think about whether I want to validate every member name, or allow an empty string as a name, as consistency in this case certainly seems reasonable. |
Just a quick note that this would be a breaking change, because some people might rely on it: Python 3.11.3 (main, May 27 2023, 12:23:35) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from enum import Enum, StrEnum
>>>
>>> InvalidEnum = StrEnum(
... "InvalidEnum", {"America/Chicago": "America/Chicago", "123": "123"}
... )
>>> I would also like to highlight that you can still use >>> InvalidEnum["America/Chicago"]
<InvalidEnum.America/Chicago: 'America/Chicago'> (in my opinion, there's no bug to fix here) |
I agree. Closing. |
Bug report
I discovered this behavior when we tried to dynamically create an Enum for all timezones available from
zoneinfo.available_timezones()
.According to the docs, the functional syntax work as such:
Using this knowledge to create
StrEnum
, this allows me to create the following Enum:This works without any errors. As we can see from the following code snippet it also works as expected:
But when trying to access the members of the Enum directly, the invalid member names become obvious:
I think the expected behavior here would be to raise a
ValueError
, similar to when trying to create an Enum with an empty string as member name:The reasoning here would be: The class syntax (obviously - due to syntax errors) prevents me from defining this enum. The functional syntax should behave in a similar manor.
ValueError
instead ofSyntaxError
, as it can only be evaluated during run time.Your environment
The text was updated successfully, but these errors were encountered: