8000 Fix metaclass of Enum-subclasses functional API by elazarg · Pull Request #4942 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fix metaclass of Enum-subclasses functional API #4942

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 1 commit into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions mypy/semanal_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def build_enum_call_typeinfo(self, name: str, items: List[str], fullname: str) -
base = self.api.named_type_or_none(fullname)
assert base is not None
info = self.api.basic_new_typeinfo(name, base)
info.metaclass_type = info.calculate_metaclass_type()
info.is_enum = True
for item in items:
var = Var(item)
Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-enum.test
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,16 @@ x = y
[out]
main:8: error: Incompatible types in assignment (expression has type "__main__.B.E", variable has type "__main__.A.E")

[case testFunctionalEnumProtocols]
from enum import IntEnum
Color = IntEnum('Color', 'red green blue')
reveal_type(Color['green']) # E: Revealed type is '__main__.Color'
for c in Color:
reveal_type(c) # E: Revealed type is '__main__.Color*'
reveal_type(list(Color)) # E: Revealed type is 'builtins.list[__main__.Color*]'

[builtins fixtures/list.pyi]

[case testEnumWorkWithForward]
from enum import Enum
a: E = E.x
Expand Down
12 changes: 7 additions & 5 deletions test-data/unit/lib-stub/enum.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from typing import Any, TypeVar, Union
from typing import Any, TypeVar, Union, Type, Sized, Iterator, Mapping

class EnumMeta(type):
pass
_T = TypeVar('_T')

class EnumMeta(type, Sized):
def __iter__(self: Type[_T]) -> Iterator[_T]: pass
def __reversed__(self: Type[_T]) -> Iterator[_T]: pass
def __getitem__(self: Type[_T], name: str) -> _T: pass

class Enum(metaclass=EnumMeta):
def __new__(cls, value: Any) -> None: pass
Expand All @@ -17,8 +21,6 @@ class Enum(metaclass=EnumMeta):
class IntEnum(int, Enum):
value = 0 # type: int

_T = TypeVar('_T')

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

# In reality Flag and IntFlag are 3.6 only
Expand Down
0