我制作了一个classproperty描述符,每当我使用用它装饰的函数时,我都会收到多个pylint检查错误。
这是一个带有示例装饰函数的示例类:
class Bar:
"""
Bar documentation.
"""
# pylint: disable=no-method-argument
@classproperty
def foo():
"""
Retrieve foo.
"""
return "foo"
Run Code Online (Sandbox Code Playgroud)
多亏了描述符,我可以调用Bar.foo并获取返回的字符串foo。
不幸的是,每当我将这样的函数与稍微复杂的项目(例如返回对象实例的函数)一起使用时,就会pylint开始抱怨诸如no-memberor 之类的东西unexpected-keyword-arg,仅仅是因为它认为Bar.foo是一种方法,而不是包装的classproperty对象。
我想为使用我的函数的任何代码禁用警告- 我绝对不能允许每次使用-wrapped 方法时都必须编写。我该怎么做?或者我应该改用不同的 linter?# pylint: disableclasspropertypylint
以下是由于上述原因生成的警告示例:
class Bar:
"""
Bar documentation.
"""
# pylint: disable=no-method-argument
@classproperty
def foo():
"""
Retrieve an object.
"""
return NotImplementedError("Argument")
print(Bar.foo.args)
Run Code Online (Sandbox Code Playgroud)
pylint抱怨E1101: Method 'foo' has no 'args' member (no-member)(即使我知道它肯定有),我想完全禁用使用Bar.foo.args或类似的任何模块/类/函数的一些警告。
对于任何感兴趣的人,这里是一个classproperty描述符的最小实现:
class classproperty:
"""
Minimal descriptor.
"""
# pylint: disable=invalid-name
def __init__(self, func):
self._func = func
def __get__(self, _obj, _type):
return self._func()
Run Code Online (Sandbox Code Playgroud)
我成功地通过将项目类型提示为以下内容来创建一个肮脏的黑客None:
class Bar:
"""
Bar documentation.
"""
# pylint: disable=no-method-argument,function-redefined,too-few-public-methods
foo: None
@classproperty
def foo():
"""
Retrieve an object.
"""
return NotImplementedError("Argument")
Run Code Online (Sandbox Code Playgroud)
我宁愿避免使用这样的代码,因为由于循环导入问题(因此None),我实际上无法导入应该进行类型提示的项目,但它pylint很好用。