Skip to content

Commit 87437b8

Browse files
Add incremental tests for TypeGuard/TypeIs (#16976)
1 parent bcb3747 commit 87437b8

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

test-data/unit/check-incremental.test

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6574,3 +6574,79 @@ class TheClass:
65746574
[out]
65756575
[out2]
65766576
tmp/a.py:3: note: Revealed type is "def (value: builtins.object) -> lib.TheClass.pyenum@6"
6577+
6578+
[case testStartUsingTypeGuard]
6579+
import a
6580+
[file a.py]
6581+
from lib import guard
6582+
from typing import Union
6583+
from typing_extensions import assert_type
6584+
x: Union[int, str]
6585+
6586+
[file a.py.2]
6587+
from lib import guard
6588+
from typing import Union
6589+
from typing_extensions import assert_type
6590+
x: Union[int, str]
6591+
if guard(x):
6592+
assert_type(x, int)
6593+
else:
6594+
assert_type(x, Union[int, str])
6595+
[file lib.py]
6596+
from typing_extensions import TypeGuard
6597+
def guard(x: object) -> TypeGuard[int]:
6598+
pass
6599+
[builtins fixtures/tuple.pyi]
6600+
6601+
[case testStartUsingTypeIs]
6602+
import a
6603+
[file a.py]
6604+
from lib import guard
6605+
from typing import Union
6606+
from typing_extensions import assert_type
6607+
x: Union[int, str]
6608+
6609+
[file a.py.2]
6610+
from lib import guard
6611+
from typing import Union
6612+
from typing_extensions import assert_type
6613+
x: Union[int, str]
6614+
if guard(x):
6615+
assert_type(x, int)
6616+
else:
6617+
assert_type(x, str)
6618+
[file lib.py]
6619+
from typing_extensions import TypeIs
6620+
def guard(x: object) -> TypeIs[int]:
6621+
pass
6622+
[builtins fixtures/tuple.pyi]
6623+
6624+
[case testTypeGuardToTypeIs]
6625+
import a
6626+
[file a.py]
6627+
from lib import guard
6628+
from typing import Union
6629+
from typing_extensions import assert_type
6630+
x: Union[int, str]
6631+
if guard(x):
6632+
assert_type(x, int)
6633+
else:
6634+
assert_type(x, Union[int, str])
6635+
[file a.py.2]
6636+
from lib import guard
6637+
from typing import Union
6638+
from typing_extensions import assert_type
6639+
x: Union[int, str]
6640+
if guard(x):
6641+
assert_type(x, int)
6642+
else:
6643+
assert_type(x, str)
6644+
[file lib.py]
6645+
from typing_extensions import TypeGuard
6646+
def guard(x: object) -> TypeGuard[int]:
6647+
pass
6648+
[file lib.py.2]
6649+
from typing_extensions import TypeIs
6650+
def guard(x: object) -> TypeIs[int]:
6651+
pass
6652+
[builtins fixtures/tuple.pyi]

test-data/unit/lib-stub/typing_extensions.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ class _TypedDict(Mapping[str, object]):
6868

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

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

7374
def dataclass_transform(
7475
*,
@@ -77,7 +78,7 @@ def dataclass_transform(
7778
kw_only_default: bool = ...,
7879
field_specifiers: tuple[type[Any] | Callable[..., Any], ...] = ...,
7980
**kwargs: Any,
80-
) -> Callable[[T], T]: ...
81+
) -> Callable[[_T], _T]: ...
8182

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

0 commit comments

Comments
 (0)