-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[red-knot] Implement type narrowing for boolean conditionals #14037
[red-knot] Implement type narrowing for boolean conditionals #14037
Conversation
|
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.
This is fantastic work!
I do have a few comments: feel free to push back if any of my comments don't seem right, it's quite possible I've missed something important!
crates/red_knot_python_semantic/resources/mdtest/narrow/conditionals_boolean.md
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/narrow/conditionals_boolean.md
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/narrow/conditionals_boolean.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/narrow/conditionals_boolean.md
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/narrow/conditionals_boolean.md
Outdated
Show resolved
Hide resolved
Thanks for the review! I'll address all comments soon |
bc1078e
to
06089fb
Compare
I think i've addressed all comments, it is ready for another review now :) |
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.
This is awesome!! One nit on one test, then it's good to go.
…ionals_boolean.md Co-authored-by: Carl Meyer <[email protected]>
06089fb
to
1fd2402
Compare
* main: [red-knot] Implement type narrowing for boolean conditionals (astral-sh#14037)
Summary
This PR enables red-knot to support type narrowing based on
and
andor
conditionals, including nested combinations and their negation (forelif
/else
blocks and fornot
operator). Part of #13694.In order to address this properly (hopefully 😅), I had to run
NarrowingConstraintsBuilder
functions recursively. In the first commit I introduced a minor refactor - instead of mutatingself.constraints
, the new constraints are now returned as function return values. I also modified the constraints map to be optional, preventing unnecessary hashmap allocations.Thanks @carljm for your support on this :)
The second commit contains the logic and tests for handling boolean ops, with improvements to intersections handling in
is_subtype_of
.As I'm still new to Rust and the internals of type checkers, I’d be more than happy to hear any insights or suggestions.
Thank you!