|
11 | 11 | from pylint.checkers.newstyle import NewStyleConflictChecker
|
12 | 12 | from pylint.checkers.variables import VariablesChecker
|
13 | 13 | from pylint.__pkginfo__ import numversion as PYLINT_VERSION
|
14 |
| -from pylint.checkers.typecheck import TypeChecker |
| 14 | +from pylint.checkers.typecheck import IterableChecker, TypeChecker |
15 | 15 | from pylint.checkers.variables import ScopeConsumer
|
16 | 16 |
|
17 | 17 | from pylint_plugin_utils import augment_visit, suppress_message
|
@@ -462,6 +462,30 @@ def is_manager_attribute(node):
|
462 | 462 | return _attribute_is_magic(node, MANAGER_ATTRS.union(QS_ATTRS), parents)
|
463 | 463 |
|
464 | 464 |
|
| 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 | + |
465 | 489 | def is_admin_attribute(node):
|
466 | 490 | """Checks that node is attribute of BaseModelAdmin."""
|
467 | 491 | parents = ('django.contrib.admin.options.BaseModelAdmin',
|
@@ -795,3 +819,6 @@ def apply_augmentations(linter):
|
795 | 819 | VariablesChecker.leave_module = wrap(current_leave_module, ignore_import_warnings_for_related_fields)
|
796 | 820 | # VariablesChecker.leave_module is now wrapped
|
797 | 821 | # 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