[ty] Better control flow for boolean expressions that are inside if#18010
Conversation
|
6859200 to
adc3f1f
Compare
|
Just adding a note that it looks like this fixes astral-sh/ty#139 |
It's fantastic that you found a way to make this work, and I'm looking forward to reviewing this! I'll just add another note here that merging this PR is not an immediate priority (anymore) because we recently decided to disable |
carljm
left a comment
There was a problem hiding this comment.
This looks really good! Thank you for the contribution. I will push a small commit with a few renaming adjustments and the TODO comments I mentioned inline, and then merge.
crates/ty_python_semantic/resources/mdtest/boolean/short_circuit.md
Outdated
Show resolved
Hide resolved
2ea02c0 to
2b59009
Compare
## Summary just a minor nit followup to #18010 -- put all the non-`Visitor` methods of `SemanticIndexBuilder` in the same impl block rather than having multiple impl blocks ## Test Plan `cargo build`
…ide if (#18010)" (#18150) This reverts commit 9910ec7. ## Summary This change introduced a serious performance regression. Revert it while we investigate. Fixes astral-sh/ty#431 ## Test Plan Timing on the snippet in astral-sh/ty#431 again shows times similar to before the regression.
…stral-sh#18010) ## Summary With this PR we now detect that x is always defined in `use`: ```py if flag and (x := number): use(x) ``` When outside if, it's still detected as possibly not defined ```py flag and (x := number) # error: [possibly-unresolved-reference] use(x) ``` In order to achieve that, I had to find a way to get access to the flow-snapshots of the boolean expression when analyzing the flow of the if statement. I did it by special casing the visitor of boolean expression to return flow control information, exporting two snapshots - `maybe_short_circuit` and `no_short_circuit`. When indexing boolean expression itself we must assume all possible flows, but when it's inside if statement, we can be smarter than that. ## Test Plan Fixed existing and added new mdtests. I went through some of mypy primer results and they look fine --------- Co-authored-by: Carl Meyer <carl@astral.sh>
## Summary just a minor nit followup to astral-sh#18010 -- put all the non-`Visitor` methods of `SemanticIndexBuilder` in the same impl block rather than having multiple impl blocks ## Test Plan `cargo build`
Summary
With this PR we now detect that x is always defined in
use:When outside if, it's still detected as possibly not defined
In order to achieve that, I had to find a way to get access to the flow-snapshots of the boolean expression when analyzing the flow of the if statement. I did it by special casing the visitor of boolean expression to return flow control information, exporting two snapshots -
might_short_circuitedandnever_short_circuited. When indexing boolean expression itself we must assume all possible flows, but when it's inside if statement, we can be smarter than that.This PR is still kind of draft, but I'd like to get your overall opinion so opening it for review :)
Hope you'll get time to review this before the alpha release :)
Best reviewed by commits
Test Plan
Fixed existing and added new mdtests.
I went through some of mypy primer results and they look fine