From 994cfb5369c2046dcb693cc53a1f030a61c55e9e Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 25 Oct 2017 09:47:45 -0700 Subject: [PATCH] Make PY2 enum more like PY3 enum, fixing bug in unique() (#1668) --- third_party/2/enum.pyi | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/third_party/2/enum.pyi b/third_party/2/enum.pyi index ae8a91c62..2bab1a883 100644 --- a/third_party/2/enum.pyi +++ b/third_party/2/enum.pyi @@ -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: ...