Skip to content

Commit 5d9b1f9

Browse files
authored
TypeGuard should be bool not Any when matching TypeVar (#17145)
Fixes #17117
1 parent 123eb91 commit 5d9b1f9

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

mypy/constraints.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,18 +1029,10 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
10291029
if template.type_guard is not None and cactual.type_guard is not None:
10301030
template_ret_type = template.type_guard
10311031
cactual_ret_type = cactual.type_guard
1032-
elif template.type_guard is not None:
1033-
template_ret_type = AnyType(TypeOfAny.special_form)
1034-
elif cactual.type_guard is not None:
1035-
cactual_ret_type = AnyType(TypeOfAny.special_form)
10361032

10371033
if template.type_is is not None and cactual.type_is is not None:
10381034
template_ret_type = template.type_is
10391035
cactual_ret_type = cactual.type_is
1040-
elif template.type_is is not None:
1041-
template_ret_type = AnyType(TypeOfAny.special_form)
1042-
elif cactual.type_is is not None:
1043-
cactual_ret_type = AnyType(TypeOfAny.special_form)
10441036

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

test-data/unit/check-typeguard.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ def main(a: Tuple[T, ...]):
8787
reveal_type(a) # N: Revealed type is "Tuple[T`-1, T`-1]"
8888
[builtins fixtures/tuple.pyi]
8989

90+
[case testTypeGuardPassedAsTypeVarIsBool]
91+
from typing import Callable, TypeVar
92+
from typing_extensions import TypeGuard
93+
T = TypeVar('T')
94+
def is_str(x: object) -> TypeGuard[str]: ...
95+
def main(f: Callable[[object], T]) -> T: ...
96+
reveal_type(main(is_str)) # N: Revealed type is "builtins.bool"
97+
[builtins fixtures/tuple.pyi]
98+
9099
[case testTypeGuardNonOverlapping]
91100
from typing import List
92101
from typing_extensions import TypeGuard

test-data/unit/check-typeis.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ def main(x: object, type_check_func: Callable[[object], TypeIs[T]]) -> T:
104104
reveal_type(main("a", is_str)) # N: Revealed type is "builtins.str"
105105
[builtins fixtures/exception.pyi]
106106

107+
[case testTypeIsPassedAsTypeVarIsBool]
108+
from typing import Callable, TypeVar
109+
from typing_extensions import TypeIs
110+
T = TypeVar('T')
111+
def is_str(x: object) -> TypeIs[str]: pass
112+
def main(f: Callable[[object], T]) -> T: pass
113+
reveal_type(main(is_str)) # N: Revealed type is "builtins.bool"
114+
[builtins fixtures/tuple.pyi]
115+
107116
[case testTypeIsUnionIn]
108117
from typing import Union
109118
from typing_extensions import TypeIs

0 commit comments

Comments
 (0)