Skip to content

Commit

Permalink
workaround to support custom Managers in Django 1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
smirolo committed Mar 8, 2015
1 parent 42d5b5c commit fa032b6
Showing 1 changed file with 43 additions and 43 deletions.
86 changes: 43 additions & 43 deletions pylint_django/augmentations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,55 +63,53 @@ class ModelB(models.Model):
warn.
"""
quack = False

if node.attrname.endswith('_set'):
# Note: it would have been nice to import the Manager object from Django and
# get its attributes that way - and this used to be the method - but unfortunately
# there's no guarantee that Django is properly configured at that stage, and importing
# anything from the django.db package causes an ImproperlyConfigured exception.
# Therefore we'll fall back on a hard-coded list of attributes which won't be as accurate,
# but this is not 100% accurate anyway.
manager_attrs = (
'none',
'all',
'count',
'dates',
'distinct',
'extra',
'get',
'get_or_create',
'create',
'bulk_create',
'filter',
'aggregate',
'annotate',
'complex_filter',
'exclude',
'in_bulk',
'iterator',
'latest',
'order_by',
'select_for_update',
'select_related',
'prefetch_related',
'values',
'values_list',
'update',
'reverse',
'defer',
'only',
'using',
'exists',
)

if node.attrname in manager_attrs or node.attrname.endswith('_set'):
# if this is a X_set method, that's a pretty strong signal that this is the default
# Django name, rather than one set by related_name
quack = True
else:
# we will
if isinstance(node.parent, Getattr):
func_name = getattr(node.parent, 'attrname', None)

# Note: it would have been nice to import the Manager object from Django and
# get its attributes that way - and this used to be the method - but unfortunately
# there's no guarantee that Django is properly configured at that stage, and importing
# anything from the django.db package causes an ImproperlyConfigured exception.
# Therefore we'll fall back on a hard-coded list of attributes which won't be as accurate,
# but this is not 100% accurate anyway.
manager_attrs = (
'none',
'all',
'count',
'dates',
'distinct',
'extra',
'get',
'get_or_create',
'create',
'bulk_create',
'filter',
'aggregate',
'annotate',
'complex_filter',
'exclude',
'in_bulk',
'iterator',
'latest',
'order_by',
'select_for_update',
'select_related',
'prefetch_related',
'values',
'values_list',
'update',
'reverse',
'defer',
'only',
'using',
'exists',
)

if func_name in manager_attrs:
quack = True

Expand All @@ -124,7 +122,9 @@ class ModelB(models.Model):
pass
else:
for cls in inferred:
if node_is_subclass(cls, 'django.db.models.base.Model'):
if (node_is_subclass(
cls, 'django.db.models.manager.Manager')
or node_is_subclass(cls, 'django.db.models.base.Model')):
# This means that we are looking at a subclass of models.Model
# and something is trying to access a <something>_set attribute.
# Since this could exist, we will return so as not to raise an
Expand Down

0 comments on commit fa032b6

Please sign in to comment.