gh-121291: Respect mixin bitwise-operator overrides on Flag subclasses - #155862
Open
SomSamantray wants to merge 5 commits into
Open
gh-121291: Respect mixin bitwise-operator overrides on Flag subclasses#155862SomSamantray wants to merge 5 commits into
SomSamantray wants to merge 5 commits into
Conversation
…lasses EnumMeta.__new__ unconditionally installed Flag's __or__/__and__/__xor__/ __ror__/__rand__/__rxor__/__invert__ onto every Flag subclass, silently discarding a mixin base's own override of these dunders -- even though the class's MRO should have resolved to the mixin's method. Give this loop the same MRO-aware check already used a few lines above for __repr__/__str__/__format__/__reduce_ex__: only install Flag's method when nothing else (neither a mixin nor a mixed-in data type, e.g. IntFlag's int) already provides a real override.
Avoids redeclaring an equivalent IntFlag enum inline when OldTestIntFlag already defines and reuses one.
The fixed loop and NEWS entry cover all seven Flag operator dunders; extend the regression test to exercise the reflected forms too, per code review (ce-code-review finding python#2).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
EnumType.__new__(Lib/enum.py) installsFlag's__or__/__and__/__xor__/__ror__/__rand__/__rxor__/__invert__onto everyFlagsubclass whenever the name isn't in the subclass's own class body — but that check never looked at what a mixin base actually resolved to, soclass MyFlag(Mixin, Flag)silently lost the mixin's own operator overrides toFlag's defaults. The__repr__/__str__/__format__/__reduce_ex__block a few lines above already handles this correctly (checking whether the class's real MRO-resolved method is still the type default before overwriting); this gives the Flag-operator loop the same MRO-aware check.The check also has to distinguish a mixin's own override from a mixed-in data type's raw operator (e.g.
int.__or__forIntFlag) — an earlier version of this fix compared only againstFlag's own method and broke everyIntFlagcombination (Color.RED | Color.BLUEsilently returned a plainintinstead of a boxedIntFlag), caught by running the fulltest_enumsuite including theDoc/howto/enum.rstdoctests.Lib/test/test_enum.pygets a regression test in bothOldTestFlagandOldTestIntFlagcovering: a mixin's override winning overFlag's default (forward and reflected forms), dunders the mixin didn't touch still gettingFlag's own, and theIntFlag/intregression case. Full suite: 1091 tests pass, 0 failures.One known scope boundary, called out during review: when the overriding mixin is itself the class selected as
member_type(e.g. a customintsubclass that also overrides__or__), the identity check still can't tell that apart from the raw data-type operator — the same limitation the pre-existing__repr__/__str__block already has. Fixing that would need a heavier MRO/__dict__-walk redesign touching both blocks; out of scope here.Fixes #121291.