Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable negative narrowing of Union TypeVar upper bounds (#17850)
Fixes #15235 ### Before ```python from typing import TypeVar class A: pass class B: b: int T = TypeVar("T", bound=A | B) def foo(x: T) -> T: if isinstance(x, A): reveal_type(x) # N: Revealed type is "__main__.A" else: reveal_type(x) # N: Revealed type is "T`-1" x.b # E: Item "A" of the upper bound "A | B" of type variable "T" has no attribute "b" return x ``` ### After ```python from typing import TypeVar class A: pass class B: b: int T = TypeVar("T", bound=A | B) def foo(x: T) -> T: if isinstance(x, A): reveal_type(x) # N: Revealed type is "__main__.A" else: reveal_type(x) # N: Revealed type is "T`-1" x.b # ok! Upper bound of T narrowed to B return x ```
- Loading branch information