Skip to content

Commit ccf05db

Browse files
authored
Fix list index while checking for Enum class. (#18426)
Fixes mypyc/mypyc#1080 Python requires that Enum must be the last class in the parent class list. This change fixes the index in `ClassDef.bases` list where we check for `Enum`.
1 parent 20355d5 commit ccf05db

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

mypyc/irbuild/classdef.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,8 @@ def add_non_ext_class_attr(
682682
# are final.
683683
if (
684684
cdef.info.bases
685-
and cdef.info.bases[0].type.is_enum
685+
# Enum class must be the last parent class.
686+
and cdef.info.bases[-1].type.is_enum
686687
# Skip these since Enum will remove it
687688
and lvalue.name not in EXCLUDED_ENUM_ATTRIBUTES
688689
):

mypyc/test-data/run-classes.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2692,3 +2692,16 @@ print(native.C(22).v)
26922692

26932693
[out]
26942694
22.1
2695+
2696+
[case testLastParentEnum]
2697+
from enum import Enum
2698+
2699+
class ColorCode(str, Enum):
2700+
OKGREEN = "okgreen"
2701+
2702+
[file driver.py]
2703+
import native
2704+
print(native.ColorCode.OKGREEN.value)
2705+
2706+
[out]
2707+
okgreen

0 commit comments

Comments
 (0)