如何禁用任何使用我的函数的 pylint 检查?
How to disable pylint inspections for anything that uses my function?
我制作了一个 classproperty
描述符,每当我使用用它装饰的函数时,我都会遇到多个 pylint
检查错误。
这是一个示例 class,其中包含一个示例装饰函数:
class Bar:
"""
Bar documentation.
"""
# pylint: disable=no-method-argument
@classproperty
def foo():
"""
Retrieve foo.
"""
return "foo"
感谢描述符,我可以调用 Bar.foo
并获取字符串 foo returned.
不幸的是,每当我将这样的函数用于稍微复杂的项目(例如 return 对象实例的函数)时,pylint
开始抱怨诸如 no-member
或 unexpected-keyword-arg
,只是因为它认为 Bar.foo
是一个方法,而不是包装的 classproperty
对象。
我想禁用任何代码的警告,使用我的功能 - 我绝对不能允许必须写# pylint: disable
每次我使用 classproperty
-wrapped 方法。我怎样才能用 pylint
做到这一点?或者也许我应该改用其他 linter?
以下是由于上述原因生成的警告示例:
class Bar:
"""
Bar documentation.
"""
# pylint: disable=no-method-argument
@classproperty
def foo():
"""
Retrieve an object.
"""
return NotImplementedError("Argument")
print(Bar.foo.args)
pylint
抱怨 E1101: Method 'foo' has no 'args' member (no-member)
(即使我知道它肯定有),我想完全禁用任何使用 Bar.foo.args
的 module/class/function 的一些警告或类似的。
对于任何感兴趣的人,这里是 classproperty
描述符的最小实现:
class classproperty:
"""
Minimal descriptor.
"""
# pylint: disable=invalid-name
def __init__(self, func):
self._func = func
def __get__(self, _obj, _type):
return self._func()
据我所知,这是不可能的。
我还没有在 pylint 的配置中找到解决这个问题的方法。我能找到的最接近的是 property-classes
选项,但它只影响 invalid-name
检查器,所以不是我们在这里寻找的:
:property-classes:
List of decorators that produce properties, such as abc.abstractproperty. Add
to this list to register other decorators that produce valid properties.
These decorators are taken in consideration only for invalid-name.
Default: ``abc.abstractproperty``
也许这是一个值得直接问 pylint's developers 的问题。
在我看来,这似乎可以通过 transform plugin (Maybe this 获得灵感来解决?)。 Pylint 可以很好地处理 @property
装饰器,所以这里建议的 @classproperty
之类的东西也应该是可行的。
一边
(你可能已经知道了)
对于 类 上的属性:
- Using property() on classmethods
- How to make a class property?
我设法通过将项目类型提示为 None
:
创建了一个肮脏的 hack
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")
我宁愿避免使用这样的代码,因为由于循环导入问题(因此 None
),我实际上无法导入应该进行类型提示的项目,但它会欺骗 pylint
嗯
我制作了一个 classproperty
描述符,每当我使用用它装饰的函数时,我都会遇到多个 pylint
检查错误。
这是一个示例 class,其中包含一个示例装饰函数:
class Bar:
"""
Bar documentation.
"""
# pylint: disable=no-method-argument
@classproperty
def foo():
"""
Retrieve foo.
"""
return "foo"
感谢描述符,我可以调用 Bar.foo
并获取字符串 foo returned.
不幸的是,每当我将这样的函数用于稍微复杂的项目(例如 return 对象实例的函数)时,pylint
开始抱怨诸如 no-member
或 unexpected-keyword-arg
,只是因为它认为 Bar.foo
是一个方法,而不是包装的 classproperty
对象。
我想禁用任何代码的警告,使用我的功能 - 我绝对不能允许必须写# pylint: disable
每次我使用 classproperty
-wrapped 方法。我怎样才能用 pylint
做到这一点?或者也许我应该改用其他 linter?
以下是由于上述原因生成的警告示例:
class Bar:
"""
Bar documentation.
"""
# pylint: disable=no-method-argument
@classproperty
def foo():
"""
Retrieve an object.
"""
return NotImplementedError("Argument")
print(Bar.foo.args)
pylint
抱怨 E1101: Method 'foo' has no 'args' member (no-member)
(即使我知道它肯定有),我想完全禁用任何使用 Bar.foo.args
的 module/class/function 的一些警告或类似的。
对于任何感兴趣的人,这里是 classproperty
描述符的最小实现:
class classproperty:
"""
Minimal descriptor.
"""
# pylint: disable=invalid-name
def __init__(self, func):
self._func = func
def __get__(self, _obj, _type):
return self._func()
据我所知,这是不可能的。
我还没有在 pylint 的配置中找到解决这个问题的方法。我能找到的最接近的是 property-classes
选项,但它只影响 invalid-name
检查器,所以不是我们在这里寻找的:
:property-classes:
List of decorators that produce properties, such as abc.abstractproperty. Add
to this list to register other decorators that produce valid properties.
These decorators are taken in consideration only for invalid-name.
Default: ``abc.abstractproperty``
也许这是一个值得直接问 pylint's developers 的问题。
在我看来,这似乎可以通过 transform plugin (Maybe this 获得灵感来解决?)。 Pylint 可以很好地处理 @property
装饰器,所以这里建议的 @classproperty
之类的东西也应该是可行的。
一边
(你可能已经知道了)
对于 类 上的属性:
- Using property() on classmethods
- How to make a class property?
我设法通过将项目类型提示为 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")
我宁愿避免使用这样的代码,因为由于循环导入问题(因此 None
),我实际上无法导入应该进行类型提示的项目,但它会欺骗 pylint
嗯