@@ -55,12 +55,12 @@ class B: y = 2
55
55
class C: pass
56
56
class D: pass
57
57
58
- u = None # type : Union[A, C, D]
59
- v = None # type : Union[C, D]
60
- w = None # type : Union[A, B]
61
- x = None # type : Union[A, C]
62
- y = None # type : int
63
- z = None # type : str
58
+ u: Union[A, C, D]
59
+ v: Union[C, D]
60
+ w: Union[A, B]
61
+ x: Union[A, C]
62
+ y: int
63
+ z: str
64
64
65
65
if int():
66
66
y = w.y
@@ -89,9 +89,9 @@ class B:
89
89
class C:
90
90
def foo(self) -> str: pass
91
91
92
- x = None # type : Union[A, B]
93
- y = None # type : Union[A, C]
94
- i = None # type : int
92
+ x: Union[A, B]
93
+ y: Union[A, C]
94
+ i: int
95
95
96
96
x.foo()
97
97
y.foo()
@@ -103,7 +103,7 @@ if int():
103
103
104
104
[case testUnionIndexing]
105
105
from typing import Union, List
106
- x = None # type : Union[List[int], str]
106
+ x: Union[List[int], str]
107
107
x[2]
108
108
x[2] + 1 # E: Unsupported operand types for + ("str" and "int") \
109
109
# N: Left operand is of type "Union[int, str]"
@@ -132,6 +132,7 @@ def f(x: Union[int, str]) -> int: pass
132
132
def f(x: type) -> str: pass
133
133
134
134
[case testUnionWithNoneItem]
135
+ # flags: --no-strict-optional
135
136
from typing import Union
136
137
def f() -> Union[int, None]: pass
137
138
x = 1
@@ -229,10 +230,10 @@ T = TypeVar('T')
229
230
S = TypeVar('S')
230
231
def u(x: T, y: S) -> Union[S, T]: pass
231
232
232
- a = None # type : Any
233
+ a: Any
233
234
234
- reveal_type(u(C(), None)) # N: Revealed type is "__main__.C"
235
- reveal_type(u(None, C())) # N: Revealed type is "__main__.C"
235
+ reveal_type(u(C(), None)) # N: Revealed type is "Union[None, __main__.C] "
236
+ reveal_type(u(None, C())) # N: Revealed type is "Union[ __main__.C, None] "
236
237
237
238
reveal_type(u(C(), a)) # N: Revealed type is "Union[Any, __main__.C]"
238
239
reveal_type(u(a, C())) # N: Revealed type is "Union[__main__.C, Any]"
@@ -266,7 +267,7 @@ class M(Generic[V]):
266
267
267
268
def f(x: M[C]) -> None:
268
269
y = x.get(None)
269
- reveal_type(y) # N: Revealed type is "__main__.C"
270
+ reveal_type(y) # N: Revealed type is "Union[ __main__.C, None] "
270
271
271
272
[case testUnionSimplificationSpecialCases2]
272
273
from typing import Any, TypeVar, Union
@@ -277,15 +278,15 @@ T = TypeVar('T')
277
278
S = TypeVar('S')
278
279
def u(x: T, y: S) -> Union[S, T]: pass
279
280
280
- a = None # type : Any
281
+ a: Any
281
282
282
283
# Base-class-Any and None, simplify
283
- reveal_type(u(C(), None)) # N: Revealed type is "__main__.C"
284
- reveal_type(u(None, C())) # N: Revealed type is "__main__.C"
284
+ reveal_type(u(C(), None)) # N: Revealed type is "Union[None, __main__.C] "
285
+ reveal_type(u(None, C())) # N: Revealed type is "Union[ __main__.C, None] "
285
286
286
287
# Normal instance type and None, simplify
287
- reveal_type(u(1, None)) # N: Revealed type is "builtins.int"
288
- reveal_type(u(None, 1)) # N: Revealed type is "builtins.int"
288
+ reveal_type(u(1, None)) # N: Revealed type is "Union[None, builtins.int] "
289
+ reveal_type(u(None, 1)) # N: Revealed type is "Union[ builtins.int, None] "
289
290
290
291
# Normal instance type and base-class-Any, no simplification
291
292
reveal_type(u(C(), 1)) # N: Revealed type is "Union[builtins.int, __main__.C]"
@@ -317,10 +318,10 @@ S = TypeVar('S')
317
318
R = TypeVar('R')
318
319
def u(x: T, y: S, z: R) -> Union[R, S, T]: pass
319
320
320
- a = None # type : Any
321
+ a: Any
321
322
322
323
reveal_type(u(1, 1, 1)) # N: Revealed type is "builtins.int"
323
- reveal_type(u(C(), C(), None)) # N: Revealed type is "__main__.C"
324
+ reveal_type(u(C(), C(), None)) # N: Revealed type is "Union[None, __main__.C] "
324
325
reveal_type(u(a, a, 1)) # N: Revealed type is "Union[builtins.int, Any]"
325
326
reveal_type(u(a, C(), a)) # N: Revealed type is "Union[Any, __main__.C]"
326
327
reveal_type(u('', 1, 1)) # N: Revealed type is "Union[builtins.int, builtins.str]"
@@ -370,9 +371,9 @@ T = TypeVar('T')
370
371
S = TypeVar('S')
371
372
def u(x: T, y: S) -> Union[S, T]: pass
372
373
373
- t_o = None # type : Type[object]
374
- t_s = None # type : Type[str]
375
- t_a = None # type : Type[Any]
374
+ t_o: Type[object]
375
+ t_s: Type[str]
376
+ t_a: Type[Any]
376
377
377
378
# Two identical items
378
379
reveal_type(u(t_o, t_o)) # N: Revealed type is "Type[builtins.object]"
@@ -397,10 +398,10 @@ T = TypeVar('T')
397
398
S = TypeVar('S')
398
399
def u(x: T, y: S) -> Union[S, T]: pass
399
400
400
- t_o = None # type : Type[object]
401
- t_s = None # type : Type[str]
402
- t_a = None # type : Type[Any]
403
- t = None # type : type
401
+ t_o: Type[object]
402
+ t_s: Type[str]
403
+ t_a: Type[Any]
404
+ t: type
404
405
405
406
# Union with object
406
407
reveal_type(u(t_o, object())) # N: Revealed type is "builtins.object"
@@ -1010,6 +1011,7 @@ MYTYPE = List[Union[str, "MYTYPE"]] # E: Cannot resolve name "MYTYPE" (possible
1010
1011
[builtins fixtures/list.pyi]
1011
1012
1012
1013
[case testNonStrictOptional]
1014
+ # flags: --no-strict-optional
1013
1015
from typing import Optional, List
1014
1016
1015
1017
def union_test1(x):
0 commit comments