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

Update Model._meta to ClassVar[Options[Self]] #1732

Merged
merged 1 commit into from
Sep 29, 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
2 changes: 1 addition & 1 deletion django-stubs/db/models/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Model(metaclass=ModelBase):
objects: ClassVar[Manager[Self]]

class Meta: ...
_meta: Options[Any]
_meta: ClassVar[Options[Self]]
pk: Any
_state: ModelState
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
Expand Down
8 changes: 0 additions & 8 deletions mypy_django_plugin/transformers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,13 +613,6 @@ def run_with_model_cls(self, model_cls: Type[Model]) -> None:
)


class AddMetaOptionsAttribute(ModelClassInitializer):
def run_with_model_cls(self, model_cls: Type[Model]) -> None:
if "_meta" not in self.model_classdef.info.names:
options_info = self.lookup_typeinfo_or_incomplete_defn_error(fullnames.OPTIONS_CLASS_FULLNAME)
self.add_new_node_to_model_class("_meta", Instance(options_info, [Instance(self.model_classdef.info, [])]))


class ProcessManyToManyFields(ModelClassInitializer):
"""
Processes 'ManyToManyField()' fields and generates any implicit through tables that
Expand Down Expand Up @@ -918,7 +911,6 @@ def process_model_class(ctx: ClassDefContext, django_context: DjangoContext) ->
AddDefaultManagerAttribute,
AddReverseLookups,
AddExtraFieldMethods,
AddMetaOptionsAttribute,
ProcessManyToManyFields,
MetaclassAdjustments,
]
Expand Down
29 changes: 29 additions & 0 deletions tests/typecheck/models/test_contrib_models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,32 @@
...
class MyUser(AbstractUser):
objects: ClassVar[MyUserManager] = MyUserManager()

- case: can_combine_permissions_mixin_and_abstract_base_user
main: |
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from myapp.models import AuthUser
reveal_type(AuthUser._meta)
reveal_type(AuthUser()._meta)
reveal_type(PermissionsMixin._meta)
reveal_type(AbstractBaseUser._meta)
out: |
main:4: note: Revealed type is "django.db.models.options.Options[myapp.models.AuthUser]"
main:5: note: Revealed type is "django.db.models.options.Options[myapp.models.AuthUser]"
main:6: note: Revealed type is "django.db.models.options.Options[django.contrib.auth.models.PermissionsMixin]"
main:7: note: Revealed type is "django.db.models.options.Options[django.contrib.auth.base_user.AbstractBaseUser]"
installed_apps:
- django.contrib.auth
- myapp
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin

class AuthUser(AbstractBaseUser, PermissionsMixin):
class Meta:
abstract = False
db_table = "auth_user"
2 changes: 1 addition & 1 deletion tests/typecheck/models/test_meta_options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from myapp.models import AbstractModel
class MyModel(AbstractModel):
pass
reveal_type(MyModel._meta.get_field('field')) # N: Revealed type is "Any"
reveal_type(MyModel._meta.get_field('field')) # N: Revealed type is "Union[django.db.models.fields.Field[Any, Any], django.db.models.fields.reverse_related.ForeignObjectRel, django.contrib.contenttypes.fields.GenericForeignKey]"
installed_apps:
- myapp
files:
Expand Down