使用django时安静pylint误报

时间:2022-01-04 04:07:30

I'd like to sanely quiet a few pylint errors when using Django. The two that are causing the greatest irritation are when deriving from django.db.models.Model and accessing objects, and django.test.TestCase. In the first, pylint complains about any code that uses the attribute 'objects', saying it isn't a member. In the second, after adding seven tests to a test case, it complains about too many public methods (I'm guessing that TestCase has fourteen)

在使用Django时,我想要保持一些pylint错误。导致最大刺激的两个是从django.db.models.Model派生并访问对象和django.test.TestCase。在第一个中,pylint抱怨任何使用属性'objects'的代码,说它不是成员。在第二个,在对测试用例添加七个测试之后,它抱怨太多的公共方法(我猜测TestCase有十四个)

I know the first part of this is a duplicate of question 115977, but that question is a little old and none of the solutions are very good so I thought I'd poke the issue.

我知道这的第一部分是问题115977的重复,但是这个问题有点陈旧,没有一个解决方案非常好,所以我想我会戳这个问题。

I don't want to simply suppress the complaints in pylint, as I like to see them in other circumstances.

我不想简单地压制pylint中的抱怨,因为我喜欢在其他情况下看到它们。

3 个解决方案

#1


6  

Easiest, provided your problematic code is not out of your control (e.g. autogenerated), is to disable the complaints in the areas you know they're spurious. Copying an example straight out of the message that first introduced this solution:

最容易的,只要您的问题代码不受您的控制(例如自动生成),就是禁用您知道它们是虚假的区域的投诉。直接从首次引入此解决方案的消息中复制示例:

1  class foo:
2    # pylint: disable=W1234
3    def bar(self):
4      # pylint: disable=W4321
5      pass
6    def gnurz(self):
7      pass

#2


4  

if you do not care some pylint's warnings, like unexistent member(E1101) and too many public methods(R0904), you can easily close it with:

如果您不关心某些pylint的警告,例如不存在的成员(E1101)和太多公共方法(R0904),您可以轻松地将其关闭:

pylint --disable=E1101,R0904

if you are interested with few checkers only, you can run pylint like this:

如果您只对几个检查器感兴趣,可以像这样运行pylint:

pylint --enable=basic,variables,classes,design,imports,newstyle,exceptions,format,miscellaneous,metrics,similarities

#3


4  

I don't like repeating myself, but here is an answer that actually works: https://*.com/a/31000713/78234
From the answer: Do not disable or weaken Pylint functionality by adding ignores or generated-members.
Use an actively developed Pylint plugin that understands Django.
This Pylint plugin for Django works quite well:

我不喜欢重复自己,但这里有一个实际可行的答案:https://*.com/a/31000713/78234答案:不要通过添加ignores或generated-members来禁用或削弱Pylint功能。使用一个了解Django的积极开发的Pylint插件。这个Django的Pylint插件工作得很好:

pip install pylint-django

and when running pylint add the following flag to the command:

并且当运行pylint时,将以下标志添加到命令:

--load-plugins pylint_django

Detailed blog post here.

详细博客文章在这里。

#1


6  

Easiest, provided your problematic code is not out of your control (e.g. autogenerated), is to disable the complaints in the areas you know they're spurious. Copying an example straight out of the message that first introduced this solution:

最容易的,只要您的问题代码不受您的控制(例如自动生成),就是禁用您知道它们是虚假的区域的投诉。直接从首次引入此解决方案的消息中复制示例:

1  class foo:
2    # pylint: disable=W1234
3    def bar(self):
4      # pylint: disable=W4321
5      pass
6    def gnurz(self):
7      pass

#2


4  

if you do not care some pylint's warnings, like unexistent member(E1101) and too many public methods(R0904), you can easily close it with:

如果您不关心某些pylint的警告,例如不存在的成员(E1101)和太多公共方法(R0904),您可以轻松地将其关闭:

pylint --disable=E1101,R0904

if you are interested with few checkers only, you can run pylint like this:

如果您只对几个检查器感兴趣,可以像这样运行pylint:

pylint --enable=basic,variables,classes,design,imports,newstyle,exceptions,format,miscellaneous,metrics,similarities

#3


4  

I don't like repeating myself, but here is an answer that actually works: https://*.com/a/31000713/78234
From the answer: Do not disable or weaken Pylint functionality by adding ignores or generated-members.
Use an actively developed Pylint plugin that understands Django.
This Pylint plugin for Django works quite well:

我不喜欢重复自己,但这里有一个实际可行的答案:https://*.com/a/31000713/78234答案:不要通过添加ignores或generated-members来禁用或削弱Pylint功能。使用一个了解Django的积极开发的Pylint插件。这个Django的Pylint插件工作得很好:

pip install pylint-django

and when running pylint add the following flag to the command:

并且当运行pylint时,将以下标志添加到命令:

--load-plugins pylint_django

Detailed blog post here.

详细博客文章在这里。