-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for type guard forms
x is ...
, x is not ...
, `x == …
…...` and `x != ...`. Support for these were recently added to mypy. This addresses #4397.
- Loading branch information
1 parent
7be1d77
commit c762694
Showing
4 changed files
with
104 additions
and
0 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
37 changes: 37 additions & 0 deletions
37
packages/pyright-internal/src/tests/samples/typeNarrowingIsEllipsis1.py
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,37 @@ | ||
# This sample tests the type analyzer's type narrowing logic for | ||
# conditions of the form "X is ...", "X is not ...", | ||
# "X == .." and "X != ...". | ||
|
||
import types | ||
from typing import TypeVar | ||
|
||
|
||
_T = TypeVar("_T", str, ellipsis) | ||
|
||
|
||
def func1(val: int | ellipsis): | ||
if val is not ...: | ||
reveal_type(val, expected_text="int") | ||
else: | ||
reveal_type(val, expected_text="ellipsis") | ||
|
||
|
||
def func2(val: _T): | ||
if val is ...: | ||
reveal_type(val, expected_text="ellipsis*") | ||
else: | ||
reveal_type(val, expected_text="str*") | ||
|
||
|
||
def func3(val: int | types.EllipsisType): | ||
if val != ...: | ||
reveal_type(val, expected_text="int") | ||
else: | ||
reveal_type(val, expected_text="ellipsis") | ||
|
||
|
||
def func4(val: int | ellipsis): | ||
if not val == ...: | ||
reveal_type(val, expected_text="int") | ||
else: | ||
reveal_type(val, expected_text="ellipsis") |
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