Skip to content

Commit

Permalink
[refs #37 #40] Fix the infinite loop problem using an inference tip t…
Browse files Browse the repository at this point in the history
…o infer that fields are both their original subclass and also a type shim representing them at runtime. Also, supresses R0903 (too few methods) from models and views, which started happening recently
  • Loading branch information
carlio committed Jan 24, 2015
1 parent 653660d commit 768aff5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
8 changes: 8 additions & 0 deletions pylint_django/augmentations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ def apply_augmentations(linter):
suppress_message(linter, ClassChecker.visit_class, 'W0232', is_model_media_subclass)
suppress_message(linter, MisdesignChecker.leave_class, 'R0903', is_model_media_subclass)

# Too few public methods started appearing for Views and Models as part of Pylint>=1.4 / astroid>=1.3.3
# Not sure why, suspect this is a failure to get the parent classes somewhere
# For now, just suppress it on models and views
suppress_message(linter, MisdesignChecker.leave_class, 'R0903', is_class('django.db.models.base.Model'))
# TODO: why does this not work with the fqn of 'View'? Must be something to do with the overriding and transforms
suppress_message(linter, MisdesignChecker.leave_class, 'R0903', is_class('.View'))


# Admin
# Too many public methods (40+/20)
# TODO: Count public methods of django.contrib.admin.options.ModelAdmin and increase
Expand Down
17 changes: 10 additions & 7 deletions pylint_django/transforms/fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from astroid import MANAGER, scoped_nodes, nodes
from astroid import MANAGER, scoped_nodes, nodes, inference_tip


_STR_FIELDS = ('CharField', 'SlugField', 'URLField', 'TextField', 'EmailField',
Expand All @@ -17,7 +17,12 @@ def is_form_field(cls):
return cls.qname().startswith('django.forms.fields')


def make_field_str(cls):
def is_model_or_form_field(cls):
return is_model_field(cls) or is_form_field(cls)


def apply_type_shim(cls, context=None):

if cls.name in _STR_FIELDS:
base_node = scoped_nodes.builtin_lookup('str')
elif cls.name in _INT_FIELDS:
Expand All @@ -35,12 +40,10 @@ def make_field_str(cls):
elif cls.name == 'DateField':
base_node = MANAGER.ast_from_module_name('datetime').lookup('date')
else:
return cls
return iter([cls])

cls.bases.append(base_node[1][0])
return cls
return iter([cls] + base_node[1])


def add_transforms(manager):
manager.register_transform(nodes.Class, make_field_str, is_model_field)
manager.register_transform(nodes.Class, make_field_str, is_form_field)
manager.register_transform(nodes.Class, inference_tip(apply_type_shim), is_model_or_form_field)

0 comments on commit 768aff5

Please sign in to comment.