forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-enum.test
More file actions
458 lines (405 loc) · 11.3 KB
/
check-enum.test
File metadata and controls
458 lines (405 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
-- This test file checks Enum
[case testEnumBasics]
from enum import Enum
class Medal(Enum):
gold = 1
silver = 2
bronze = 3
reveal_type(Medal.bronze) # E: Revealed type is '__main__.Medal'
m = Medal.gold
if int():
m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Medal")
[case testEnumFromEnumMetaBasics]
from enum import EnumMeta
class Medal(metaclass=EnumMeta):
gold = 1
silver = "hello"
bronze = None
# Without __init__ the definition fails at runtime, but we want to verify that mypy
# uses `enum.EnumMeta` and not `enum.Enum` as the definition of what is enum.
def __init__(self, *args): pass
reveal_type(Medal.bronze) # E: Revealed type is '__main__.Medal'
m = Medal.gold
if int():
m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Medal")
[case testEnumFromEnumMetaSubclass]
from enum import EnumMeta
class Achievement(metaclass=EnumMeta): pass
class Medal(Achievement):
gold = 1
silver = "hello"
bronze = None
# See comment in testEnumFromEnumMetaBasics
def __init__(self, *args): pass
reveal_type(Medal.bronze) # E: Revealed type is '__main__.Medal'
m = Medal.gold
if int():
m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Medal")
[case testEnumFromEnumMetaGeneric]
from enum import EnumMeta
from typing import Generic, TypeVar
T = TypeVar("T")
class Medal(Generic[T], metaclass=EnumMeta): # E: Enum class cannot be generic
q = None
[case testEnumNameAndValue]
from enum import Enum
class Truth(Enum):
true = True
false = False
x = ''
x = Truth.true.name
reveal_type(Truth.true.name)
reveal_type(Truth.false.value)
[builtins fixtures/bool.pyi]
[out]
main:7: error: Revealed type is 'builtins.str'
main:8: error: Revealed type is 'Any'
[case testEnumUnique]
import enum
@enum.unique
class E(enum.Enum):
x = 1
y = 1 # NOTE: This duplicate value is not detected by mypy at the moment
x = 1
x = E.x
[out]
main:7: error: Incompatible types in assignment (expression has type "E", variable has type "int")
[case testIntEnum_assignToIntVariable]
from enum import IntEnum
class N(IntEnum):
x = 1
y = 1
n = 1
if int():
n = N.x # Subclass of int, so it's okay
s = ''
if int():
s = N.y # E: Incompatible types in assignment (expression has type "N", variable has type "str")
[case testIntEnum_functionTakingIntEnum]
from enum import IntEnum
class SomeIntEnum(IntEnum):
x = 1
def takes_some_int_enum(n: SomeIntEnum):
pass
takes_some_int_enum(SomeIntEnum.x)
takes_some_int_enum(1) # Error
takes_some_int_enum(SomeIntEnum(1)) # How to deal with the above
[out]
main:7: error: Argument 1 to "takes_some_int_enum" has incompatible type "int"; expected "SomeIntEnum"
[case testIntEnum_functionTakingInt]
from enum import IntEnum
class SomeIntEnum(IntEnum):
x = 1
def takes_int(i: int):
pass
takes_int(SomeIntEnum.x)
takes_int(2)
[case testIntEnum_functionReturningIntEnum]
from enum import IntEnum
class SomeIntEnum(IntEnum):
x = 1
def returns_some_int_enum() -> SomeIntEnum:
return SomeIntEnum.x
an_int = 1
an_int = returns_some_int_enum()
an_enum = SomeIntEnum.x
an_enum = returns_some_int_enum()
[out]
[case testEnumMethods]
from enum import Enum
class Color(Enum):
red = 1
green = 2
def m(self, x: int): pass
@staticmethod
def m2(x: int): pass
Color.red.m('')
Color.m2('')
[builtins fixtures/staticmethod.pyi]
[out]
main:11: error: Argument 1 to "m" of "Color" has incompatible type "str"; expected "int"
main:12: error: Argument 1 to "m2" of "Color" has incompatible type "str"; expected "int"
[case testIntEnum_ExtendedIntEnum_functionTakingExtendedIntEnum]
from enum import IntEnum
class ExtendedIntEnum(IntEnum):
pass
class SomeExtIntEnum(ExtendedIntEnum):
x = 1
def takes_int(i: int):
pass
takes_int(SomeExtIntEnum.x)
def takes_some_ext_int_enum(s: SomeExtIntEnum):
pass
takes_some_ext_int_enum(SomeExtIntEnum.x)
[case testNamedTupleEnum]
from typing import NamedTuple
from enum import Enum
N = NamedTuple('N', [('bar', int)])
class E(N, Enum):
X = N(1)
def f(x: E) -> None: pass
f(E.X)
[case testEnumCall]
from enum import IntEnum
class E(IntEnum):
a = 1
x = None # type: int
reveal_type(E(x))
[out]
main:5: error: Revealed type is '__main__.E'
[case testEnumIndex]
from enum import IntEnum
class E(IntEnum):
a = 1
s = None # type: str
reveal_type(E[s])
[out]
main:5: error: Revealed type is '__main__.E'
[case testEnumIndexError]
from enum import IntEnum
class E(IntEnum):
a = 1
E[1] # E: Enum index should be a string (actual index type "int")
x = E[1] # E: Enum index should be a string (actual index type "int")
[case testEnumIndexIsNotAnAlias]
from enum import Enum
class E(Enum):
a = 1
b = 2
reveal_type(E['a']) # E: Revealed type is '__main__.E'
E['a']
x = E['a']
reveal_type(x) # E: Revealed type is '__main__.E'
def get_member(name: str) -> E:
val = E[name]
return val
reveal_type(get_member('a')) # E: Revealed type is '__main__.E'
[case testGenericEnum]
from enum import Enum
from typing import Generic, TypeVar
T = TypeVar('T')
class F(Generic[T], Enum): # E: Enum class cannot be generic
x: T
y: T
reveal_type(F[int].x) # E: Revealed type is '__main__.F[builtins.int*]'
[case testEnumFlag]
from enum import Flag
class C(Flag):
a = 1
b = 2
x = C.a
if int():
x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "C")
if int():
x = x | C.b
[case testEnumIntFlag]
from enum import IntFlag
class C(IntFlag):
a = 1
b = 2
x = C.a
if int():
x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "C")
if int():
x = x | C.b
[case testAnonymousEnum]
from enum import Enum
class A:
def f(self) -> None:
class E(Enum):
a = 1
self.x = E.a
a = A()
reveal_type(a.x)
[out]
main:8: error: Revealed type is '__main__.E@4'
[case testEnumInClassBody]
from enum import Enum
class A:
class E(Enum):
a = 1
class B:
class E(Enum):
a = 1
x = A.E.a
y = B.E.a
if int():
x = y # E: Incompatible types in assignment (expression has type "__main__.B.E", variable has type "__main__.A.E")
[case testFunctionalEnumString]
from enum import Enum, IntEnum
E = Enum('E', 'foo bar')
I = IntEnum('I', ' bar, baz ')
reveal_type(E.foo)
reveal_type(E.bar.value)
reveal_type(I.bar)
reveal_type(I.baz.value)
[out]
main:4: error: Revealed type is '__main__.E'
main:5: error: Revealed type is 'Any'
main:6: error: Revealed type is '__main__.I'
main:7: error: Revealed type is 'builtins.int'
[case testFunctionalEnumListOfStrings]
from enum import Enum, IntEnum
E = Enum('E', ('foo', 'bar'))
F = IntEnum('F', ['bar', 'baz'])
reveal_type(E.foo)
reveal_type(F.baz)
[out]
main:4: error: Revealed type is '__main__.E'
main:5: error: Revealed type is '__main__.F'
[case testFunctionalEnumListOfPairs]
from enum import Enum, IntEnum
E = Enum('E', [('foo', 1), ['bar', 2]])
F = IntEnum('F', (['bar', 1], ('baz', 2)))
reveal_type(E.foo)
reveal_type(F.baz)
reveal_type(E.foo.value)
reveal_type(F.bar.name)
[out]
main:4: error: Revealed type is '__main__.E'
main:5: error: Revealed type is '__main__.F'
main:6: error: Revealed type is 'Any'
main:7: error: Revealed type is 'builtins.str'
[case testFunctionalEnumDict]
from enum import Enum, IntEnum
E = Enum('E', {'foo': 1, 'bar': 2})
F = IntEnum('F', {'bar': 1, 'baz': 2})
reveal_type(E.foo)
reveal_type(F.baz)
reveal_type(E.foo.value)
reveal_type(F.bar.name)
[out]
main:4: error: Revealed type is '__main__.E'
main:5: error: Revealed type is '__main__.F'
main:6: error: Revealed type is 'Any'
main:7: error: Revealed type is 'builtins.str'
[case testFunctionalEnumErrors]
from enum import Enum, IntEnum
A = Enum('A')
B = Enum('B', 42)
C = Enum('C', 'a b', 'x')
D = Enum('D', foo)
bar = 'x y z'
E = Enum('E', bar)
I = IntEnum('I')
J = IntEnum('I', 42)
K = IntEnum('I', 'p q', 'z')
L = Enum('L', ' ')
M = Enum('M', ())
N = IntEnum('M', [])
P = Enum('P', [42])
Q = Enum('Q', [('a', 42, 0)])
R = IntEnum('R', [[0, 42]])
S = Enum('S', {1: 1})
T = Enum('T', keyword='a b')
U = Enum('U', *['a'])
V = Enum('U', **{'a': 1})
W = Enum('W', 'a b')
W.c
[typing fixtures/typing-full.pyi]
[out]
main:2: error: Too few arguments for Enum()
main:3: error: Enum() expects a string, tuple, list or dict literal as the second argument
main:4: error: Too many arguments for Enum()
main:5: error: Enum() expects a string, tuple, list or dict literal as the second argument
main:5: error: Name 'foo' is not defined
main:7: error: Enum() expects a string, tuple, list or dict literal as the second argument
main:8: error: Too few arguments for IntEnum()
main:9: error: IntEnum() expects a string, tuple, list or dict literal as the second argument
main:10: error: Too many arguments for IntEnum()
main:11: error: Enum() needs at least one item
main:12: error: Enum() needs at least one item
main:13: error: IntEnum() needs at least one item
main:14: error: Enum() with tuple or list expects strings or (name, value) pairs
main:15: error: Enum() with tuple or list expects strings or (name, value) pairs
main:16: error: IntEnum() with tuple or list expects strings or (name, value) pairs
main:17: error: Enum() with dict literal requires string literals
main:18: error: Unexpected arguments to Enum()
main:19: error: Unexpected arguments to Enum()
main:20: error: Unexpected arguments to Enum()
main:22: error: "Type[W]" has no attribute "c"
[case testFunctionalEnumFlag]
from enum import Flag, IntFlag
A = Flag('A', 'x y')
B = IntFlag('B', 'a b')
reveal_type(A.x)
reveal_type(B.a)
[out]
main:4: error: Revealed type is '__main__.A'
main:5: error: Revealed type is '__main__.B'
[case testAnonymousFunctionalEnum]
from enum import Enum
class A:
def f(self) -> None:
E = Enum('E', 'a b')
self.x = E.a
a = A()
reveal_type(a.x)
[out]
main:7: error: Revealed type is '__main__.A.E@4'
[case testFunctionalEnumInClassBody]
from enum import Enum
class A:
E = Enum('E', 'a b')
class B:
E = Enum('E', 'a b')
x = A.E.a
y = B.E.a
if int():
x = y # E: 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
class E(Enum):
x = 1
y = 2
[out]
[case testEnumWorkWithForward2]
from enum import Enum
b: F
F = Enum('F', {'x': 1, 'y': 2})
def fn(x: F) -> None:
pass
fn(b)
[out]
[case testFunctionalEnum_python2]
from enum import Enum
Eu = Enum(u'Eu', u'a b')
Eb = Enum(b'Eb', b'a b')
Gu = Enum(u'Gu', {u'a': 1})
Gb = Enum(b'Gb', {b'a': 1})
Hu = Enum(u'Hu', [u'a'])
Hb = Enum(b'Hb', [b'a'])
Eu.a
Eb.a
Gu.a
Gb.a
Hu.a
Hb.a
[out]
[case testEnumIncremental]
import m
reveal_type(m.E.a)
reveal_type(m.F.b)
[file m.py]
from enum import Enum
class E(Enum):
a = 1
b = 2
F = Enum('F', 'a b')
[rechecked]
[stale]
[out1]
main:2: error: Revealed type is 'm.E'
main:3: error: Revealed type is 'm.F'
[out2]
main:2: error: Revealed type is 'm.E'
main:3: error: Revealed type is 'm.F'