Skip to content

Fix inference of IntEnum value attribute type #10412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/plugins/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def _implements_new(info: TypeInfo) -> bool:
type_with_new = _first(ti for ti in info.mro if ti.names.get('__new__'))
if type_with_new is None:
return False
return type_with_new.fullname != 'enum.Enum'
return type_with_new.fullname not in ('enum.Enum', 'enum.IntEnum')


def enum_value_callback(ctx: 'mypy.plugin.AttributeContext') -> Type:
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-enum.test
Original file line number Diff line number Diff line change
Expand Up @@ -1349,3 +1349,14 @@ b: Literal[E.A, E.B] = e # E: Incompatible types in assignment (expression has
c: Literal[E.A, E.C] = e # E: Incompatible types in assignment (expression has type "E", variable has type "Union[Literal[E.A], Literal[E.C]]")
b = a # E: Incompatible types in assignment (expression has type "Union[Literal[E.A], Literal[E.B], Literal[E.C]]", variable has type "Union[Literal[E.A], Literal[E.B]]")
[builtins fixtures/bool.pyi]

[case testIntEnumWithNewTypeValue]
from typing import NewType
from enum import IntEnum

N = NewType("N", int)

class E(IntEnum):
A = N(0)

reveal_type(E.A.value) # N: Revealed type is "__main__.N"
1 change: 1 addition & 0 deletions test-data/unit/lib-stub/enum.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Enum(metaclass=EnumMeta):

class IntEnum(int, Enum):
value: int
def __new__(cls: Type[_T], value: Union[int, _T]) -> _T: ...

def unique(enumeration: _T) -> _T: pass

Expand Down