Skip to content

Commit

Permalink
Add incremental tests for TypeGuard/TypeIs (python#16976)
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra authored Mar 1, 2024
1 parent bcb3747 commit 87437b8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
76 changes: 76 additions & 0 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -6574,3 +6574,79 @@ class TheClass:
[out]
[out2]
tmp/a.py:3: note: Revealed type is "def (value: builtins.object) -> lib.TheClass.pyenum@6"

[case testStartUsingTypeGuard]
import a
[file a.py]
from lib import guard
from typing import Union
from typing_extensions import assert_type
x: Union[int, str]

[file a.py.2]
from lib import guard
from typing import Union
from typing_extensions import assert_type
x: Union[int, str]
if guard(x):
assert_type(x, int)
else:
assert_type(x, Union[int, str])
[file lib.py]
from typing_extensions import TypeGuard
def guard(x: object) -> TypeGuard[int]:
pass
[builtins fixtures/tuple.pyi]

[case testStartUsingTypeIs]
import a
[file a.py]
from lib import guard
from typing import Union
from typing_extensions import assert_type
x: Union[int, str]

[file a.py.2]
from lib import guard
from typing import Union
from typing_extensions import assert_type
x: Union[int, str]
if guard(x):
assert_type(x, int)
else:
assert_type(x, str)
[file lib.py]
from typing_extensions import TypeIs
def guard(x: object) -> TypeIs[int]:
pass
[builtins fixtures/tuple.pyi]

[case testTypeGuardToTypeIs]
import a
[file a.py]
from lib import guard
from typing import Union
from typing_extensions import assert_type
x: Union[int, str]
if guard(x):
assert_type(x, int)
else:
assert_type(x, Union[int, str])
[file a.py.2]
from lib import guard
from typing import Union
from typing_extensions import assert_type
x: Union[int, str]
if guard(x):
assert_type(x, int)
else:
assert_type(x, str)
[file lib.py]
from typing_extensions import TypeGuard
def guard(x: object) -> TypeGuard[int]:
pass
[file lib.py.2]
from typing_extensions import TypeIs
def guard(x: object) -> TypeIs[int]:
pass
[builtins fixtures/tuple.pyi]
5 changes: 3 additions & 2 deletions test-data/unit/lib-stub/typing_extensions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class _TypedDict(Mapping[str, object]):

def TypedDict(typename: str, fields: Dict[str, Type[_T]], *, total: Any = ...) -> Type[dict]: ...

def reveal_type(__obj: T) -> T: pass
def reveal_type(__obj: _T) -> _T: pass
def assert_type(__val: _T, __typ: Any) -> _T: pass

def dataclass_transform(
*,
Expand All @@ -77,7 +78,7 @@ def dataclass_transform(
kw_only_default: bool = ...,
field_specifiers: tuple[type[Any] | Callable[..., Any], ...] = ...,
**kwargs: Any,
) -> Callable[[T], T]: ...
) -> Callable[[_T], _T]: ...

def override(__arg: _T) -> _T: ...
def deprecated(__msg: str) -> Callable[[_T], _T]: ...
Expand Down

0 comments on commit 87437b8

Please sign in to comment.