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] Add Type::bool and boolean expression inference #13449

Merged
merged 14 commits into from
Sep 25, 2024

Conversation

TomerBin
Copy link
Contributor

Summary

Add boolean operator (and and or) support for several types. I'm pretty new to Rust so any feedback would be great.

A new Type::bool function is created which may also help in #13432 as well.
Contributes to #12701

Test Plan

Cargo tests

Copy link
Contributor

github-actions bot commented Sep 22, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you! Not a complete review (I'm on holiday at the moment), just a couple of things I spotted:

crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
@MichaReiser MichaReiser added the red-knot Multi-file analysis & type inference label Sep 23, 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.

Thank you, this is awesome! I think there's one bug, a couple tests that would be good to add, and I think we can also avoid allocating vectors in infer_boolean_expression.

crates/red_knot_python_semantic/src/types/infer.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types/infer.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types/infer.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types/infer.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types/infer.rs Outdated Show resolved Hide resolved
@TomerBin
Copy link
Contributor Author

Thanks for these great comments, I'll address them soon.

Something else I was thinking about is to use Type::bool() when inferring the return value of the bool function (ie. understanding x is True in x = bool(1))
It would also be beneficial for testing the bool utility.

What do you think about this? Should we do that? Maybe in another PR?

@carljm
Copy link
Contributor

carljm commented Sep 24, 2024

Something else I was thinking about is to use Type::bool() when inferring the return value of the bool function (ie. understanding x is True in x = bool(1))
It would also be beneficial for testing the bool utility.

What do you think about this? Should we do that? Maybe in another PR?

Yes, that's something we will want. Definitely recommend separate PR for this, as there will be some decisions to make there about how to detect that we are calling the builtin bool type.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I prefer to use ::from methods rather than .into() methods where possible, as it's much clearer when reading things outside of an IDE what exactly you're converting the type into

crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
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 looks great to me! Thank you!!

@TomerBin
Copy link
Contributor Author

Thanks again for your well explained and welcoming comments ☺️

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! :-D

@AlexWaygood AlexWaygood enabled auto-merge (squash) September 24, 2024 23:59
@AlexWaygood AlexWaygood changed the title red-knot: Add boolean operator [red-knot] Add Type::bool and boolean expression inference Sep 24, 2024
@AlexWaygood AlexWaygood merged commit be1d5e3 into astral-sh:main Sep 25, 2024
17 checks passed
carljm pushed a commit that referenced this pull request Sep 27, 2024
## Summary
Following #13449, this PR adds custom handling for the bool constructor,
so when the input type has statically known truthiness value, it will be
used as the return value of the bool function.
For example, in the following snippet x will now be resolved to
`Literal[True]` instead of `bool`.
```python
x = bool(1)
```

## Test Plan
Some cargo tests were added.
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.

4 participants