我非常希望将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)的所有错误,这不是一个可接受的解决方案,因为(在我看来)这是一个非常有用的错误。如果有其他方法,而不增加pylint源代码,请告诉我细节:)
有关我在pychecker
和pyflakes
中遇到的问题的摘要,请参阅here --事实证明,对于一般用途来说,它们太不稳定了。(在pychecker的例子中,崩溃源于pychecker代码--而不是它正在加载/调用的源代码。)
发布于 2015-06-23 18:47:46
请勿通过添加ignores
或generated-members
来禁用或削弱Pylint功能。
使用一个能够理解 Django的活跃开发的Pylint插件。
This Pylint plugin for Django运行得很好:
pip install pylint-django
在运行pylint时,将以下标志添加到命令中:
--load-plugins pylint_django
详细的博客帖子here。
发布于 2009-09-12 22:21:47
我使用以下代码:pylint --generated-members=objects
发布于 2017-12-12 22:09:20
如果您使用Visual Studio代码,请执行以下操作:
pip install pylint-django
并添加到VSC配置:
"python.linting.pylintArgs": [
"--load-plugins=pylint_django"
],
https://stackoverflow.com/questions/115977
复制相似问题