我正在尝试将 pylint 集成到使用 Django (1.6.1) 的本地项目中,并且我让它与 Pylint 0.27.0 一起使用,但现在我已经更新到最新的 1.2.1 一些新错误是突然出现,我似乎无法让它们消失。
这是错误的性质:
from django.db import models
class UserData(models.Model):
# data...
fieldA = models.IntegerField(default=0)
fieldB = models.IntegerField(default=0)
# ...
x = UserData(fieldA=1, fieldB=2)
# The above line of code generates errors:
# Unexpected keyword argument 'fieldA' in constructor call (unexpected-keyword-arg)
# Unexpected keyword argument 'fieldB' in constructor call (unexpected-keyword-arg)
# No value for argument 'name' in constructor call (no-value-for-parameter)
# No value for argument 'bases' in constructor call (no-value-for-parameter)
# No value for argument 'attrs' in constructor call (no-value-for-parameter)
我尝试通过在 pylint 检查期间编辑文件来解决此问题,使用如下内容:
UserData.__init__ = lambda self, *args, **kwargs: None
但是 Pylint 仍然打印出相同的错误。我也尝试直接将构造函数调用添加到 UserData 对象,但仍然没有成功。
有什么方法可以修改代码或 pylint 设置来消除这些错误吗?最好不要隐藏整个项目的这些错误。
最佳答案
您可以通过appending a specific rule在模块级别禁用这些错误。在模块顶部的评论中:
# pylint: disable=unexpected-keyword-arg, no-value-for-parameter
from django.db import models
class UserData(models.Model):
...
可以使用错误的符号名称(而不是错误号)starting with version 0.25.3 .
看来 pylint 的这个特定问题是由于 a commit from April 2014 造成的。该问题已被审稿人指出,有other users也受到此影响,但似乎尚未修复。
本质上,当 pylint 分析可调用调用时,它会根据可调用类型检查不同的参数。不幸的是,如果可调用是一个类,那么它首先检查 __new__
(在某些情况下,它可能存在于父元类中),如果找到,则完全忽略 __init__
>。这就是为什么您收到的错误提到 name
、bases
、attrs
,例如它们是 __new__
的参数。
关于python - 使用 Django 时如何隐藏 Pylint 1.2.1 错误输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24130248/