Skip to content

Commit

Permalink
Always allow lambda calls (#17430)
Browse files Browse the repository at this point in the history
See #17408 for context.
  • Loading branch information
ilevkivskyi committed Jun 23, 2024
1 parent 79b1c8d commit 39b9b89
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
ARG_STAR,
ARG_STAR2,
IMPLICITLY_ABSTRACT,
LAMBDA_NAME,
LITERAL_TYPE,
REVEAL_LOCALS,
REVEAL_TYPE,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ def get_nongen_builtins(python_version: tuple[int, int]) -> dict[str, str]:
"typing_extensions.runtime_checkable",
)

LAMBDA_NAME: Final = "<lambda>"


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

@property
def name(self) -> str:
return "<lambda>"
return LAMBDA_NAME

def expr(self) -> Expression:
"""Return the expression (the body) of the lambda."""
Expand Down
2 changes: 1 addition & 1 deletion mypy/plugins/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

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

PARTIAL = "functools.partial"
PARTIAL: Final = "functools.partial"


class _MethodInfo(NamedTuple):
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]

0 comments on commit 39b9b89

Please sign in to comment.