Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress FieldDoesNotExist raised from attribute on class definition #1329

Merged
merged 1 commit into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions mypy_django_plugin/transformers/fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import TYPE_CHECKING, Any, Optional, Tuple, Union, cast

from django.core.exceptions import FieldDoesNotExist
from django.db.models.fields import AutoField, Field
from django.db.models.fields.related import RelatedField
from django.db.models.fields.reverse_related import ForeignObjectRel
Expand Down Expand Up @@ -38,8 +39,10 @@ def _get_current_field_from_assignment(
if model_cls is None:
return None

current_field = model_cls._meta.get_field(field_name)
return current_field
try:
return model_cls._meta.get_field(field_name)
except FieldDoesNotExist:
return None


def reparametrize_related_field_type(related_field_type: Instance, set_type: MypyType, get_type: MypyType) -> Instance:
Expand Down
21 changes: 21 additions & 0 deletions tests/typecheck/fields/test_base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,24 @@
obj = MyModel()

reveal_type(obj.small) # N: Revealed type is "builtins.int"

- case: test_ignores_renamed_field
main: |
# Ref: https://github.com/typeddjango/django-stubs/issues/1261
# Django modifies the model so it doesn't have 'modelname', but we don't follow
# along. But the 'name=' argument to a field isn't a documented feature.
from myapp.models import RenamedField
instance = RenamedField()
reveal_type(instance.modelname) # N: Revealed type is "builtins.int"
instance.fieldname # E: "RenamedField" has no attribute "fieldname"
instance.modelname = 1
instance.fieldname = 1 # E: "RenamedField" has no attribute "fieldname"
installed_apps:
- myapp
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from django.db import models
class RenamedField(models.Model):
modelname = models.IntegerField(name="fieldname", choices=((1, 'One'),))