[ty] Propagate type context through conditional expressions (#21443)

## Summary

Resolves https://github.com/astral-sh/ty/issues/1543.
This commit is contained in:
Ibraheem Ahmed 2025-11-14 15:19:08 -05:00 committed by GitHub
parent 0a55327d64
commit ffb7bdd595
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 30 additions and 7 deletions

View file

@ -341,8 +341,6 @@ d: X[Any] = X(1)
reveal_type(d) # revealed: X[Any]
def _(flag: bool):
# TODO: Handle unions correctly.
# error: [invalid-assignment] "Object of type `X[int]` is not assignable to `X[int | None]`"
a: X[int | None] = X(1) if flag else X(2)
reveal_type(a) # revealed: X[int | None]
```

View file

@ -281,6 +281,27 @@ A(f(1))
A(f([]))
```
## Conditional expressions
```toml
[environment]
python-version = "3.12"
```
The type context is propagated through both branches of conditional expressions:
```py
def f[T](x: T) -> list[T]:
raise NotImplementedError
def _(flag: bool):
x1 = f(1) if flag else f(2)
reveal_type(x1) # revealed: list[Literal[1]] | list[Literal[2]]
x2: list[int | None] = f(1) if flag else f(2)
reveal_type(x2) # revealed: list[int | None]
```
## Multi-inference diagnostics
```toml