Skip to content

Commit

Permalink
Add transformations for model_utils.InheritanceManager. Fix #160
Browse files Browse the repository at this point in the history
instead of trying to special-case this like we did before I've
added transformation definitions which allow pylint to properly
infer the types of the overriden object managers and know that
they are iterators.
  • Loading branch information
atodorov committed May 25, 2018
1 parent a82a6f9 commit b15a3d7
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 deletions.
27 changes: 0 additions & 27 deletions pylint_django/augmentations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,30 +481,6 @@ 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 @@ -855,8 +831,5 @@ def apply_augmentations(linter):
# 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)

# wsgi.py
suppress_message(linter, _visit_assignname(NameChecker), 'invalid-name', is_wsgi_application)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# with a manager from the django-model-utils package pylint-django
# does not report not-an-iterator error, see
# https://github.com/PyCQA/pylint-django/issues/117
# pylint: disable=missing-docstring
# pylint: disable=missing-docstring, invalid-name

from model_utils.managers import InheritanceManager

Expand All @@ -25,3 +25,12 @@ def function():

for record in BuggyModel.objects.all():
print(record.name)


class Unit(models.Model):
sim = models.CharField(max_length=64, null=True, blank=True)
objects = InheritanceManager()


installed_units = Unit.objects.filter(sim__isnull=False)
ids = [u.sim for u in installed_units]
4 changes: 4 additions & 0 deletions pylint_django/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@ def set_fake_locals(module):
'Manager')
_add_transform('django.utils.translation', 'ugettext_lazy')
_add_transform('mongoengine', 'Document')
_add_transform('model_utils.managers',
'InheritanceManager',
'QueryManager',
'SoftDeletableManager')
# register transform for FileField/ImageField, see #60
_add_transform('django.db.models.fields.files', 'FileField')
Empty file.
2 changes: 1 addition & 1 deletion pylint_django/transforms/transforms/django_db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Manager(object):
get_or_create = __noop
create = __noop
bulk_create = __noop
filter = __noop
filter = __noop_list
aggregate = __noop
annotate = __noop
complex_filter = __noop
Expand Down
11 changes: 11 additions & 0 deletions pylint_django/transforms/transforms/model_utils_managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django_db_models import Manager

class InheritanceManager(Manager):
pass


class QueryManager(Manager):
pass

class SoftDeletableManager(Manager):
pass

0 comments on commit b15a3d7

Please sign in to comment.