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: support narrowing for bool(E) #14668

Merged
merged 7 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,36 @@ if x is None:
if y is not x:
reveal_type(y) # revealed: str | None
```

## `bool(..)` builtin containing expression narrows for expression
carljm marked this conversation as resolved.
Show resolved Hide resolved

```py
def flag() -> bool: ...

x = 1 if flag() else None

# valid invocation, positive
reveal_type(x) # revealed: Literal[1] | None
if bool(x is not None):
reveal_type(x) # revealed: Literal[1]

# valid invocation, negative
reveal_type(x) # revealed: Literal[1] | None
if not bool(x is not None):
reveal_type(x) # revealed: None

# no args/narrowing
reveal_type(x) # revealed: Literal[1] | None
if bool():
reveal_type(x) # revealed: Literal[1] | None
carljm marked this conversation as resolved.
Show resolved Hide resolved

# invalid invocation, too many positional args
reveal_type(x) # revealed: Literal[1] | None
if bool(x is not None, 5):
reveal_type(x) # revealed: Literal[1] | None

# invalid invocation, too many kwargs
reveal_type(x) # revealed: Literal[1] | None
if bool(x is not None, y=5):
reveal_type(x) # revealed: Literal[1] | None
```
24 changes: 21 additions & 3 deletions crates/red_knot_python_semantic/src/types/narrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,11 @@ impl<'db> NarrowingConstraintsBuilder<'db> {
let scope = self.scope();
let inference = infer_expression_types(self.db, expression);

let expr_ty = inference.expression_ty(expr_call.func.scoped_expression_id(self.db, scope));
sharkdp marked this conversation as resolved.
Show resolved Hide resolved

// TODO: add support for PEP 604 union types on the right hand side of `isinstance`
// and `issubclass`, for example `isinstance(x, str | (int | float))`.
match inference
.expression_ty(expr_call.func.scoped_expression_id(self.db, scope))
match expr_ty
.into_function_literal()
.and_then(|f| f.known(self.db))
.and_then(KnownFunction::constraint_function)
Expand Down Expand Up @@ -426,7 +427,24 @@ impl<'db> NarrowingConstraintsBuilder<'db> {
None
}
}
_ => None,
_ => {
carljm marked this conversation as resolved.
Show resolved Hide resolved
let is_valid_bool_invocation =
expr_call.arguments.args.len() == 1 && expr_call.arguments.keywords.is_empty();

if is_valid_bool_invocation
&& expr_ty
.into_class_literal()
.is_some_and(|lit| lit.class.is_known(self.db, KnownClass::Bool))
{
self.evaluate_expression_node_constraint(
&expr_call.arguments.args[0],
expression,
is_positive,
)
} else {
None
}
}
}
}

Expand Down