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

walk mro for Field set type annotations #2244

Merged
merged 1 commit into from
Jul 3, 2024
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
2 changes: 1 addition & 1 deletion mypy_django_plugin/django/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def get_expected_types(self, api: TypeChecker, model_cls: Type[Model], *, method
def get_field_set_type_from_model_type_info(info: Optional[TypeInfo], field_name: str) -> Optional[MypyType]:
if info is None:
return None
field_node = info.names.get(field_name)
field_node = info.get(field_name)
if field_node is None or not isinstance(field_node.type, Instance):
return None
elif not field_node.type.args:
Expand Down
21 changes: 21 additions & 0 deletions tests/typecheck/models/test_create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@
class Child4(Child1):
value4 = models.IntegerField()

- case: mro_is_walked_for_field_annotations
main: |
from myapp.models import Child
Child.objects.create(dct={"hello": "world"}) # no errors
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add:

c: Child
reveal_type(c.dct)

?

This is needed to prevent a possible regression that dct is Any.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's relevant / useful -- the type for dct is explicitly annotated. the bug here is strictly in the plugin parts of create

installed_apps:
- myapp
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from __future__ import annotations

from django.db import models

class JSONField(models.TextField): pass # incomplete

class Base(models.Model):
dct: models.Field[dict[str, str], dict[str, str]] = JSONField()

class Child(Base): pass

- case: optional_id_fields_for_create_is_error_if_not_autofield
main: |
from myapp.models import Publisher, Book
Expand Down
Loading