Skip to content

Commit

Permalink
Fix list index while checking for Enum class. (#18426)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
advait-dixit authored Jan 7, 2025
1 parent 20355d5 commit ccf05db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion mypyc/irbuild/classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,8 @@ def add_non_ext_class_attr(
# are final.
if (
cdef.info.bases
and cdef.info.bases[0].type.is_enum
# Enum class must be the last parent class.
and cdef.info.bases[-1].type.is_enum
# Skip these since Enum will remove it
and lvalue.name not in EXCLUDED_ENUM_ATTRIBUTES
):
Expand Down
13 changes: 13 additions & 0 deletions mypyc/test-data/run-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -2692,3 +2692,16 @@ print(native.C(22).v)

[out]
22.1

[case testLastParentEnum]
from enum import Enum

class ColorCode(str, Enum):
OKGREEN = "okgreen"

[file driver.py]
import native
print(native.ColorCode.OKGREEN.value)

[out]
okgreen

0 comments on commit ccf05db

Please sign in to comment.