Skip to content

Commit 0273db7

Browse files
Fix false positive for unnecessary-lambda. (#9149) (#9200)
This simplifies the check for the call having kwargs but not the lambda. (cherry picked from commit 2289c71) Co-authored-by: Clément Schreiner <[email protected]>
1 parent 53d4541 commit 0273db7

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed false positive for ``unnecessary-lambda`` when the call has keyword arguments but not the lambda.
2+
3+
Closes #9148

pylint/checkers/base/basic_checker.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,6 @@ def _has_variadic_argument(
519519
)
520520

521521
@utils.only_required_for_messages("unnecessary-lambda")
522-
# pylint: disable-next=too-many-return-statements
523522
def visit_lambda(self, node: nodes.Lambda) -> None:
524523
"""Check whether the lambda is suspicious."""
525524
# if the body of the lambda is a call expression with the same
@@ -544,29 +543,20 @@ def visit_lambda(self, node: nodes.Lambda) -> None:
544543
# return something else (but we don't check that, yet).
545544
return
546545

547-
call_site = astroid.arguments.CallSite.from_call(call)
548546
ordinary_args = list(node.args.args)
549547
new_call_args = list(self._filter_vararg(node, call.args))
550548
if node.args.kwarg:
551-
if self._has_variadic_argument(call.kwargs, node.args.kwarg):
549+
if self._has_variadic_argument(call.keywords, node.args.kwarg):
552550
return
551+
elif call.keywords:
552+
return
553553

554554
if node.args.vararg:
555555
if self._has_variadic_argument(call.starargs, node.args.vararg):
556556
return
557557
elif call.starargs:
558558
return
559559

560-
if call.keywords:
561-
# Look for additional keyword arguments that are not part
562-
# of the lambda's signature
563-
lambda_kwargs = {keyword.name for keyword in node.args.defaults}
564-
if len(lambda_kwargs) != len(call_site.keyword_arguments):
565-
# Different lengths, so probably not identical
566-
return
567-
if set(call_site.keyword_arguments).difference(lambda_kwargs):
568-
return
569-
570560
# The "ordinary" arguments must be in a correspondence such that:
571561
# ordinary_args[i].name == call.args[i].name.
572562
if len(ordinary_args) != len(new_call_args):

tests/functional/u/unnecessary/unnecessary_lambda.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
_ = lambda: _ANYARGS(*[3], **{'three': 3})
5353
_ = lambda: _ANYARGS(func=42)
5454

55+
# pylint: disable=missing-function-docstring
56+
def f(d):
57+
print(lambda x: str(x, **d))
58+
5559
# Don't warn about this.
5660
_ = lambda: code().analysis()
5761

0 commit comments

Comments
 (0)