-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[red-knot] Type narrow in else clause (#13918)
## Summary Add support for type narrowing in elif and else scopes as part of #13694. ## Test Plan - mdtest - builder unit test for union negation. --------- Co-authored-by: Carl Meyer <[email protected]>
- Loading branch information
Showing
17 changed files
with
363 additions
and
53 deletions.
There are no files selected for viewing
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
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
57 changes: 57 additions & 0 deletions
57
crates/red_knot_python_semantic/resources/mdtest/narrow/conditionals_elif_else.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,57 @@ | ||
# Narrowing for conditionals with elif and else | ||
|
||
## Positive contributions become negative in elif-else blocks | ||
|
||
```py | ||
def int_instance() -> int: | ||
return 42 | ||
|
||
x = int_instance() | ||
|
||
if x == 1: | ||
# cannot narrow; could be a subclass of `int` | ||
reveal_type(x) # revealed: int | ||
elif x == 2: | ||
reveal_type(x) # revealed: int & ~Literal[1] | ||
elif x != 3: | ||
reveal_type(x) # revealed: int & ~Literal[1] & ~Literal[2] & ~Literal[3] | ||
``` | ||
|
||
## Positive contributions become negative in elif-else blocks, with simplification | ||
|
||
```py | ||
def bool_instance() -> bool: | ||
return True | ||
|
||
x = 1 if bool_instance() else 2 if bool_instance() else 3 | ||
|
||
if x == 1: | ||
# TODO should be Literal[1] | ||
reveal_type(x) # revealed: Literal[1, 2, 3] | ||
elif x == 2: | ||
# TODO should be Literal[2] | ||
reveal_type(x) # revealed: Literal[2, 3] | ||
else: | ||
reveal_type(x) # revealed: Literal[3] | ||
``` | ||
|
||
## Multiple negative contributions using elif, with simplification | ||
|
||
```py | ||
def bool_instance() -> bool: | ||
return True | ||
|
||
x = 1 if bool_instance() else 2 if bool_instance() else 3 | ||
|
||
if x != 1: | ||
reveal_type(x) # revealed: Literal[2, 3] | ||
elif x != 2: | ||
# TODO should be `Literal[1]` | ||
reveal_type(x) # revealed: Literal[1, 3] | ||
elif x == 3: | ||
# TODO should be Never | ||
reveal_type(x) # revealed: Literal[1, 2, 3] | ||
else: | ||
# TODO should be Never | ||
reveal_type(x) # revealed: Literal[1, 2] | ||
``` |
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
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
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
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
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
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
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
Oops, something went wrong.