Skip to content
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

Add TypeGuard and TypeIs traversing in TypeTraverserVisitor (fixes #17029) #17071

Merged
merged 2 commits into from
Mar 29, 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
6 changes: 6 additions & 0 deletions mypy/typetraverser.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ def visit_callable_type(self, t: CallableType) -> None:
t.ret_type.accept(self)
t.fallback.accept(self)

if t.type_guard is not None:
t.type_guard.accept(self)
Comment on lines +89 to +90
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an issue for TypeIs as well? Probably as easy as adding an if t.type_is is not None clause here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please add an analogous test for TypeIs as well. It should behave exactly the same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks! It was interesting to learn about TypeIs :)


if t.type_is is not None:
t.type_is.accept(self)

def visit_tuple_type(self, t: TupleType) -> None:
self.traverse_types(t.items)
t.partial_fallback.accept(self)
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-typeguard.test
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ def main(a: object, b: object) -> None:
reveal_type(b) # N: Revealed type is "builtins.object"
[builtins fixtures/tuple.pyi]

[case testTypeGuardTypeVarReturn]
from typing import Callable, Optional, TypeVar
from typing_extensions import TypeGuard
T = TypeVar('T')
def is_str(x: object) -> TypeGuard[str]: pass
def main(x: object, type_check_func: Callable[[object], TypeGuard[T]]) -> T:
if not type_check_func(x):
raise Exception()
return x
reveal_type(main("a", is_str)) # N: Revealed type is "builtins.str"
[builtins fixtures/exception.pyi]

[case testTypeGuardIsBool]
from typing_extensions import TypeGuard
def f(a: TypeGuard[int]) -> None: pass
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-typeis.test
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ def main(a: Tuple[object, ...]):
reveal_type(a) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
[builtins fixtures/tuple.pyi]

[case testTypeIsTypeVarReturn]
from typing import Callable, Optional, TypeVar
from typing_extensions import TypeIs
T = TypeVar('T')
def is_str(x: object) -> TypeIs[str]: pass
def main(x: object, type_check_func: Callable[[object], TypeIs[T]]) -> T:
if not type_check_func(x):
raise Exception()
return x
reveal_type(main("a", is_str)) # N: Revealed type is "builtins.str"
[builtins fixtures/exception.pyi]

[case testTypeIsUnionIn]
from typing import Union
from typing_extensions import TypeIs
Expand Down
Loading