Skip to content

Commit e4bf526

Browse files
authored
Fix inference of IntEnum value attribute type (Python 2) (#10417)
The enum stubs for Python 2 are slightly different from Python 3. The fix in #10412 didn't quite work in Python 2 mode, since `IntEnum` doesn't define `__new__`, and the `__new__` from `builtins.int` is found instead. Work around this by skipping any `__new__` methods found in built-in types. I gave up trying to write a test case for this. It's tricky because `enum` is in Python 3 stdlib but not in Python 2 stdlib.
1 parent b8c8833 commit e4bf526

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

mypy/plugins/enums.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ def _implements_new(info: TypeInfo) -> bool:
109109
subclass. In the latter case, we must infer Any as long as mypy can't infer
110110
the type of _value_ from assignments in __new__.
111111
"""
112-
type_with_new = _first(ti for ti in info.mro if ti.names.get('__new__'))
112+
type_with_new = _first(
113+
ti
114+
for ti in info.mro
115+
if ti.names.get('__new__') and not ti.fullname.startswith('builtins.')
116+
)
113117
if type_with_new is None:
114118
return False
115119
return type_with_new.fullname not in ('enum.Enum', 'enum.IntEnum', 'enum.StrEnum')

0 commit comments

Comments
 (0)