Search code examples
pythonpython-3.xattributesmetaclasspylint

Why does Pylint error on metaclass-defined attribute usage?


I am using a metaclass in my code, and the code works. (Using a metaclass, it sets the attribute test_attr to "Success!" on object creation.) When I run pylint on this code, however, it displays errors in Test.test, saying that test_attr is not defined.

class MyMeta(type):
    def __new__(mcs, name, bases, attrs):
        attrs["test_attr"] = "Success!"
        return super().__new__(mcs, name, bases, attrs)


class Test(metaclass=MyMeta):
    def test(self):
        return self.test_attr

What should I do to satisfy pylint? Is there a configuration option to fix this? Is there something about my code that I should fix?


Solution

  • I'm guessing pylint simply doesn't understand this kind of "magic" - why are you doing it in the first place?

    You could set generated-members or ignored-classes (see docs) to tell pylint about it.