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 for unexpected-keyword-arg for decorators with inner inner function #5784

Open
martimlobao opened this issue Feb 9, 2022 · 3 comments
Labels
Decorators False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@martimlobao
Copy link

martimlobao commented Feb 9, 2022

Bug description

Just tried this out with the latest version of the main branch (any version after bfeca4c) and there seems to be a case where #5547 is still raising a false positive unexpected-keyword-arg error.

def example_decorator(*new_func_args, **new_func_kwargs):
    def decorator(func):
        def wrapper(*args, **kwargs):
            print_me = kwargs.pop("print_me", None)
            def inner(*inner_args, **inner_kwargs):
                print(print_me)
                return func(*inner_args, **inner_kwargs)

            return inner(*args, **kwargs)

        return wrapper

    if len(new_func_args) == 1 and len(new_func_kwargs) == 0 and callable(new_func_args[0]):
        return decorator(new_func_args[0])
    return decorator

@example_decorator
def example_func(num):
    return num ** 2

example_func(6, print_me="hello")

(Note that no error is raised by pylint if we remove the top-level function (example_decorator). The reason for someone to define the decorator this way is so that it's possible to use the bare @example_decorator, as well as with arguments @example_decorator(foo=1).)

Using pylint on commit hash e75e37a, this is the error that's printed out:

E1123: Unexpected keyword argument 'print_me' in function call (unexpected-keyword-arg)

Calling example_func works fine:

>>> example_task(6, print_me="hello")
hello
36

Command used

pylint a.py

Pylint output

************* Module a
E1123: Unexpected keyword argument 'print_me' in function call (unexpected-keyword-arg)

Expected behavior

No unexpected-keyword-arg error raised

Pylint version

current pylint version in the `main` branch (set for 2.13.0)
@martimlobao martimlobao added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Feb 9, 2022
@martimlobao
Copy link
Author

See #5547 for additional context

@DanielNoord DanielNoord changed the title false positive False positive for unexpected-keyword-arg for decorators with inner inner function Feb 9, 2022
@DanielNoord DanielNoord added False Positive 🦟 A message is emitted but nothing is wrong with the code Decorators and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Feb 9, 2022
@Pierre-Sassoulas Pierre-Sassoulas added the Needs PR This issue is accepted, sufficiently specified and now needs an implementation label Jul 6, 2022
@rmorshea
Copy link

Unsure if this is a true solution, but perhaps related: #8332

@mamiu
Copy link

mamiu commented Sep 15, 2023

Have the same problem. Here's a very simplified version of the code that displays this error:

def clean_html(html):
    elements_to_delete = [
        {"search_term": "footer", "delete_after": True}
    ]

    for etd in elements_to_delete:
        search_term = etd.pop("search_term", None)
        elements = html.find_all(search_term)

        html = delete_elements(html, elements, **etd)

        # ... rest of the code

This is the error I get:

        html = delete_elements(html, elements, **etd)
#              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#              Pylint: Unexpected keyword argument 'search_term' in function call

BTW: This is the function definition of the delete_elements function:

def delete_elements(html, elements=[], delete_before=False, delete_after=False):
    # some code to delete html elements

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Decorators False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

No branches or pull requests

5 participants