diff --git a/django-stubs/db/models/base.pyi b/django-stubs/db/models/base.pyi index 38ac288ab..c529897b2 100644 --- a/django-stubs/db/models/base.pyi +++ b/django-stubs/db/models/base.pyi @@ -36,8 +36,7 @@ class Model(metaclass=ModelBase): # and re-add them to correct concrete subclasses of 'Model' DoesNotExist: Final[type[ObjectDoesNotExist]] MultipleObjectsReturned: Final[type[BaseMultipleObjectsReturned]] - # This 'objects' attribute will be deleted, via the plugin, in favor of managing it - # to only exist on subclasses it exists on during runtime. + objects: ClassVar[Manager[Self]] _meta: ClassVar[Options[Self]] diff --git a/mypy_django_plugin/transformers/models.py b/mypy_django_plugin/transformers/models.py index c85c87bf1..e77334cd5 100644 --- a/mypy_django_plugin/transformers/models.py +++ b/mypy_django_plugin/transformers/models.py @@ -960,12 +960,6 @@ def adjust_model_class(cls, ctx: ClassDefContext) -> None: ): del ctx.cls.info.names["MultipleObjectsReturned"] - objects = ctx.cls.info.names.get("objects") - if objects is not None and isinstance(objects.node, Var) and not objects.plugin_generated: - del ctx.cls.info.names["objects"] - - return - def get_exception_bases(self, name: str) -> List[Instance]: bases = [] for model_base in self.model_classdef.info.direct_base_classes(): diff --git a/tests/typecheck/models/test_abstract.yml b/tests/typecheck/models/test_abstract.yml index 446161a27..a7566d016 100644 --- a/tests/typecheck/models/test_abstract.yml +++ b/tests/typecheck/models/test_abstract.yml @@ -51,7 +51,7 @@ Recursive(parent=Recursive(parent=None)) Concrete(parent=Concrete(parent=None)) out: | - main:4: error: "Type[Recursive]" has no attribute "objects" [attr-defined] + main:4: error: Unexpected attribute "parent" for model "Recursive" [misc] main:5: error: Cannot instantiate abstract class "Recursive" with abstract attributes "DoesNotExist" and "MultipleObjectsReturned" [abstract] main:5: error: Unexpected attribute "parent" for model "Recursive" [misc] installed_apps: diff --git a/tests/typecheck/models/test_contrib_models.yml b/tests/typecheck/models/test_contrib_models.yml index ca7b14be0..388ce1aa0 100644 --- a/tests/typecheck/models/test_contrib_models.yml +++ b/tests/typecheck/models/test_contrib_models.yml @@ -38,7 +38,7 @@ - case: can_override_abstract_user_manager main: | from myapp.models import MyBaseUser, MyUser - reveal_type(MyBaseUser.objects) # N: Revealed type is "myapp.models.MyBaseUserManager[myapp.models.MyBaseUser]" + reveal_type(MyBaseUser.objects) # N: Revealed type is "myapp.models.MyBaseUserManager" reveal_type(MyBaseUser.objects.all()) # N: Revealed type is "django.db.models.query.QuerySet[myapp.models.MyBaseUser, myapp.models.MyBaseUser]" reveal_type(MyUser.objects) # N: Revealed type is "myapp.models.MyUserManager" reveal_type(MyUser.objects.all()) # N: Revealed type is "django.db.models.query.QuerySet[myapp.models.MyUser, myapp.models.MyUser]" @@ -56,7 +56,7 @@ ... class MyBaseUser(AbstractBaseUser): - objects = MyBaseUserManager() + objects: ClassVar[MyBaseUserManager] = MyBaseUserManager() class MyUserManager(UserManager["MyUser"]): ... diff --git a/tests/typecheck/models/test_meta_options.yml b/tests/typecheck/models/test_meta_options.yml index f6e2b604f..36c0aa234 100644 --- a/tests/typecheck/models/test_meta_options.yml +++ b/tests/typecheck/models/test_meta_options.yml @@ -81,7 +81,7 @@ # Errors: AbstractModel() # E: Cannot instantiate abstract class "AbstractModel" with abstract attributes "DoesNotExist" and "MultipleObjectsReturned" [abstract] - AbstractModel.objects.create() # E: "Type[AbstractModel]" has no attribute "objects" [attr-defined] + AbstractModel.objects.create() installed_apps: - myapp files: