Skip to content

Commit

Permalink
TypeGuard should be bool not Any when matching TypeVar (python#17145)
Browse files Browse the repository at this point in the history
  • Loading branch information
sloboegen authored Oct 18, 2024
1 parent 123eb91 commit 5d9b1f9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
8 changes: 0 additions & 8 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,18 +1029,10 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
if template.type_guard is not None and cactual.type_guard is not None:
template_ret_type = template.type_guard
cactual_ret_type = cactual.type_guard
elif template.type_guard is not None:
template_ret_type = AnyType(TypeOfAny.special_form)
elif cactual.type_guard is not None:
cactual_ret_type = AnyType(TypeOfAny.special_form)

if template.type_is is not None and cactual.type_is is not None:
template_ret_type = template.type_is
cactual_ret_type = cactual.type_is
elif template.type_is is not None:
template_ret_type = AnyType(TypeOfAny.special_form)
elif cactual.type_is is not None:
cactual_ret_type = AnyType(TypeOfAny.special_form)

res.extend(infer_constraints(template_ret_type, cactual_ret_type, self.direction))

Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-typeguard.test
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ def main(a: Tuple[T, ...]):
reveal_type(a) # N: Revealed type is "Tuple[T`-1, T`-1]"
[builtins fixtures/tuple.pyi]

[case testTypeGuardPassedAsTypeVarIsBool]
from typing import Callable, TypeVar
from typing_extensions import TypeGuard
T = TypeVar('T')
def is_str(x: object) -> TypeGuard[str]: ...
def main(f: Callable[[object], T]) -> T: ...
reveal_type(main(is_str)) # N: Revealed type is "builtins.bool"
[builtins fixtures/tuple.pyi]

[case testTypeGuardNonOverlapping]
from typing import List
from typing_extensions import TypeGuard
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-typeis.test
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ def main(x: object, type_check_func: Callable[[object], TypeIs[T]]) -> T:
reveal_type(main("a", is_str)) # N: Revealed type is "builtins.str"
[builtins fixtures/exception.pyi]

[case testTypeIsPassedAsTypeVarIsBool]
from typing import Callable, TypeVar
from typing_extensions import TypeIs
T = TypeVar('T')
def is_str(x: object) -> TypeIs[str]: pass
def main(f: Callable[[object], T]) -> T: pass
reveal_type(main(is_str)) # N: Revealed type is "builtins.bool"
[builtins fixtures/tuple.pyi]

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

0 comments on commit 5d9b1f9

Please sign in to comment.