-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
red-knot: support narrowing for bool(E) (#14668)
Resolves #14547 by delegating narrowing to `E` for `bool(E)` where `E` is some expression. This change does not include other builtin class constructors which should also work in this position, like `int(..)` or `float(..)`, as the original issue does not mention these. It should be easy enough to add checks for these as well if we want to. I don't see a lot of markdown tests for malformed input, maybe there's a better place for the no args and too many args cases to go? I did see after the fact that it looks like this task was intended for a new hire.. my apologies. I got here from #13694, which is marked help-wanted. --------- Co-authored-by: David Peter <[email protected]>
- Loading branch information
1 parent
91e2d9a
commit 3e702e1
Showing
2 changed files
with
78 additions
and
34 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
crates/red_knot_python_semantic/resources/mdtest/narrow/bool-call.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
## Narrowing for `bool(..)` checks | ||
|
||
```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 not bool(): | ||
reveal_type(x) # revealed: Literal[1] | None | ||
|
||
# invalid invocation, too many positional args | ||
reveal_type(x) # revealed: Literal[1] | None | ||
if bool(x is not None, 5): # TODO diagnostic | ||
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): # TODO diagnostic | ||
reveal_type(x) # revealed: Literal[1] | None | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters