From 68047f90bc4f140a717a4dd64ba762003122b20d Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Mon, 1 Nov 2021 00:11:29 +0300 Subject: [PATCH] Do not use TypeGuard context for lambda (#11417) Closes #9927 --- mypy/checkexpr.py | 6 ++++++ test-data/unit/check-typeguard.test | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 42990d4d28a3..536626f175f3 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -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: diff --git a/test-data/unit/check-typeguard.test b/test-data/unit/check-typeguard.test index 6dae011b59ce..32fe5e750989 100644 --- a/test-data/unit/check-typeguard.test +++ b/test-data/unit/check-typeguard.test @@ -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