pylint icon indicating copy to clipboard operation
pylint copied to clipboard

no need to complain 'attribute-defined-outside-init' when base class not in same file as sub class

Open CipherChen opened this issue 5 years ago • 8 comments

Steps to reproduce

from m import BaseA


class A(BaseA):
    def load(self, x):
        self.x = x

Current behavior

/tmp/3.py:7: [W0201(attribute-defined-outside-init), A.load] Attribute 'x' defined outside __init__

Expected behavior

In this module, the pylint would complain 'attribute-defined-outside-init' since it does not found self.x defined in __init__. But the fact is that self.x MAY have already been defined in base class BaseA in different module m.py.

To my understanding, pylint should not complain such indeterminacy.

pylint --version output

Using config file $PATH_TO_pylintrc pylint 1.9.5, astroid 1.6.6 Python 2.7.10 (default, Aug 17 2018, 17:41:52) [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)]

CipherChen avatar Apr 21 '20 09:04 CipherChen

After a quick check it seems pylint does not emit this when the base class is importable and the attribute is defined in __init__. But it still emits the error if the base class cannot be found or inferred. We should change the behaviour of the latter.

PCManticore avatar Apr 23 '20 06:04 PCManticore

Also shows up if the init() for class BaseA calls something like self.initialize() and you try to initialize vars for the subclass in that subclass.

stridera avatar Oct 15 '21 09:10 stridera

Is there any update on this?

jeroenNG avatar Jan 06 '22 11:01 jeroenNG

We're not maintaining pylint 1.9 anymore. In the latest pylint, with:

a.py:

from m import BaseA


class A(BaseA):
    def load(self, x):
        self.x = x

And m.py:

class BaseA:
	def __init__(self):
		self.x = 1

We do not get any attribute-defined-outside-init as expected.

Also shows up if the init() for class BaseA calls something like self.initialize() and you try to initialize vars for the subclass in that subclass.

If you're using a function called initialize or ìnit, consider initializing in the function that is semantically the constructor in python (__init__) or disable the warning for your project or the class.

Pierre-Sassoulas avatar Jan 06 '22 11:01 Pierre-Sassoulas

@Pierre-Sassoulas The latest version pylint at the time of my comment (v2.17.5) seems to have reintroduced this bug, as I am getting the attribute-defined-outside-init error using exactly the example code provided.

Please see the attached 10 second video for a demo. Can we reopen this issue?

https://github.com/pylint-dev/pylint/assets/124291546/3a28f7e1-2554-4690-abef-9e16d8ed419a

bfrisbie-wiz avatar Sep 11 '23 17:09 bfrisbie-wiz

Hello, the latest version of pylint is 3.0.0a7, would you mind testing after upgrading ?

Pierre-Sassoulas avatar Sep 11 '23 19:09 Pierre-Sassoulas

Hello, the latest version of pylint is 3.0.0a7, would you mind testing after upgrading ?

Hi @Pierre-Sassoulas and thanks for your quick reply!

Yes, the error/bug still occurs on version 3.0.0a7, as shown in the video below.

https://github.com/pylint-dev/pylint/assets/124291546/d784b148-fca8-43c8-8ccd-89a46b324954

bfrisbie-wiz avatar Sep 11 '23 21:09 bfrisbie-wiz

As of today, this bug still exists, please fix, thanks!!!

HaKuNamaTT-ZZH avatar Jan 05 '24 09:01 HaKuNamaTT-ZZH