Skip to content

Commit

Permalink
Do not use TypeGuard context for lambda (#11417)
Browse files Browse the repository at this point in the history
Closes #9927
  • Loading branch information
sobolevn authored Oct 31, 2021
1 parent 63c414a commit 68047f9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3560,6 +3560,12 @@ def infer_lambda_type_using_context(self, e: LambdaExpr) -> Tuple[Optional[Calla
callable_ctx = get_proper_type(replace_meta_vars(ctx, ErasedType()))
assert isinstance(callable_ctx, CallableType)

if callable_ctx.type_guard is not None:
# Lambda's return type cannot be treated as a `TypeGuard`,
# because it is implicit. And `TypeGuard`s must be explicit.
# See https://github.com/python/mypy/issues/9927
return None, None

arg_kinds = [arg.kind for arg in e.arguments]

if callable_ctx.is_ellipsis_args:
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-typeguard.test
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def main1(a: object) -> None:

[builtins fixtures/tuple.pyi]

[case testTypeGuardOverload-skip]
[case testTypeGuardOverload]
# flags: --strict-optional
from typing import overload, Any, Callable, Iterable, Iterator, List, Optional, TypeVar
from typing_extensions import TypeGuard
Expand Down

0 comments on commit 68047f9

Please sign in to comment.