maf
maf

Reputation: 333

Pylint: How to specify a self-defined property decorator with property-classes?

Pylint reports an error E0202 (method-hidden) for Python code that uses a self-defined property decorator. My attempts to use the property-classes option have failed.

Here are props.py

from functools import wraps

def myproperty(func):
    @wraps(func)
    def fget(self):
        return func(self)
    return property(fget)

and testimport.py:

#!/usr/bin/python

from props import myproperty

class E0202(object):

    def __init__(self):
        self._attr = 'attr'
        self._myattr = 'myattr'

    @property
    def attr(self):
        return self._attr

    @attr.setter
    def attr(self, value):
        self._attr = value

    @myproperty
    def myattr(self):
        return self._myattr

    @myattr.setter
    def myattr(self, value):
        self._myattr = value

    def assign_values(self):
        self.attr = 'value'
        self.myattr = 'myvalue'

if __name__ == '__main__':
    o = E0202()
    print(o.attr, o.myattr)
    o.assign_values()
    print(o.attr, o.myattr)

Running the code with Python 2.7.13 produces the expected results:

$ python test.py
('attr', 'myattr')
('value', 'myvalue')

Pylint 1.6.5 reports an error for the self-defined property, but not for the regular property:

$ pylint -E --property-classes=props.myproperty testimport.py 
No config file found, using default configuration
************* Module testimport
E: 20, 4: An attribute defined in testimport line 29 hides this method (method-hidden)

Line 29 is the use of the setter of the self-defined property:

        self.myattr = 'myvalue'

What would be the proper options for pylint? Or is this a false positive?

Upvotes: 3

Views: 995

Answers (1)

bgbbg
bgbbg

Reputation: 61

Not sure if I was having the same problem as you were, as I was getting a no-member error.

The decorator I am using is named @memoized_property, and I was able to solve the problem by adding this to my pylintrc:

init-hook="import astroid.bases; astroid.bases.POSSIBLE_PROPERTIES.add('memoized_property')"

(you can also pass it as an argument to pylint: --init-hook="import astroid.bases; astroid.bases.POSSIBLE_PROPERTIES.add('memoized_property')")

Upvotes: 6

Related Questions