使用自定义属性类时如何避免 pylint 不可迭代 [英] How to avoid pylint not-an-iterable when using a custom property class
问题描述
我的代码使用了常用的cachead>_property来自 werkzeug 的课程.考虑以下代码段:
My code uses the commonly used cached_property class from werkzeug. Consider the following snippet:
from werkzeug import cached_property
class SampleClass(object):
@cached_property
def list_prop(self):
return [1, 2]
sample = SampleClass()
for item in sample.list_prop:
print item
我在 CI 过程中使用 pylint.如果我运行 pylint not-an-iterable 检查此代码,即使代码非常好,它也会失败.
I use pylint in my CI process. If I run the pylint not-an-iterable check on this code, it fails even though the code is perfectly fine.
$ pylint --disable=all --enable=not-an-iterable prop.py
************* Module prop
E: 9,12: Non-iterable value sample.list_prop is used in an iterating context (not-an-iterable)
pylint 在使用内置的 @property
装饰器而不是 @cached_property
检查相同的代码时效果很好:
pylint works well when checking the same code with the built-in @property
decorator instead of @cached_property
:
class SampleClass(object):
@property
def list_prop(self):
return [1, 2]
我应该怎么做才能帮助 pylint 克服这种误报?
What should I do to help pylint overcome this false positive?
推荐答案
您似乎错误地导入了 cached_property
.它位于 werkzeug.utils
中.pylint
发现了这个错误:E: 1, 0: No name 'cached_property' in module 'werkzeug' (no-name-in-module)
.这是固定代码:
Looks like you are importing cached_property
incorrectly. It lives in werkzeug.utils
. pylint
caught that error: E: 1, 0: No name 'cached_property' in module 'werkzeug' (no-name-in-module)
. Here's the fixed code:
from werkzeug.utils import cached_property
class SampleClass(object):
@cached_property
def list_prop(self):
return [1, 2]
sample = SampleClass()
for item in sample.list_prop:
print item
当我在应用此修复程序后运行 pylint
时,它停止抱怨:
When I run pylint
after applying this fix, it stops complaining:
$ pylint test
No config file found, using default configuration
************* Module test
C: 1, 0: Missing module docstring (missing-docstring)
C: 3, 0: Missing class docstring (missing-docstring)
C: 5, 4: Missing method docstring (missing-docstring)
R: 3, 0: Too few public methods (1/2) (too-few-public-methods)
C: 8, 0: Invalid constant name "sample" (invalid-name)
这篇关于使用自定义属性类时如何避免 pylint 不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!