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 undefined-variable when variable name in decoration matches function argument #3791

Closed
mthuurne opened this issue Aug 24, 2020 · 2 comments · Fixed by #4737
Closed
Labels
Milestone

Comments

@mthuurne
Copy link
Contributor

Steps to reproduce

Run pylint on the following code:

def decorate(seq):
    return lambda f: f

@decorate(x*x for x in range(3))
def f(x):
    pass

@decorate(x*x for x in range(3))
def g(y):
    pass

Current behavior

pylint reports:

4:12: E0602: Undefined variable 'x' (undefined-variable)

Line 4 being the @decorate preceding f().

Note that no error is reported for g(): the problem only occurs if the variable name inside the comprehension matches one of the decorated function's arguments. Which is strange, since they should be in separate scopes.

Expected behavior

No error is reported.

pylint --version output

pylint 2.6.0
astroid 2.4.2
Python 3.8.4 (default, Jul 14 2020, 09:08:56) [GCC]

This problem did not occur until I upgraded from pylint 2.5.3 to 2.6.0.

@stpierre
Copy link

We're hitting this as well. The other requirement of this is that the variable needs to be used twice in a list comprehension in the decorator. (This is really remarkably specific!) So for instance, these all give the false-positive undefined-variable:

@decorate(i * i for i in [])
def thing1(i):
    pass

@decorate(dict(i=i, j=i) for i in [])
def thing2(i):
    pass

@decorate((i, i) for i in [])
def thing3(i):
    pass

But this does not:

@decorate(i for i in [])
def thing4(i):
    pass

Neither do other ways of declaring and introducing new variables, for instance lambdas. These are both fine:

@decorate(lambda i: i * i)
def thing5(i):
    pass

@decorate(lambda i: [i * i for i in []])
def thing6(i):
    pass

This is also broken in pylint 2.7.

@rr-
Copy link
Contributor

rr- commented Jul 22, 2021

I believe this is relevant place to patch
pylint/checkers/variables.py:992

            # if the name node is used as a function default argument's value or as
            # a decorator, then start from the parent frame of the function instead
            # of the function frame - and thus open an inner class scope
            if (
                current_consumer.scope_type == "function"
                and self._defined_in_function_definition(node, current_consumer.node)
            ):
                # ignore function scope if is an annotation/default/decorator, as not in the body
                continue

Relevant issue: #1731

@rr- rr- mentioned this issue Jul 22, 2021
4 tasks
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.9.6 milestone Jul 22, 2021
CirqBot pushed a commit to quantumlib/Cirq that referenced this issue May 18, 2022
The `undefined-variable` pylint plugin can now be re-added in, as it was removed until [an underlying issue](pylint-dev/pylint#3791) had been fixed.
samdoran added a commit to samdoran/ansible that referenced this issue Jul 21, 2022
Update pylint to 2.9.6 to get around a false positive for
undefined-variable in a decorator.

pylint-dev/pylint#3791
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
The `undefined-variable` pylint plugin can now be re-added in, as it was removed until [an underlying issue](pylint-dev/pylint#3791) had been fixed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants