1+ [case testUnionTypeVarInferenceBasic]
2+ from typing import TypeVar, Union
3+
4+ class A: pass
5+ class B: pass
6+ class C: pass
7+
8+ T = TypeVar('T')
9+
10+ def foo(x: Union[T, A]) -> T: ...
11+
12+ obj: Union[B, C]
13+ reveal_type(foo(obj)) # N: Revealed type is "__main__.B | __main__.C"
14+
15+ [builtins fixtures/tuple.pyi]
16+
17+ [case testUnionTypeVarInferenceSingle]
18+ from typing import TypeVar, Union
19+
20+ class A: pass
21+ class B: pass
22+
23+ T = TypeVar('T')
24+
25+ def foo(x: Union[T, A]) -> T: ...
26+
27+ obj: B
28+ reveal_type(foo(obj)) # N: Revealed type is "__main__.B"
29+
30+ [builtins fixtures/tuple.pyi]
31+
32+ [case testUnionTypeVarInferenceThreeWay]
33+ from typing import TypeVar, Union
34+
35+ class A: pass
36+ class B: pass
37+ class C: pass
38+ class D: pass
39+
40+ T = TypeVar('T')
41+
42+ def foo(x: Union[T, A]) -> T: ...
43+
44+ obj: Union[B, C, D]
45+ reveal_type(foo(obj)) # N: Revealed type is "__main__.B | __main__.C | __main__.D"
46+
47+ [builtins fixtures/tuple.pyi]
48+
49+ [case testUnionTypeVarInferenceOverlapping]
50+ from typing import TypeVar, Union
51+
52+ class A: pass
53+ class B: pass
54+
55+ T = TypeVar('T')
56+
57+ def foo(x: Union[T, A]) -> T: ...
58+
59+ obj: Union[A, B]
60+ reveal_type(foo(obj)) # N: Revealed type is "__main__.A | __main__.B"
61+
62+ [builtins fixtures/tuple.pyi]
63+
64+ [case testUnionTypeVarInferenceJustA]
65+ from typing import TypeVar, Union
66+
67+ class A: pass
68+
69+ T = TypeVar('T')
70+
71+ def foo(x: Union[T, A]) -> T: ...
72+
73+ obj: A
74+ reveal_type(foo(obj)) # N: Revealed type is "__main__.A"
75+
76+ [builtins fixtures/tuple.pyi]
77+
78+ [case testUnionTypeVarInferenceComplex]
79+ from typing import TypeVar, Union
80+ from dataclasses import dataclass
81+ import pathlib
82+
83+ class Cancelled: pass
84+
85+ T = TypeVar('T')
86+
87+ @dataclass
88+ class CreateProject:
89+ jsonFilePath: pathlib.Path
90+
91+ @dataclass
92+ class LoadProject:
93+ jsonFilePath: pathlib.Path
94+
95+ @dataclass
96+ class MigrateProject:
97+ oldJsonFilePath: pathlib.Path
98+ newProjectFolderPath: pathlib.Path
99+
100+ Project = Union[CreateProject, LoadProject, MigrateProject]
101+
102+ def getProject() -> Union[Project, Cancelled]: ...
103+
104+ def value(maybeCancelled: Union[T, Cancelled]) -> T: ...
105+
106+ def main() -> None:
107+ maybeCancelled = getProject()
108+ project: Project = reveal_type(value(maybeCancelled)) # N: Revealed type is "__main__.CreateProject | __main__.LoadProject | __main__.MigrateProject"
109+
110+ [builtins fixtures/tuple.pyi]
111+ [typing fixtures/typing-medium.pyi]
112+
113+ [case testUnionTypeVarInferenceComplex]
114+ from typing import TypeVar, Union
115+ import pathlib
116+
117+ class Cancelled: pass
118+
119+ T = TypeVar('T')
120+
121+ @dataclass
122+ class CreateProject:
123+ jsonFilePath: pathlib.Path
124+
125+ @dataclass
126+ class LoadProject:
127+ jsonFilePath: pathlib.Path
128+
129+ @dataclass
130+ class MigrateProject:
131+ oldJsonFilePath: pathlib.Path
132+ newProjectFolderPath: pathlib.Path
133+
134+ Project = Union[CreateProject, LoadProject, MigrateProject]
135+
136+ def getProject() -> Union[Project, Cancelled]: ...
137+
138+ def value(maybeCancelled: Union[T, Cancelled]) -> T: ...
139+
140+ def main() -> None:
141+ maybeCancelled = getProject()
142+ project: Project = reveal_type(value(maybeCancelled)) # N: Revealed type is "__main__.CreateProject | __main__.LoadProject | __main__.MigrateProject"
143+
144+ [builtins fixtures/tuple.pyi]
145+ [typing fixtures/typing-medium.pyi]
0 commit comments