Skip to content

Do not auto-infer TypeGuard return type for lambda, refs #9927 #11417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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