@@ -1821,19 +1821,23 @@ if issubclass(fm, Baz):
18211821from typing import TypeVar
18221822
18231823class A: pass
1824- class B(A): pass
1824+ class B(A):
1825+ attr: int
18251826
18261827T = TypeVar('T', bound=A)
18271828
18281829def f(x: T) -> None:
18291830 if isinstance(x, B):
1830- reveal_type(x) # N: Revealed type is "__main__.B"
1831+ reveal_type(x) # N: Revealed type is "T`-1"
1832+ reveal_type(x.attr) # N: Revealed type is "builtins.int"
18311833 else:
18321834 reveal_type(x) # N: Revealed type is "T`-1"
1835+ x.attr # E: "T" has no attribute "attr"
18331836 reveal_type(x) # N: Revealed type is "T`-1"
1837+ x.attr # E: "T" has no attribute "attr"
18341838[builtins fixtures/isinstance.pyi]
18351839
1836- [case testIsinstanceAndNegativeNarrowTypeVariableWithUnionBound ]
1840+ [case testIsinstanceAndNegativeNarrowTypeVariableWithUnionBound1 ]
18371841from typing import Union, TypeVar
18381842
18391843class A:
@@ -1845,9 +1849,11 @@ T = TypeVar("T", bound=Union[A, B])
18451849
18461850def f(x: T) -> T:
18471851 if isinstance(x, A):
1848- reveal_type(x) # N: Revealed type is "__main__.A "
1852+ reveal_type(x) # N: Revealed type is "T`-1 "
18491853 x.a
1850- x.b # E: "A" has no attribute "b"
1854+ x.b # E: "T" has no attribute "b"
1855+ if bool():
1856+ return x
18511857 else:
18521858 reveal_type(x) # N: Revealed type is "T`-1"
18531859 x.a # E: "T" has no attribute "a"
@@ -1857,6 +1863,24 @@ def f(x: T) -> T:
18571863 return x
18581864[builtins fixtures/isinstance.pyi]
18591865
1866+ [case testIsinstanceAndNegativeNarrowTypeVariableWithUnionBound2]
1867+ from typing import Union, TypeVar
1868+
1869+ class A:
1870+ a: int
1871+ class B:
1872+ b: int
1873+
1874+ T = TypeVar("T", bound=Union[A, B])
1875+
1876+ def f(x: T) -> T:
1877+ if isinstance(x, A):
1878+ return x
1879+ x.a # E: "T" has no attribute "a"
1880+ x.b # OK
1881+ return x
1882+ [builtins fixtures/isinstance.pyi]
1883+
18601884[case testIsinstanceAndTypeType]
18611885from typing import Type
18621886def f(x: Type[int]) -> None:
0 commit comments