pylint icon indicating copy to clipboard operation
pylint copied to clipboard

False positives with imported variable that shadows class

Open kulikjak opened this issue 1 year ago • 1 comments

Bug description

other.py:

class MyDict(dict):
    pass

MyDict = MyDict()
print("item" in MyDict)

main.py:

from other import MyDict
print("item" in MyDict)

Pylint seems to be confused about the class being named the same as the variable. When I call pylint directly on the other.py file, everything works as expected. However, when I call it on main.py (which imports other), it gets confused.

When I rename the class to MyDict class to something else, it works again.

Command used

pylint main.py

Pylint output

main.py:2:16: E1135: Value 'MyDict' doesn't support membership test (unsupported-membership-test)

Expected behavior

No unsupported-membership-test error.

Pylint version

pylint 3.2.6
astroid 3.2.4
Python 3.11.8 (main, Jul  2 2024, 13:41:42) [GCC 13.2.0]

OS / Environment

No response

Additional dependencies

No response

kulikjak avatar Aug 01 '24 11:08 kulikjak

I just realized that it's not just unsupported-membership-test, but also other errors related to the same confusion like:

W0125: Using a conditional statement with a constant value (using-constant-test)
E1137: 'MyDict' does not support item assignment (unsupported-assignment-operation)
...

kulikjak avatar Aug 01 '24 11:08 kulikjak