Make PY2 enum more like PY3 enum, fixing bug in unique() (#1668)

This commit is contained in:
Guido van Rossum
2017-10-25 09:47:45 -07:00
committed by Matthias Kramm
parent c366a1be55
commit 994cfb5369

View File

@@ -1,8 +1,20 @@
from typing import List, Any, TypeVar, Type, Iterable, Iterator
from typing import List, Any, TypeVar, Union, Iterable, Iterator, TypeVar, Generic, Type, Sized, Reversible, Container, Mapping
from abc import ABCMeta
_T = TypeVar('_T', bound=Enum)
class EnumMeta(type, Iterable[Enum]):
def __iter__(self: Type[_T]) -> Iterator[_T]: ... # type: ignore
_S = TypeVar('_S', bound=Type[Enum])
# Note: EnumMeta actually subclasses type directly, not ABCMeta.
# This is a temporary workaround to allow multiple creation of enums with builtins
# such as str as mixins, which due to the handling of ABCs of builtin types, cause
# spurious inconsistent metaclass structure. See #1595.
class EnumMeta(ABCMeta, Iterable[Enum], Sized, Reversible[Enum], Container[Enum]):
def __iter__(self: Type[_T]) -> Iterator[_T]: ...
def __reversed__(self: Type[_T]) -> Iterator[_T]: ...
def __contains__(self, member: Any) -> bool: ...
def __getitem__(self: Type[_T], name: str) -> _T: ...
@property
def __members__(self: Type[_T]) -> Mapping[str, _T]: ...
class Enum(metaclass=EnumMeta):
def __new__(cls: Type[_T], value: Any) -> _T: ...
@@ -18,4 +30,4 @@ class Enum(metaclass=EnumMeta):
class IntEnum(int, Enum): ...
def unique(enumeration: _T) -> _T: ...
def unique(enumeration: _S) -> _S: ...