Search code examples
pythonvisual-studio-codepylint

Make Pylint recognise variables defined by modifying __dict__


I have a codebase (that I did not write and can't modify) where objects are created with their names given as strings in the following way:

import sys

class A:
    def __init__(self, name):
        self.name = name
        sys.modules['__main__'].__dict__[name] = self

The objects are then defined and accessed as

A('test')
print(test.name)

However, when I open this in Visual Studio Code, all of these variables are marked as undefined by pylint. Does anyone know of a good workaround to get Pylint not to recognise these objects? I could turn off undefined variable warnings altogether, but I would like to still have the benefits of variable checks, if possible.


Solution

  • There's no way to make Pylint work in this case. Pylint does static analysis and this would require dynamic analysis.