Skip to content

Commit

Permalink
Don't remove objects attribute from Model in plugin
Browse files Browse the repository at this point in the history
Partially reverts #1672
  • Loading branch information
flaeppe committed Jul 26, 2024
1 parent a28717d commit a60eaf1
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 12 deletions.
3 changes: 1 addition & 2 deletions django-stubs/db/models/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
6 changes: 0 additions & 6 deletions mypy_django_plugin/transformers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion tests/typecheck/models/test_abstract.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tests/typecheck/models/test_contrib_models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand All @@ -56,7 +56,7 @@
...
class MyBaseUser(AbstractBaseUser):
objects = MyBaseUserManager()
objects: ClassVar[MyBaseUserManager] = MyBaseUserManager()
class MyUserManager(UserManager["MyUser"]):
...
Expand Down
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 @@ -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:
Expand Down

0 comments on commit a60eaf1

Please sign in to comment.