Pylint:如何使用属性类指定自定义属性装饰器?



Pylint 报告使用自定义属性装饰器的 Python 代码的错误 E0202(方法隐藏(。我尝试使用属性类选项失败了。

以下是props.py

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

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)

使用 Python 2.7.13 运行代码会产生预期的结果:

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

Pylint 1.6.5 报告自定义属性的错误,但不报告常规属性的错误:

$ 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)

第 29 行是使用自定义属性的 setter:

self.myattr = 'myvalue'

什么是合适的选择?还是误报?

不确定我是否遇到了与您相同的问题,因为我遇到了no-member错误。

我正在使用的装饰器名为@memoized_property,我能够通过将其添加到我的 pylintrc 来解决问题:

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

(你也可以把它作为一个参数传递给 pylint:--init-hook="import astroid.bases; astroid.bases.POSSIBLE_PROPERTIES.add('memoized_property')"(

最新更新