Skip to content

Commit

Permalink
Merge pull request #116 from yalef/master
Browse files Browse the repository at this point in the history
Add exception handling for model field checking
  • Loading branch information
rocioar committed Jul 31, 2023
2 parents a317220 + 72235ea commit 2c28a8c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
3 changes: 2 additions & 1 deletion flake8_django/checkers/model_content_order.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import astroid
from astroid.exceptions import InferenceError
from functools import partial

from .base_model_checker import BaseModelChecker
Expand Down Expand Up @@ -33,7 +34,7 @@ def is_field_declaration(node):
):
return True
return False
except AttributeError:
except (AttributeError, InferenceError):
return False


Expand Down
12 changes: 9 additions & 3 deletions flake8_django/checkers/model_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ def is_string_dunder_all(self, element):
"""
Return True if element is astroid.Const, astroid.List or astroid.Tuple and equals "__all__"
"""
if isinstance(element.value, (astroid.List, astroid.Tuple)):
assign_value = element.value
if not isinstance(
assign_value,
(astroid.List, astroid.Tuple, astroid.Const),
):
return False
if isinstance(assign_value, (astroid.List, astroid.Tuple)):
return any(
iter_item.value == '__all__'
for iter_item in element.value.itered()
for iter_item in assign_value.itered()
)
else:
node_value = element.value.value
node_value = assign_value.value
if isinstance(node_value, bytes):
node_value = node_value.decode()
return node_value == '__all__'
Expand Down
24 changes: 24 additions & 0 deletions tests/fixtures/model_content_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,27 @@ def my_method(self):
@property
def random_property(self):
return '%s' % self


class CustomManager(models.Manager):
def manager_only_method(self):
return


class CustomQuerySet(models.QuerySet):
def manager_and_queryset_method(self):
return


class ModelWithCustomManager(models.Model):
"""
Model with custom manager which can't be inferred correctly.
"""
first_name = models.CharField(max_length=32)
objects = CustomManager.from_queryset(CustomQuerySet)()
class Meta:
verbose_name = 'test'
verbose_name_plural = 'tests'

def __str__(self):
return 'Perfectly fine!'
5 changes: 5 additions & 0 deletions tests/fixtures/model_form_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ class Meta:

def test_method_doesnt_error(self):
pass


class ExtendedUser(User):
class Meta(User.Meta):
fields = User.Meta.fields + ('email',)
1 change: 0 additions & 1 deletion tests/fixtures/model_form_fields_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ class User5(ModelForm):
class Meta:
model = User2
fields = ['__all__']

0 comments on commit 2c28a8c

Please sign in to comment.