Skip to content

Commit

Permalink
fix: TypeGuard becomes bool instead of Any when passed as TypeVar
Browse files Browse the repository at this point in the history
  • Loading branch information
sloboegen committed Apr 20, 2024
1 parent df35dcf commit bd485c2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,21 +1018,25 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
param_spec = template.param_spec()

template_ret_type, cactual_ret_type = template.ret_type, cactual.ret_type
bool_type = UnionType(
[LiteralType(True, cactual_ret_type), LiteralType(False, cactual_ret_type)] # type: ignore[arg-type]
)

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)
cactual_ret_type = bool_type

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)
cactual_ret_type = bool_type

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 bd485c2

Please sign in to comment.