-
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 for not-equal narrowing (#13749)
Add type narrowing for `!=` expression as stated in #13694. ### Test Plan Add tests in new md format. --------- Co-authored-by: David Peter <[email protected]>
- Loading branch information
Showing
4 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
crates/red_knot_python_semantic/resources/mdtest/narrow/conditionals_nested.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,13 @@ | ||
# Narrowing for nested conditionals | ||
|
||
```py | ||
def int_instance() -> int: ... | ||
|
||
|
||
x = int_instance() | ||
|
||
if x != 1: | ||
if x != 2: | ||
if x != 3: | ||
reveal_type(x) # revealed: int & ~Literal[1] & ~Literal[2] & ~Literal[3] | ||
``` |
58 changes: 58 additions & 0 deletions
58
crates/red_knot_python_semantic/resources/mdtest/narrow/conditionals_not_eq.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,58 @@ | ||
# Narrowing for `!=` conditionals | ||
|
||
## `x != None` | ||
|
||
```py | ||
x = None if flag else 1 | ||
|
||
if x != None: | ||
reveal_type(x) # revealed: Literal[1] | ||
``` | ||
|
||
## `!=` for other singleton types | ||
|
||
```py | ||
x = True if flag else False | ||
|
||
if x != False: | ||
reveal_type(x) # revealed: Literal[True] | ||
``` | ||
|
||
## `x != y` where `y` is of literal type | ||
|
||
```py | ||
x = 1 if flag else 2 | ||
|
||
if x != 1: | ||
reveal_type(x) # revealed: Literal[2] | ||
``` | ||
|
||
## `x != y` where `y` is a single-valued type | ||
|
||
```py | ||
class A: ... | ||
|
||
|
||
class B: ... | ||
|
||
|
||
C = A if flag else B | ||
|
||
if C != A: | ||
reveal_type(C) # revealed: Literal[B] | ||
``` | ||
|
||
## `!=` for non-single-valued types | ||
|
||
Only single-valued types should narrow the type: | ||
|
||
```py | ||
def int_instance() -> int: ... | ||
|
||
|
||
x = int_instance() if flag else None | ||
y = int_instance() | ||
|
||
if x != y: | ||
reveal_type(x) # revealed: int | 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
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