Skip to content
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

Merged

Conversation

TomerBin
Copy link
Contributor

@TomerBin TomerBin commented Nov 1, 2024

Summary

This PR enables red-knot to support type narrowing based on and and or conditionals, including nested combinations and their negation (for elif / else blocks and for not 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 mutating self.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!

@TomerBin TomerBin marked this pull request as ready for review November 1, 2024 11:29
Copy link
Contributor

github-actions bot commented Nov 1, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser MichaReiser added the red-knot Multi-file analysis & type inference label Nov 1, 2024
Copy link
Contributor

@carljm carljm left a 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!

@TomerBin
Copy link
Contributor Author

TomerBin commented Nov 1, 2024

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!

Thanks for the review! I'll address all comments soon

@TomerBin TomerBin force-pushed the tomer/type-narrow-boolean-conditionals branch 2 times, most recently from bc1078e to 06089fb Compare November 3, 2024 15:55
@TomerBin
Copy link
Contributor Author

TomerBin commented Nov 3, 2024

I think i've addressed all comments, it is ready for another review now :)

Copy link
Contributor

@carljm carljm left a 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.

crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
@TomerBin TomerBin force-pushed the tomer/type-narrow-boolean-conditionals branch from 06089fb to 1fd2402 Compare November 4, 2024 22:49
@carljm carljm enabled auto-merge (squash) November 4, 2024 22:53
@carljm carljm merged commit 6c56a7a into astral-sh:main Nov 4, 2024
18 checks passed
carljm added a commit to Glyphack/ruff that referenced this pull request Nov 5, 2024
* main:
  [red-knot] Implement type narrowing for boolean conditionals (astral-sh#14037)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants