Pylint: attribute-defined-outside-init warning wrongly triggered when using properties

Created on 5 Dec 2014  路  2Comments  路  Source: PyCQA/pylint

Originally reported by: James Nande (BitBucket: jnande)


Hi,

Pylint seems unable to detect Python properties properly. For instance, an attribute-defined-outside-init is triggered on the last line of the following class declaration :

#!python
class Mine:
    def __init__(self, x):
        self.x = x

    @property
    def x(self):
        return self.__x

    @x.setter
    def x(self, x):
        self.__x = x

Besides using a disable-msg comment, the only way I've found to circumvent this is to put a spurious self.__x = None before self.x = x.


bug

Most helpful comment

I just ran into this today. Seems it hasn't gotten much attention though it's a known (albeit minor) issue. Is this just something no one has gotten around to or is there a technical limitaion which negates being able detect this condition and not show the linter warning?

Actually, while I don't know what PEP has to say about it, it might be nice if there was a way to detect if a method was only ever called from __init__ that any self. assignments not have their warnings shown either, since that line was technically executed only during object initialization. This would naturally encapsulate the property setter call, as well.

All 2 comments

I just ran into this today. Seems it hasn't gotten much attention though it's a known (albeit minor) issue. Is this just something no one has gotten around to or is there a technical limitaion which negates being able detect this condition and not show the linter warning?

Actually, while I don't know what PEP has to say about it, it might be nice if there was a way to detect if a method was only ever called from __init__ that any self. assignments not have their warnings shown either, since that line was technically executed only during object initialization. This would naturally encapsulate the property setter call, as well.

I'm also hitting this. Please fix. I bet it's a difficult check though since you can call anything from anywhere and you'd have to check it all to make sure things are called from within __init__, etc.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DGalt picture DGalt  路  3Comments

z4y4ts picture z4y4ts  路  3Comments

GergelyKalmar picture GergelyKalmar  路  3Comments

PCManticore picture PCManticore  路  3Comments

pylint-bot picture pylint-bot  路  3Comments