Skip to content
Prev Previous commit
Next Next commit
new test
  • Loading branch information
hauntsaninja committed Dec 30, 2025
commit 92051d994189a8a4f3eae6ad256610b6ab90857f
31 changes: 31 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -2739,3 +2739,34 @@ def foo(x: dict[str, Any]) -> bool:
return True
raise
[builtins fixtures/dict.pyi]


[case testNarrowingTypeObjects]
from __future__ import annotations
from typing import Callable, Any, TypeVar, Generic, Protocol
_T_co = TypeVar('_T_co', covariant=True)

class Boxxy(Protocol[_T_co]):
def get_box(self) -> _T_co: ...

class TupleLike(Generic[_T_co]):
def __init__(self, iterable: Boxxy[_T_co], /) -> None:
raise

class Box1(Generic[_T_co]):
def __init__(self, content: _T_co, /) -> None: ...
def get_box(self) -> _T_co: raise

class Box2(Generic[_T_co]):
def __init__(self, content: _T_co, /) -> None: ...
def get_box(self) -> _T_co: raise

def get_type(setting_name: str) -> Callable[[Box1], Any] | type[Any]:
raise

def main(key: str):
existing_value_type = get_type(key)
if existing_value_type is TupleLike:
reveal_type(TupleLike) # N: Revealed type is "(def (__main__.Box1[Any]) -> Any) | type[Any]"
TupleLike(Box2("str")) # E: Argument 1 has incompatible type "Box2[str]"; expected "Box1[Any]"
[builtins fixtures/tuple.pyi]