Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Description
Bug report
Bug description:
I noticed some strange behavior withenum.Flag an 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 returnsFalse, which is expected, since3 is not a member ofWeekday. However, the second print statement returnsTrue, which is unexpected, since3 is still not a member ofWeekday.
importenumclassWeekday(enum.Flag):MONDAY=1TUESDAY=2WEDNESDAY=4THURSDAY=8FRIDAY=16SATURDAY=32SUNDAY=64print(f"{3inWeekday}")# => False (expected)_=Weekday.MONDAY|Weekday.TUESDAYprint(f"{3inWeekday}")# => True (not expected)
Problem 2: Behavior is not comparable to Python 3.11
Since Python 3.12 the behavior ofEnum.__contains__ has changed, so that it is possible to compare not only with an enum-member, but also with non-enum-members (seehere orhere). 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.
importenumclassWeekday(enum.Flag):MONDAY=1TUESDAY=2WEDNESDAY=4THURSDAY=8FRIDAY=16SATURDAY=32SUNDAY=64print(f"{Weekday(3)inWeekday}")# => Python 3.11: False (expected) Python 3.12/3.13: True (not expected)_=Weekday.MONDAY|Weekday.TUESDAYprint(f"{Weekday(3)inWeekday}")# => Python 3.11: False (expected) Python 3.12/3.13: True (not expected)
Conclusion
I would have expected that in all cases the result isFalse, but since Python 3.12 it gets very strange...
CPython versions tested on:
3.12, 3.13
Operating systems tested on:
Windows