-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
Unexpected behaviour of IntFlag with a custom __new__ in Python 3.11.0. #101541
Comments
Thank you for the thorough bug report! The issue is that in 3.11+, when a new (psuedo) member needs to be created, the original So the immediate issue is that your
which will work in 3.11, 3.10, etc.. |
Thank you for your answser, but as I mentioned in my report, this would only work in some specific cases and would not handle the second case where the transformation maps As I said, this may be (partially) avoided by checking whether A way to handle the new attributes in a future version of Python is to require some other special method to be overloaded instead of EDIT: For instance, in order to distinguish an enumeration member declaration (e.g. "a = ord('a')") with the pseudo-member creation, we could write "a = (ord('a'), None)" and ignore the |
First, my apologies for not noticing that you were already aware of the "workaround" (checking the type of the incoming value). Second, when I try the Thirdly (from your edit), if you are creating members directly from |
Thank you for your quick answer !
Do not worry about that !
I said "3.11.1" but it's "3.11.0", sorry about that. I recompiled using 3.11.1 sources but I still had the same issue. I was wondering whether it's an issue on my side but I cannot find an online interpreter where I change the interpreter version to exactly match a desired version.
Assume the enumeration members are declared with an ordinal value (in my example, it would be from enum import IntFlag
class FlagFromChar(IntFlag):
def __new__(cls, c):
value = 1 << c
self = int.__new__(cls, value)
self._value_ = value
return self
a = ord('a')
print(FlagFromChar.a | 1) With Python 3.10.3, it does not abort but with 3.11.0 / 3.11.1, it fails as follows:
As we can see, class FlagFromChar(IntFlag):
def __new__(cls, c, *args):
# args = (None,) iff we are dealing with a real member
value = (1 << c) if args else c
self = int.__new__(cls, value)
self._value_ = value
return self
a = (97, None) In 3.10.3, pseudo-members (for IntFlag) were created by creating singleton pseudo-members 1 and by decomposing the input value into known flags and extra ones, namely when writing >>> # Python 3.10.3
>>> FlagFromChar._value2member_map_
{158456325028528675187087900672: <Flag.a: 158456325028528675187087900672>}
>>> Flag.a | 1
<Flag.a|1: 158456325028528675187087900673>
>>> FlagFromChar._value2member_map_
{158456325028528675187087900672: <Flag.a: 158456325028528675187087900672>, 1: <Flag.1: 1>, 158456325028528675187087900673: <Flag.a|1: 158456325028528675187087900673>} In 3.11, the extra flags are not extracted since pseudo-members are directly created via This is somewhat entirely unrelated to this issue but when digging through the codebase and the documentation, I saw that the official documentation says the IntFlag class has the EJECT boundary but this is not the case (#93250). I don't know whether a doc-issue is already taking care of that. Footnotes |
Ah, thanks -- now I get it: |
…ginal __new__ (pythonGH-101590) (cherry picked from commit ef7c2bf) Co-authored-by: Ethan Furman <[email protected]>
…__new__ (GH-101590) (cherry picked from commit ef7c2bf) Co-authored-by: Ethan Furman <[email protected]>
Looks like the fix and backport both landed? Closing as fixed, feel free to reopen if there is further work needed here beyond the merged PR. |
Reopening this issue. The docs mentioned above had originally been written for For class Coordinate(bytes, Enum):
PX = (0, 'P.X', 'km')
PX.value == 0 For As an example of such behavior, here is an excerpt from a mixed class TermColor(str, Flag):
def __new__(cls, ...):
...
#
Bright = '1' # ESC [ 1 m # bright
# foreground - 30s
FG_Green = '32' # ESC [ 32 m # green
# background - 40s
BG_Black = '40' # ESC [ 30 m # black
TermColor.FG_Green | TermColor.BG_Black
# <TermColor.FG_Green|BG_Black>
str(TermColor.FG_Green | TermColor.BG_Black)
# '\x1b[32;40m' |
Forgot to mention: I'll get a deprecation warning in 3.12. |
Which part would get a DeprecationWarning? does it mean that |
I no longer remember what I was going to deprecate, so I won't. At some point in the future I hope to add capabilities to allow the |
Bug report
According to the enum documentation, it is possible to customize the enumeration value via a custom
__new__
method and the enumeration member (e.g., by adding an attribute) via a custom__init__
method. However, the implementation of the enum.Flag class in 3.11.0 (and probably in 3.11.1) introduces some issues compared to the one in 3.10.3, especially in the case of an enum.IntFlag:This also results in the impossibility of writing
Flag.a | i
fori != 0
(fori = 0
, it does work ! and this is confusing !), which IMHO is a regression compared to what was proposed in 3.10.3. It also clashes with the following assumption:Currently, the FlagBoundary for IntFlag is KEEP, so
Flag.a | 12
is expected to beFlag.a|8|4
as in 3.10.3.In order to avoid this issue, users need to write something like:
Neverthless, this is only possible if
__new__
converts an inputU
to an entirely different typeV
(enum member type) or ifargs
is non-empty when declaring enumeration members. However, this fails in the following example:Environment
Linked PRs
The text was updated successfully, but these errors were encountered: