File tree 5 files changed +21
-25
lines changed
5 files changed +21
-25
lines changed Original file line number Diff line number Diff line change @@ -81,9 +81,6 @@ def get_column(self) -> int:
81
81
LITERAL_TYPE = 1
82
82
LITERAL_NO = 0
83
83
84
- # Hard coded name of Enum baseclass.
85
- ENUM_BASECLASS = "enum.Enum"
86
-
87
84
node_kinds = {
88
85
LDEF : 'Ldef' ,
89
86
GDEF : 'Gdef' ,
@@ -2085,7 +2082,6 @@ def calculate_mro(self) -> None:
2085
2082
mro = linearize_hierarchy (self )
2086
2083
assert mro , "Could not produce a MRO at all for %s" % (self ,)
2087
2084
self .mro = mro
2088
- self .is_enum = self ._calculate_is_enum ()
2089
2085
2090
2086
def calculate_metaclass_type (self ) -> 'Optional[mypy.types.Instance]' :
2091
2087
declared = self .declared_metaclass
@@ -2108,17 +2104,6 @@ def is_metaclass(self) -> bool:
2108
2104
return (self .has_base ('builtins.type' ) or self .fullname () == 'abc.ABCMeta' or
2109
2105
self .fallback_to_any )
2110
2106
2111
- def _calculate_is_enum (self ) -> bool :
2112
- """
2113
- If this is "enum.Enum" itself, then yes, it's an enum.
2114
- If the flag .is_enum has been set on anything in the MRO, it's an enum.
2115
- """
2116
- if self .fullname () == ENUM_BASECLASS :
2117
- return True
2118
- if self .mro :
2119
- return any (type_info .is_enum for type_info in self .mro )
2120
- return False
2121
-
2122
2107
def has_base (self , fullname : str ) -> bool :
2123
2108
"""Return True if type has a base type with the specified name.
2124
2109
Original file line number Diff line number Diff line change @@ -1083,8 +1083,6 @@ def analyze_base_classes(self, defn: ClassDef) -> None:
1083
1083
# the MRO. Fix MRO if needed.
1084
1084
if info .mro and info .mro [- 1 ].fullname () != 'builtins.object' :
1085
1085
info .mro .append (self .object_type ().type )
1086
- if defn .info .is_enum and defn .type_vars :
1087
- self .fail ("Enum class cannot be generic" , defn )
1088
1086
1089
1087
def update_metaclass (self , defn : ClassDef ) -> None :
1090
1088
"""Lookup for special metaclass declarations, and update defn fields accordingly.
@@ -1221,6 +1219,11 @@ def analyze_metaclass(self, defn: ClassDef) -> None:
1221
1219
# do not declare explicit metaclass, but it's harder to catch at this stage
1222
1220
if defn .metaclass is not None :
1223
1221
self .fail ("Inconsistent metaclass structure for '%s'" % defn .name , defn )
1222
+ else :
1223
+ if defn .info .metaclass_type .type .fullname () == 'enum.EnumMeta' :
1224
+ defn .info .is_enum = True
1225
+ if defn .type_vars :
1226
+ self .fail ("Enum class cannot be generic" , defn )
1224
1227
1225
1228
def object_type (self ) -> Instance :
1226
1229
return self .named_type ('__builtins__.object' )
Original file line number Diff line number Diff line change @@ -183,10 +183,6 @@ def visit_instance(self, left: Instance) -> bool:
183
183
if isinstance (item , AnyType ):
184
184
return True
185
185
if isinstance (item , Instance ):
186
- # Special-case enum since we don't have better way of expressing it
187
- if (is_named_instance (left , 'enum.EnumMeta' )
188
- and is_named_instance (item , 'enum.Enum' )):
189
- return True
190
186
return is_named_instance (item , 'builtins.object' )
191
187
if isinstance (right , CallableType ):
192
188
# Special case: Instance can be a subtype of Callable.
Original file line number Diff line number Diff line change @@ -6,10 +6,19 @@ class Medal(Enum):
6
6
gold = 1
7
7
silver = 2
8
8
bronze = 3
9
+ reveal_type(Medal.bronze) # E: Revealed type is '__main__.Medal'
9
10
m = Medal.gold
10
- m = 1
11
- [out]
12
- main:7: error: Incompatible types in assignment (expression has type "int", variable has type "Medal")
11
+ m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Medal")
12
+
13
+ [case testEnumFromEnumMetaBasics]
14
+ from enum import EnumMeta
15
+ class Medal(metaclass=EnumMeta):
16
+ gold = 1
17
+ silver = "hello"
18
+ bronze = None
19
+ reveal_type(Medal.bronze) # E: Revealed type is '__main__.Medal'
20
+ m = Medal.gold
21
+ m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Medal")
13
22
14
23
[case testEnumNameAndValue]
15
24
from enum import Enum
Original file line number Diff line number Diff line change 1
1
from typing import Any , TypeVar , Union
2
2
3
- class Enum :
3
+ class EnumMeta (type ):
4
+ pass
5
+
6
+ class Enum (metaclass = EnumMeta ):
4
7
def __new__ (cls , value : Any ) -> None : pass
5
8
def __repr__ (self ) -> str : pass
6
9
def __str__ (self ) -> str : pass
You can’t perform that action at this time.
0 commit comments