From 39b9b899178e6a30e7e8664c12f0eb610b8a44a5 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sun, 23 Jun 2024 22:09:22 +0100 Subject: [PATCH] Always allow lambda calls (#17430) See https://github.com/python/mypy/pull/17408 for context. --- mypy/checkexpr.py | 2 ++ mypy/nodes.py | 4 +++- mypy/plugins/functools.py | 2 +- test-data/unit/check-functions.test | 16 ++++++++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 7ae23cfe516c..fdc0f94b3997 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -36,6 +36,7 @@ ARG_STAR, ARG_STAR2, IMPLICITLY_ABSTRACT, + LAMBDA_NAME, LITERAL_TYPE, REVEAL_LOCALS, REVEAL_TYPE, @@ -599,6 +600,7 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> and self.chk.in_checked_function() and isinstance(callee_type, CallableType) and callee_type.implicit + and callee_type.name != LAMBDA_NAME ): if fullname is None and member is not None: assert object_type is not None diff --git a/mypy/nodes.py b/mypy/nodes.py index 5d3a1d31aece..d215bcfce098 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -175,6 +175,8 @@ def get_nongen_builtins(python_version: tuple[int, int]) -> dict[str, str]: "typing_extensions.runtime_checkable", ) +LAMBDA_NAME: Final = "" + class Node(Context): """Common base class for all non-type parse tree nodes.""" @@ -2262,7 +2264,7 @@ class LambdaExpr(FuncItem, Expression): @property def name(self) -> str: - return "" + return LAMBDA_NAME def expr(self) -> Expression: """Return the expression (the body) of the lambda.""" diff --git a/mypy/plugins/functools.py b/mypy/plugins/functools.py index 19be71ca36df..9589c6aeca8b 100644 --- a/mypy/plugins/functools.py +++ b/mypy/plugins/functools.py @@ -25,7 +25,7 @@ _ORDERING_METHODS: Final = {"__lt__", "__le__", "__gt__", "__ge__"} -PARTIAL = "functools.partial" +PARTIAL: Final = "functools.partial" class _MethodInfo(NamedTuple): diff --git a/test-data/unit/check-functions.test b/test-data/unit/check-functions.test index 29cd977fe5d6..93540e203c36 100644 --- a/test-data/unit/check-functions.test +++ b/test-data/unit/check-functions.test @@ -3366,3 +3366,19 @@ class C(B): ) -> None: ... [builtins fixtures/tuple.pyi] + +[case testLambdaAlwaysAllowed] +# flags: --disallow-untyped-calls +from typing import Callable, Optional + +def func() -> Optional[str]: ... +var: Optional[str] + +factory: Callable[[], Optional[str]] +for factory in ( + lambda: var, + func, +): + reveal_type(factory) # N: Revealed type is "def () -> Union[builtins.str, None]" + var = factory() +[builtins fixtures/tuple.pyi]