Skip to content

Commit abd1164

Browse files
committed
Suppress not-an-iterable message for 'objects'. Fixes #117
1 parent 555e168 commit abd1164

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

pylint_django/augmentations/__init__.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pylint.checkers.newstyle import NewStyleConflictChecker
1212
from pylint.checkers.variables import VariablesChecker
1313
from pylint.__pkginfo__ import numversion as PYLINT_VERSION
14-
from pylint.checkers.typecheck import TypeChecker
14+
from pylint.checkers.typecheck import IterableChecker, TypeChecker
1515
from pylint.checkers.variables import ScopeConsumer
1616

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

464464

465+
def is_model_manager(node):
466+
"""Checks that node is an attribute named `objects` which usually represents
467+
an iterator. This function fixes issues #117.
468+
"""
469+
try:
470+
# try to get the model manager node, i.e. 'objects'
471+
manager = node.func.expr
472+
except: # noqa: E722, pylint: disable=bare-except
473+
return False
474+
475+
# parents = (
476+
# 'django.db.models.manager.Manager',
477+
# '.Manager',
478+
# 'django.db.models.query.QuerySet',
479+
# '.QuerySet',
480+
# 'model_utils.managers.InheritanceManager',
481+
# 'model_utils.managers.QueryManager',
482+
# 'model_utils.managers.SoftDeletableManager',
483+
# )
484+
# TODO: properly filter based on class instead of the property name
485+
# return _attribute_is_magic(manager, ['objects'], parents)
486+
return isinstance(manager, Attribute) and manager.attrname == 'objects'
487+
488+
465489
def is_admin_attribute(node):
466490
"""Checks that node is attribute of BaseModelAdmin."""
467491
parents = ('django.contrib.admin.options.BaseModelAdmin',
@@ -795,3 +819,6 @@ def apply_augmentations(linter):
795819
VariablesChecker.leave_module = wrap(current_leave_module, ignore_import_warnings_for_related_fields)
796820
# VariablesChecker.leave_module is now wrapped
797821
# else VariablesChecker.leave_module is already wrapped
822+
823+
# supress not-an-iterable for model_utils.managers. See #117
824+
suppress_message(linter, IterableChecker._check_iterable, 'not-an-iterable', is_model_manager)

0 commit comments

Comments
 (0)