Skip to content

Commit 796b51d

Browse files
committed
fix: TypeGuard becomes bool instead of Any when passed as TypeVar
1 parent 82ebd86 commit 796b51d

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
@@ -98,6 +98,15 @@ def main(a: Tuple[T, ...]):
9898
reveal_type(a) # N: Revealed type is "Tuple[T`-1, T`-1]"
9999
[builtins fixtures/tuple.pyi]
100100

101+
[case testTypeGuardPassedAsTypeVarIsBool]
102+
from typing import Callable, TypeVar
103+
from typing_extensions import TypeGuard
104+
T = TypeVar('T')
105+
def is_str(x: object) -> TypeGuard[str]: ...
106+
def main(f: Callable[[object], T]) -> T: ...
107+
reveal_type(main(is_str)) # N: Revealed type is "builtins.bool"
108+
[builtins fixtures/tuple.pyi]
109+
101110
[case testTypeGuardNonOverlapping]
102111
from typing import List
103112
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
@@ -115,6 +115,15 @@ def main(x: object, type_check_func: Callable[[object], TypeIs[T]]) -> T:
115115
reveal_type(main("a", is_str)) # N: Revealed type is "builtins.str"
116116
[builtins fixtures/exception.pyi]
117117

118+
[case testTypeIsPassedAsTypeVarIsBool]
119+
from typing import Callable, TypeVar
120+
from typing_extensions import TypeIs
121+
T = TypeVar('T')
122+
def is_str(x: object) -> TypeIs[str]: pass
123+
def main(f: Callable[[object], T]) -> T: pass
124+
reveal_type(main(is_str)) # N: Revealed type is "builtins.bool"
125+
[builtins fixtures/tuple.pyi]
126+
118127
[case testTypeIsUnionIn]
119128
from typing import Union
120129
from typing_extensions import TypeIs

0 commit comments

Comments
 (0)