From ccf05db67f6f99878c73eb902fc59a6f037b18a6 Mon Sep 17 00:00:00 2001 From: Advait Dixit <48302999+advait-dixit@users.noreply.github.com> Date: Mon, 6 Jan 2025 22:37:00 -0800 Subject: [PATCH] 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`. --- mypyc/irbuild/classdef.py | 3 ++- mypyc/test-data/run-classes.test | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/mypyc/irbuild/classdef.py b/mypyc/irbuild/classdef.py index 6072efa2c593..84dd493c6d15 100644 --- a/mypyc/irbuild/classdef.py +++ b/mypyc/irbuild/classdef.py @@ -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 ): diff --git a/mypyc/test-data/run-classes.test b/mypyc/test-data/run-classes.test index db5459e22f5e..0eab15d89746 100644 --- a/mypyc/test-data/run-classes.test +++ b/mypyc/test-data/run-classes.test @@ -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