@@ -112,35 +112,33 @@ from typing import TypeVar
112112T = TypeVar('T', int, str)
113113def f(x: T) -> T:
114114 if isinstance(x, int):
115- return 2
115+ return 2 # E: Incompatible return value type (got "int", expected "str")
116116 return x
117117def g(x: T) -> T:
118118 if isinstance(x, str):
119- return ''
119+ return '' # E: Incompatible return value type (got "str", expected "int")
120120 return x
121121def h(x: T) -> T:
122122 if isinstance(x, int):
123123 return '' # E: Incompatible return value type (got "str", expected "int")
124124 return x
125125[builtins fixtures/isinstance.pyi]
126- [out]
127126
128127[case testIsinstanceAndTypeVarValues2]
129128from typing import TypeVar
130129T = TypeVar('T', int, str)
131130def f(x: T) -> T:
132131 if isinstance(x, int):
133- return 2
132+ return 2 # E: Incompatible return value type (got "int", expected "str")
134133 else:
135- return ''
134+ return '' # E: Incompatible return value type (got "str", expected "int")
136135def g(x: T) -> T:
137136 if isinstance(x, int):
138137 return '' # E: Incompatible return value type (got "str", expected "int")
139138 else:
140139 return 2 # E: Incompatible return value type (got "int", expected "str")
141140 return x
142141[builtins fixtures/isinstance.pyi]
143- [out]
144142
145143[case testIsinstanceAndTypeVarValues3]
146144from typing import TypeVar
@@ -149,8 +147,8 @@ def f(x: T) -> T:
149147 if isinstance(x, int):
150148 y = 1
151149 else:
152- y = ''
153- return y
150+ y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
151+ return y # E: Incompatible return value type (got "int", expected "str")
154152[builtins fixtures/isinstance.pyi]
155153
156154[case testIsinstanceAndTypeVarValues4]
@@ -160,10 +158,9 @@ def f(x: T) -> T:
160158 if isinstance(x, int):
161159 y = 1
162160 else:
163- y = object()
164- return y # E: Incompatible return value type (got "object ", expected "str")
161+ y = object() # E: Incompatible types in assignment (expression has type "object", variable has type "int")
162+ return y # E: Incompatible return value type (got "int ", expected "str")
165163[builtins fixtures/isinstance.pyi]
166- [out]
167164
168165[case testIsinstanceAndTypeVarValues5]
169166from typing import TypeVar
@@ -189,9 +186,10 @@ def f1(x: T1) -> None:
189186 x = y
190187 x = A() # E: Incompatible types in assignment (expression has type "A", variable has type "B")
191188 else:
192- x = B()
189+ x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
193190 x = y
194- x.foo() # E: "B" has no attribute "foo"
191+ x.foo() # E: "A" has no attribute "foo" \
192+ # E: "B" has no attribute "foo"
195193
196194class C:
197195 field: int
@@ -203,19 +201,19 @@ def f2(x: T2) -> None:
203201 if isinstance(x, C):
204202 # C and D are non-overlapping, so this branch is never checked
205203 x = y
206- x = C()
204+ x = C() # E: Incompatible types in assignment (expression has type "C", variable has type "D")
207205 else:
208- x = D()
206+ x = D() # E: Incompatible types in assignment (expression has type "D", variable has type "C")
209207 x = y
210- x.foo() # E: "D" has no attribute "foo"
208+ x.foo() # E: "C" has no attribute "foo" \
209+ # E: "D" has no attribute "foo"
211210
212211S = TypeVar('S', int, str)
213212def g(x: S) -> None:
214213 y = x
215214 if isinstance(x, int):
216215 x = y
217216[builtins fixtures/isinstance.pyi]
218- [out]
219217
220218[case testIsinstanceWithUserDefinedTypeAndTypeVarValues2]
221219from typing import TypeVar
@@ -226,14 +224,13 @@ def f(x: T) -> None:
226224 if isinstance(x, S):
227225 # This is checked only when type of x is str.
228226 x = y
229- x = S()
227+ x = S() # E: Incompatible types in assignment (expression has type "S", variable has type "int")
230228 x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "S")
231229 else:
232230 x = y
233- x = 1
231+ x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "S")
234232 x = S() # E: Incompatible types in assignment (expression has type "S", variable has type "int")
235233[builtins fixtures/isinstance.pyi]
236- [out]
237234
238235[case testTypeVarValuesAndNestedCalls]
239236from typing import TypeVar
@@ -740,8 +737,10 @@ W = TypeVar("W", int, str)
740737
741738def fn(w: W) -> W:
742739 if type(w) is str:
743- reveal_type(w) # N: Revealed type is "builtins.str"
740+ reveal_type(w) # N: Revealed type is "builtins.int" \
741+ # N: Revealed type is "builtins.str"
744742 elif type(w) is int:
745- reveal_type(w) # N: Revealed type is "builtins.int"
743+ reveal_type(w) # N: Revealed type is "builtins.int" \
744+ # N: Revealed type is "builtins.str"
746745 return w
747746[builtins fixtures/isinstance.pyi]
0 commit comments