Skip to content

Commit 2842e8f

Browse files
authored
[mypyc] Fixing check for enum classes. (#18178)
Fixes [mypyc/mypyc#1065](mypyc/mypyc#1065) Fixes [mypyc/mypyc#1059](mypyc/mypyc#1059) Fixes [mypyc/mypyc#1022](mypyc/mypyc#1022) * Checking for enum classes using `is_enum` flag instead of `fullname` of base class. That way, mypyc recognizes classes derived from `IntEnum` as enum classes too. * After fixing the above bug, test failures revealed that mypyc was sending all MRO classes to `__prepare__` function. For example, for the `Player` test class added in this PR, mypyc was generating C code to call `__prepare__` of the base class with `(IntEnum, int, Enum)` instead of just `(IntEnum,)`. This bug has been fixed in `classdef.py:populate_non_ext_bases`.
1 parent 15cd6d3 commit 2842e8f

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

mypyc/irbuild/classdef.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ def populate_non_ext_bases(builder: IRBuilder, cdef: ClassDef) -> Value:
513513
is_named_tuple = cdef.info.is_named_tuple
514514
ir = builder.mapper.type_to_ir[cdef.info]
515515
bases = []
516-
for cls in cdef.info.mro[1:]:
516+
for cls in (b.type for b in cdef.info.bases):
517517
if cls.fullname == "builtins.object":
518518
continue
519519
if is_named_tuple and cls.fullname in (
@@ -682,7 +682,7 @@ def add_non_ext_class_attr(
682682
# are final.
683683
if (
684684
cdef.info.bases
685-
and cdef.info.bases[0].type.fullname == "enum.Enum"
685+
and cdef.info.bases[0].type.is_enum
686686
# Skip these since Enum will remove it
687687
and lvalue.name not in EXCLUDED_ENUM_ATTRIBUTES
688688
):

mypyc/test-data/run-classes.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,3 +2619,15 @@ def test_final_attribute() -> None:
26192619
assert C.a['x'] == 'y'
26202620
assert C.b['x'] == 'y'
26212621
assert C.a is C.b
2622+
2623+
[case testClassDerivedFromIntEnum]
2624+
from enum import IntEnum, auto
2625+
2626+
class Player(IntEnum):
2627+
MIN = auto()
2628+
2629+
print(f'{Player.MIN = }')
2630+
[file driver.py]
2631+
from native import Player
2632+
[out]
2633+
Player.MIN = <Player.MIN: 1>

0 commit comments

Comments
 (0)