Skip to content

Commit

Permalink
Suppress not-an-iterable message for 'objects'. Fixes #117
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov committed Apr 5, 2018
1 parent 555e168 commit abd1164
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion pylint_django/augmentations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pylint.checkers.newstyle import NewStyleConflictChecker
from pylint.checkers.variables import VariablesChecker
from pylint.__pkginfo__ import numversion as PYLINT_VERSION
from pylint.checkers.typecheck import TypeChecker
from pylint.checkers.typecheck import IterableChecker, TypeChecker
from pylint.checkers.variables import ScopeConsumer

from pylint_plugin_utils import augment_visit, suppress_message
Expand Down Expand Up @@ -462,6 +462,30 @@ def is_manager_attribute(node):
return _attribute_is_magic(node, MANAGER_ATTRS.union(QS_ATTRS), parents)


def is_model_manager(node):
"""Checks that node is an attribute named `objects` which usually represents
an iterator. This function fixes issues #117.
"""
try:
# try to get the model manager node, i.e. 'objects'
manager = node.func.expr
except: # noqa: E722, pylint: disable=bare-except
return False

# parents = (
# 'django.db.models.manager.Manager',
# '.Manager',
# 'django.db.models.query.QuerySet',
# '.QuerySet',
# 'model_utils.managers.InheritanceManager',
# 'model_utils.managers.QueryManager',
# 'model_utils.managers.SoftDeletableManager',
# )
# TODO: properly filter based on class instead of the property name
# return _attribute_is_magic(manager, ['objects'], parents)
return isinstance(manager, Attribute) and manager.attrname == 'objects'


def is_admin_attribute(node):
"""Checks that node is attribute of BaseModelAdmin."""
parents = ('django.contrib.admin.options.BaseModelAdmin',
Expand Down Expand Up @@ -795,3 +819,6 @@ def apply_augmentations(linter):
VariablesChecker.leave_module = wrap(current_leave_module, ignore_import_warnings_for_related_fields)
# VariablesChecker.leave_module is now wrapped
# else VariablesChecker.leave_module is already wrapped

# supress not-an-iterable for model_utils.managers. See #117
suppress_message(linter, IterableChecker._check_iterable, 'not-an-iterable', is_model_manager)

0 comments on commit abd1164

Please sign in to comment.