Skip to content

Commit bd485c2

Browse files
committed
fix: TypeGuard becomes bool instead of Any when passed as TypeVar
1 parent df35dcf commit bd485c2

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

mypy/constraints.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,21 +1018,25 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
10181018
param_spec = template.param_spec()
10191019

10201020
template_ret_type, cactual_ret_type = template.ret_type, cactual.ret_type
1021+
bool_type = UnionType(
1022+
[LiteralType(True, cactual_ret_type), LiteralType(False, cactual_ret_type)] # type: ignore[arg-type]
1023+
)
1024+
10211025
if template.type_guard is not None and cactual.type_guard is not None:
10221026
template_ret_type = template.type_guard
10231027
cactual_ret_type = cactual.type_guard
10241028
elif template.type_guard is not None:
10251029
template_ret_type = AnyType(TypeOfAny.special_form)
10261030
elif cactual.type_guard is not None:
1027-
cactual_ret_type = AnyType(TypeOfAny.special_form)
1031+
cactual_ret_type = bool_type
10281032

10291033
if template.type_is is not None and cactual.type_is is not None:
10301034
template_ret_type = template.type_is
10311035
cactual_ret_type = cactual.type_is
10321036
elif template.type_is is not None:
10331037
template_ret_type = AnyType(TypeOfAny.special_form)
10341038
elif cactual.type_is is not None:
1035-
cactual_ret_type = AnyType(TypeOfAny.special_form)
1039+
cactual_ret_type = bool_type
10361040

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

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)