@@ -2022,3 +2022,73 @@ def f(x: Union[int, Sequence[int]]) -> None:
20222022 ):
20232023 reveal_type(x) # N: Revealed type is "Tuple[builtins.int, builtins.int]"
20242024[builtins fixtures/len.pyi]
2025+
2026+ [case testNarrowingIsSubclassNoneType1]
2027+ from typing import Type, Union
2028+
2029+ def f(cls: Type[Union[None, int]]) -> None:
2030+ if issubclass(cls, int):
2031+ reveal_type(cls) # N: Revealed type is "Type[builtins.int]"
2032+ else:
2033+ reveal_type(cls) # N: Revealed type is "Type[None]"
2034+ [builtins fixtures/isinstance.pyi]
2035+
2036+ [case testNarrowingIsSubclassNoneType2]
2037+ from typing import Type, Union
2038+
2039+ def f(cls: Type[Union[None, int]]) -> None:
2040+ if issubclass(cls, type(None)):
2041+ reveal_type(cls) # N: Revealed type is "Type[None]"
2042+ else:
2043+ reveal_type(cls) # N: Revealed type is "Type[builtins.int]"
2044+ [builtins fixtures/isinstance.pyi]
2045+
2046+ [case testNarrowingIsSubclassNoneType3]
2047+ from typing import Type, Union
2048+
2049+ NoneType_ = type(None)
2050+
2051+ def f(cls: Type[Union[None, int]]) -> None:
2052+ if issubclass(cls, NoneType_):
2053+ reveal_type(cls) # N: Revealed type is "Type[None]"
2054+ else:
2055+ reveal_type(cls) # N: Revealed type is "Type[builtins.int]"
2056+ [builtins fixtures/isinstance.pyi]
2057+
2058+ [case testNarrowingIsSubclassNoneType4]
2059+ # flags: --python-version 3.10
2060+
2061+ from types import NoneType
2062+ from typing import Type, Union
2063+
2064+ def f(cls: Type[Union[None, int]]) -> None:
2065+ if issubclass(cls, NoneType):
2066+ reveal_type(cls) # N: Revealed type is "Type[None]"
2067+ else:
2068+ reveal_type(cls) # N: Revealed type is "Type[builtins.int]"
2069+ [builtins fixtures/isinstance.pyi]
2070+
2071+ [case testNarrowingIsInstanceNoIntersectionWithFinalTypeAndNoneType]
2072+ # flags: --warn-unreachable --python-version 3.10
2073+
2074+ from types import NoneType
2075+ from typing import final
2076+
2077+ class X: ...
2078+ class Y: ...
2079+ @final
2080+ class Z: ...
2081+
2082+ x: X
2083+
2084+ if isinstance(x, (Y, Z)):
2085+ reveal_type(x) # N: Revealed type is "__main__.<subclass of "X" and "Y">"
2086+ if isinstance(x, (Y, NoneType)):
2087+ reveal_type(x) # N: Revealed type is "__main__.<subclass of "X" and "Y">1"
2088+ if isinstance(x, (Y, Z, NoneType)):
2089+ reveal_type(x) # N: Revealed type is "__main__.<subclass of "X" and "Y">2"
2090+ if isinstance(x, (Z, NoneType)): # E: Subclass of "X" and "Z" cannot exist: "Z" is final \
2091+ # E: Subclass of "X" and "NoneType" cannot exist: "NoneType" is final
2092+ reveal_type(x) # E: Statement is unreachable
2093+
2094+ [builtins fixtures/isinstance.pyi]
0 commit comments