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
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 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]
What should I do to help pylint overcome this false positive?
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
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)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加