mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-19 20:24:27 +00:00
[ty] Propagate type context through conditional expressions (#21443)
## Summary Resolves https://github.com/astral-sh/ty/issues/1543.
This commit is contained in:
parent
0a55327d64
commit
ffb7bdd595
4 changed files with 30 additions and 7 deletions
|
|
@ -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]
|
||||
```
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue