Skip to content

Always allow lambda calls #17430

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
Jun 23, 2024
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
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]
Loading