python - 在 Django 中使用 Pylint

标签 python django static-analysis pylint

我很想整合pylint进入构建过程 我的 python 项目,但我遇到了一个阻碍:其中一个 我发现非常有用的错误类型--:E1101: *%s %r has no %r member*--使用常见的django字段时经常报错, 例如:

E1101:125:get_user_tags: Class 'Tag' has no 'objects' member

这是由这段代码引起的:

def get_user_tags(username):
   """
   Gets all the tags that username has used.

   Returns a query set.
   """
   return Tag.objects.filter(  ## This line triggers the error.
       tagownership__users__username__exact=username).distinct()

# Here is the Tag class, models.Model is provided by Django:
class Tag(models.Model):
   """
   Model for user-defined strings that help categorize Events on
   on a per-user basis.
   """
   name = models.CharField(max_length=500, null=False, unique=True)

   def __unicode__(self):
       return self.name

如何调整 Pylint 以正确考虑对象等字段? (我还查看了 Django 源代码,但我一直无法找到 objects 的实现,所以我怀疑它不“只是”一个类字段。另一方面,我我对 python 还很陌生,所以我很可能忽略了一些东西。)

编辑: 我发现告诉 pylint 不对这些警告发出警告的唯一方法是阻止 (E1101) 类型的所有错误,这是 Not Acceptable 解决方案,因为那是 (在我看来)一个非常有用的错误。如果有另一种方法,而不增加 pylint 源,请指出具体细节:)

here总结一下我在使用 pycheckerpyflakes 时遇到的问题——事实证明,它们对于一般用途来说远非不稳定。 (在 pychecker 的情况下,崩溃源于 pychecker 代码——而不是它正在加载/调用的源代码。)

最佳答案

不要通过添加 ignoresgenerated-members 来禁用或削弱 Pylint 功能。
使用积极开发的 理解 Django 的 Pylint 插件。
This Pylint plugin for Django效果很好:

pip install pylint-django

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

--load-plugins pylint_django

详细博文here .

关于python - 在 Django 中使用 Pylint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/115977/

相关文章:

python - TensorFlow DCGAN 模型 : stability and convergence problems

django cookie 中没有 csrftoken

jenkins - 如何跟踪 Jenkins 构建之间的值(静态分析)

python - 如何退出正在运行线程的Python程序?

python - 使用 python 3 使用 PyQt4 QWebView 查看 map

Django - 通用 View 子类 - url 参数

c++ - 使用了危险的魔数(Magic Number) N

java - Findbugs 注释 - 我部署的代码中是否需要 annotation.jar 和 jsr305.jar?

python PIL : IOError: cannot identify image file

django - 如果 DEBUG 为 True,也启用 Django 日志记录