Skip to content

Add incremental tests for TypeGuard/TypeIs #16976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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