Skip to content

Red Knot - Infer the return value of bool() #13538

Merged
carljm merged 3 commits intoastral-sh:mainfrom
TomerBin:tomer/bool-function
Sep 27, 2024
Merged

Red Knot - Infer the return value of bool() #13538
carljm merged 3 commits intoastral-sh:mainfrom
TomerBin:tomer/bool-function

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.

@codspeed-hq
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

@github-actions
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 ty 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
@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

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants