Commit 848cb72
authored
[ty] Fix narrowing of nonlocal variables with conditional assignments (#22966)
## Summary
Given:
```python
def _(maybe_float: float | None, certain_int: int, flag: bool) -> None:
if isinstance(maybe_float, int):
return
x = maybe_float
def _() -> None:
nonlocal x
if flag:
x = certain_int
assert x is not None
+x # error: Unary operator `+` is not supported for operand of type `int | float | None`
```
With the guard (removed in this PR), `assert x is not None` would have
no effect, since `int | (float & ~int)` is not assignable to `(float &
~int) | None`.
Closes astral-sh/ty#2649.1 parent da7f33a commit 848cb72
2 files changed
Lines changed: 21 additions & 4 deletions
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
446 | 446 | | |
447 | 447 | | |
448 | 448 | | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11908 | 11908 | | |
11909 | 11909 | | |
11910 | 11910 | | |
11911 | | - | |
11912 | | - | |
11913 | | - | |
11914 | | - | |
| 11911 | + | |
11915 | 11912 | | |
11916 | 11913 | | |
11917 | 11914 | | |
| |||
0 commit comments