Skip to content
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

False positive: unnecessary lambda with kwargs in the call #9148

Closed
clemux opened this issue Oct 15, 2023 · 1 comment · Fixed by #9149
Closed

False positive: unnecessary lambda with kwargs in the call #9148

clemux opened this issue Oct 15, 2023 · 1 comment · Fixed by #9149
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Milestone

Comments

@clemux
Copy link
Contributor

clemux commented Oct 15, 2023

Bug description

# pylint: disable=missing-module-docstring,undefined-variable,missing-function-docstring
def f(a, d):
    print(lambda x: a(x, **d))   

Configuration

No response

Command used

pylint test.py

Pylint output

************* Module test
test.py:3:2: W0108: Lambda may not be necessary (unnecessary-lambda)

------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 0.00/10, +0.00)

Expected behavior


Your code has been rated at 10.00/10 (previous run: 0.00/10, +10.00)

Pylint version

pylint 3.0.1
astroid 3.0.0
Python 3.11.4 (main, Jun  9 2023, 07:59:55) [GCC 12.3.0]

OS / Environment

Distributor ID: Ubuntu
Description: Ubuntu 23.04
Release: 23.04
Codename: lunar

Additional dependencies

No response

@clemux clemux added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Oct 15, 2023
@clemux
Copy link
Contributor Author

clemux commented Oct 15, 2023

I stumbled upon this false positive for W0108 (unnecessary-lambda) in the tests of pandas, for example this

I've looked into the implementation, and this part looks weird to me:

        if call.keywords:
            # Look for additional keyword arguments that are not part
            # of the lambda's signature
            lambda_kwargs = {keyword.name for keyword in node.args.defaults}
            if len(lambda_kwargs) != len(call_site.keyword_arguments):
                # Different lengths, so probably not identical
                return
            if set(call_site.keyword_arguments).difference(lambda_kwargs):
                return

As far as I can tell, lambda_kwargs is always empty because of this condition at the beginning of the check

        if node.args.defaults:
            return

In the case of my example, len(call_site.keyword_arguments) is empty. I don't know why it is empty, or even why the condition is made on this instead of simply comparing call.keywords and node.args.kwargs, but it explains why the warning is triggered here.

@clemux clemux changed the title False positive: unnecessary lambda with kwargs in the call False positive: unnecessary lambda with kwargs in the call Oct 15, 2023
@clemux clemux changed the title False positive: unnecessary lambda with kwargs in the call False positive: unnecessary lambda with kwargs in the call Oct 15, 2023
@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Oct 29, 2023
@Pierre-Sassoulas Pierre-Sassoulas added this to the 3.0.3 milestone Oct 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants