@@ -1583,6 +1583,61 @@ def f(x: Union[int, str], typ: type) -> None:
15831583 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
15841584[builtins fixtures/isinstancelist.pyi]
15851585
1586+ [case testIsInstanceWithUnknownTypeMultipleNarrowing]
1587+ # flags: --strict-equality --warn-unreachable --python-version 3.10
1588+ from __future__ import annotations
1589+ from typing import Iterable
1590+ from typing_extensions import TypeAlias
1591+ import types
1592+
1593+ # Regression test for https://github.com/python/mypy/issues/21181
1594+ # We don't have the same type context as with the real stubs, so sort of fake it
1595+ _ClassInfoLike: TypeAlias = "type | tuple[_ClassInfoLike, ...]"
1596+
1597+ class A: ...
1598+ class B(A): ...
1599+
1600+ def fake_type_context(ts: list[type[A]]) -> _ClassInfoLike:
1601+ return tuple(ts) # E: Too many arguments for "tuple"
1602+
1603+
1604+ def f1(x: A | None) -> None:
1605+ if x is not None:
1606+ reveal_type(x) # N: Revealed type is "__main__.A"
1607+ if isinstance(x, object):
1608+ reveal_type(x) # N: Revealed type is "__main__.A"
1609+
1610+
1611+ def f2(x: A | None, ts: list[type[A]]) -> None:
1612+ if x is not None:
1613+ reveal_type(x) # N: Revealed type is "__main__.A"
1614+ if isinstance(x, fake_type_context(ts)):
1615+ reveal_type(x) # N: Revealed type is "__main__.A"
1616+
1617+
1618+ def f3(x: A | None, t: type | type[A]) -> None:
1619+ if x is not None:
1620+ reveal_type(x) # N: Revealed type is "__main__.A"
1621+ if isinstance(x, t):
1622+ reveal_type(x) # N: Revealed type is "__main__.A"
1623+
1624+
1625+ def f4(x: A | None, t: type) -> None:
1626+ if x is not None:
1627+ reveal_type(x) # N: Revealed type is "__main__.A"
1628+ if isinstance(x, t):
1629+ reveal_type(x) # N: Revealed type is "__main__.A"
1630+
1631+
1632+ def f5(x: object | None, ta: type[A], tb: type[B]) -> None:
1633+ if x is not None:
1634+ reveal_type(x) # N: Revealed type is "builtins.object"
1635+ if isinstance(x, ta):
1636+ reveal_type(x) # N: Revealed type is "__main__.A"
1637+ if isinstance(x, tb):
1638+ reveal_type(x) # N: Revealed type is "__main__.B"
1639+ [builtins fixtures/isinstancelist.pyi]
1640+
15861641[case testIsInstanceWithBoundedType]
15871642# flags: --warn-unreachable
15881643from typing import Union, Type
0 commit comments