-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[ty] Eagerly simplify 'True' and 'False' constraints #18998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
f57163a
to
698f9a2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice!
Theoretically we could also simplify constraints such as not True
not False
, or not not True
, etc. But it probably wouldn't be worth the added complexity -- I don't know why you'd write code like that?! I can find a few examples on grep.app, but it doesn't seem like a useful pattern...
Agreed. I also grepped for some common patterns of definitely-true/false conditions and the only other thing that seemed frequently used and feasible to unambiguously detect in semantic index building was the Another thing that could be very valuable for performance would be to detect certain frequently used patterns that will definitely lead to an ambiguous truthiness result later. But I'm not sure what kind of patterns that might be... |
Yeah, I think that could be worth doing! |
## Summary Evaluate `TYPE_CHECKING` to `ALWAYS_TRUE` and `not TYPE_CHECKING` to `ALWAYS_FALSE` during semantic index building. This is a follow-up to #18998 and is in principle just a performance optimization. We see some (favorable) ecosystem changes because we can eliminate definitely-unreachable branches early now and retain narrowing constraints without solving astral-sh/ty#690 first.
## Summary Evaluate `TYPE_CHECKING` to `ALWAYS_TRUE` and `not TYPE_CHECKING` to `ALWAYS_FALSE` during semantic index building. This is a follow-up to astral-sh#18998 and is in principle just a performance optimization. We see some (favorable) ecosystem changes because we can eliminate definitely-unreachable branches early now and retain narrowing constraints without solving astral-sh/ty#690 first.
Summary
Simplifies literal
True
andFalse
conditions toALWAYS_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 astral-sh/ty#690 first:closes astral-sh/ty#713
Test Plan