Skip to content

Commit 68047f9

Browse files
authored
Do not use TypeGuard context for lambda (#11417)
Closes #9927
1 parent 63c414a commit 68047f9

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

mypy/checkexpr.py

+6
Original file line numberDiff line numberDiff line change
@@ -3560,6 +3560,12 @@ def infer_lambda_type_using_context(self, e: LambdaExpr) -> Tuple[Optional[Calla
35603560
callable_ctx = get_proper_type(replace_meta_vars(ctx, ErasedType()))
35613561
assert isinstance(callable_ctx, CallableType)
35623562

3563+
if callable_ctx.type_guard is not None:
3564+
# Lambda's return type cannot be treated as a `TypeGuard`,
3565+
# because it is implicit. And `TypeGuard`s must be explicit.
3566+
# See https://github.com/python/mypy/issues/9927
3567+
return None, None
3568+
35633569
arg_kinds = [arg.kind for arg in e.arguments]
35643570

35653571
if callable_ctx.is_ellipsis_args:

test-data/unit/check-typeguard.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def main1(a: object) -> None:
247247

248248
[builtins fixtures/tuple.pyi]
249249

250-
[case testTypeGuardOverload-skip]
250+
[case testTypeGuardOverload]
251251
# flags: --strict-optional
252252
from typing import overload, Any, Callable, Iterable, Iterator, List, Optional, TypeVar
253253
from typing_extensions import TypeGuard

0 commit comments

Comments
 (0)