mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 10:48:32 +00:00
[ty] Eagerly simplify 'True' and 'False' constraints (#18998)
## Summary Simplifies literal `True` and `False` conditions to `ALWAYS_TRUE` / `ALWAYS_FALSE` during semantic index building. This allows us to eagerly evaluate more constraints, which should help with performance (looks like there is a tiny 1% improvement in instrumented benchmarks), but also allows us to eliminate definitely-unreachable branches in control-flow merging. This can lead to better type inference in some cases because it allows us to retain narrowing constraints without solving https://github.com/astral-sh/ty/issues/690 first: ```py def _(c: int | None): if c is None: assert False reveal_type(c) # int, previously: int | None ``` closes https://github.com/astral-sh/ty/issues/713 ## Test Plan * Regression test for https://github.com/astral-sh/ty/issues/713 * Made sure that all ecosystem diffs trace back to removed false positives
This commit is contained in:
parent
54769ac9f9
commit
db3dcd8ad6
6 changed files with 129 additions and 43 deletions
|
@ -59,3 +59,17 @@ while x != 1:
|
|||
|
||||
x = next_item()
|
||||
```
|
||||
|
||||
## With `break` statements
|
||||
|
||||
```py
|
||||
def next_item() -> int | None:
|
||||
return 1
|
||||
|
||||
while True:
|
||||
x = next_item()
|
||||
if x is not None:
|
||||
break
|
||||
|
||||
reveal_type(x) # revealed: int
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue