-
-
Notifications
You must be signed in to change notification settings - Fork 34.6k
enum.Flag.__contains__ changed behavior since python 3.12 #131045
Copy link
Copy link
Closed
Labels
3.12only security fixesonly security fixes3.13bugs and security fixesbugs and security fixes3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Metadata
Metadata
Assignees
Labels
3.12only security fixesonly security fixes3.13bugs and security fixesbugs and security fixes3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
Status
Done
Bug report
Bug description:
I noticed some strange behavior with
enum.Flagan the__contains__method in Python 3.12/3.13, as shown in the following examples.Problem 1: Behavior changes at runtime
In the following code snippet the first print statement returns
False, which is expected, since3is not a member ofWeekday. However, the second print statement returnsTrue, which is unexpected, since3is still not a member ofWeekday.Problem 2: Behavior is not comparable to Python 3.11
Since Python 3.12 the behavior of
Enum.__contains__has changed, so that it is possible to compare not only with an enum-member, but also with non-enum-members (see here or here). So with Python 3.11 the code above will raise anTypeError: unsupported operand type(s) for 'in': 'int' and 'EnumType'. There you have to change the code to the following. But this in turn always produces unexpected behavior in Python 3.12/3.13.Conclusion
I would have expected that in all cases the result is
False, but since Python 3.12 it gets very strange...CPython versions tested on:
3.12, 3.13
Operating systems tested on:
Windows
Linked PRs