pylint icon indicating copy to clipboard operation
pylint copied to clipboard

undefined-variable regression since 2.6.0

Open twmr opened this issue 3 years ago • 3 comments

Bug description

Since 2.6.0 pylint reports a false positive undefined-variable warning when local decorators are used for a class inside a class.

from myconverter import Converter


class MyContainer:
    CONVERTER = Converter()

    @CONVERTER.register()
    class NamedArray:
        def __init__(self, *args, **kwargs):
            pass

Configuration

No response

Command used

pylint --disable all --enable undefined-variable pylint_issue.py

Pylint output

************* Module pylint_issue_py311
pylint_issue.py:7:5: E0602: Undefined variable 'CONVERTER' (undefined-variable)

-------------------------------------------------------------------
Your code has been rated at 1.67/10 (previous run: 10.00/10, -8.33)

Expected behavior

No undefined-variable warning

Pylint version

pylint --version                                                                                                                                                                        
pylint 2.6.0
astroid 2.5
Python 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:36:39) [GCC 10.4.0]

(but also can be reproduced with the latest version)

OS / Environment

ubuntu 22.04

Additional dependencies

No response

twmr avatar Nov 05 '22 16:11 twmr

Thank you @thisch for the report. Unfortunately I can't reproduce it easily; could you let us know what Converter looks like?

Update: I can reproduce now (earlier I was using an older Pylint version).

mbyrnepr2 avatar Nov 05 '22 16:11 mbyrnepr2

Update: I can reproduce now (earlier I was using an older Pylint version).

👍🏿 . Note that the module myconverter doesn't really exist on my host, but I can still reproduce it.

Note that I can fix the issue by moving the instantiation of the converter class outside of the class MyContainer. This is not a big problem. I just wanted to inform you about the regression

twmr avatar Nov 05 '22 17:11 twmr

I also encountered this bug with pylint 3.3.6. Here is a self-contained example:

def f(cls):
    return cls

class C:
    g = f

    @g
    class D:
        pass

Output:

************* Module test
test.py:7:5: E0602: Undefined variable 'g' (undefined-variable)

sattlerc avatar Apr 13 '25 04:04 sattlerc