Skip to content

Commit 39b9b89

Browse files
authored
Always allow lambda calls (#17430)
See #17408 for context.
1 parent 79b1c8d commit 39b9b89

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

mypy/checkexpr.py

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
ARG_STAR,
3737
ARG_STAR2,
3838
IMPLICITLY_ABSTRACT,
39+
LAMBDA_NAME,
3940
LITERAL_TYPE,
4041
REVEAL_LOCALS,
4142
REVEAL_TYPE,
@@ -599,6 +600,7 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) ->
599600
and self.chk.in_checked_function()
600601
and isinstance(callee_type, CallableType)
601602
and callee_type.implicit
603+
and callee_type.name != LAMBDA_NAME
602604
):
603605
if fullname is None and member is not None:
604606
assert object_type is not None

mypy/nodes.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ def get_nongen_builtins(python_version: tuple[int, int]) -> dict[str, str]:
175175
"typing_extensions.runtime_checkable",
176176
)
177177

178+
LAMBDA_NAME: Final = "<lambda>"
179+
178180

179181
class Node(Context):
180182
"""Common base class for all non-type parse tree nodes."""
@@ -2262,7 +2264,7 @@ class LambdaExpr(FuncItem, Expression):
22622264

22632265
@property
22642266
def name(self) -> str:
2265-
return "<lambda>"
2267+
return LAMBDA_NAME
22662268

22672269
def expr(self) -> Expression:
22682270
"""Return the expression (the body) of the lambda."""

mypy/plugins/functools.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
_ORDERING_METHODS: Final = {"__lt__", "__le__", "__gt__", "__ge__"}
2727

28-
PARTIAL = "functools.partial"
28+
PARTIAL: Final = "functools.partial"
2929

3030

3131
class _MethodInfo(NamedTuple):

test-data/unit/check-functions.test

+16
Original file line numberDiff line numberDiff line change
@@ -3366,3 +3366,19 @@ class C(B):
33663366
) -> None:
33673367
...
33683368
[builtins fixtures/tuple.pyi]
3369+
3370+
[case testLambdaAlwaysAllowed]
3371+
# flags: --disallow-untyped-calls
3372+
from typing import Callable, Optional
3373+
3374+
def func() -> Optional[str]: ...
3375+
var: Optional[str]
3376+
3377+
factory: Callable[[], Optional[str]]
3378+
for factory in (
3379+
lambda: var,
3380+
func,
3381+
):
3382+
reveal_type(factory) # N: Revealed type is "def () -> Union[builtins.str, None]"
3383+
var = factory()
3384+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)