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 - Infer the return value of bool() #13538

Merged
merged 3 commits into from
Sep 27, 2024

Conversation

TomerBin
Copy link
Contributor

Summary

Following #13449, this PR adds custom handling for the bool contractor, 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.

x = bool(1)

Test Plan

Some cargo tests were added.

Copy link

codspeed-hq bot commented Sep 27, 2024

CodSpeed Performance Report

Merging #13538 will not alter performance

Comparing TomerBin:tomer/bool-function (4aabb57) with main (1639488)

Summary

✅ 32 untouched benchmarks

Copy link
Contributor

github-actions bot commented Sep 27, 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 Sep 27, 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 looks like the right behavior, but the implementation should go inside Type::call.

Comment on lines 2033 to 2034
return if function_type == builtins_symbol_ty(self.db, "bool") {
self.infer_bool_call_expression(arg_types.as_slice())
Copy link
Contributor

Choose a reason for hiding this comment

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

This should happen inside Type::call instead of here, and it should use FunctionType::is_stdlib_symbol instead of querying the builtin type and comparing for equality.

@carljm carljm merged commit ec72e67 into astral-sh:main Sep 27, 2024
20 checks passed
@TomerBin TomerBin deleted the tomer/bool-function branch September 27, 2024 19:18
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