Skip to content

Model.objects cannot be defined using QuerySet.as_manager #1713

@orsinium

Description

@orsinium

Bug report

What's wrong

class SubscriptionPromotion(BasePromotion):
    objects = SubscriptionPromotionQuerySet.as_manager()
error: Cannot override class variable (previously declared on base class "BasePromotion") with instance variable  [misc]
error: Incompatible types in assignment (expression has type "Manager[BasePromotion]", base class "BasePromotion" defined the type as "ManagerFromPromotionQuerySet[BasePromotion]")  [assignment]

How is that should be

The second error was there before but the first one is newly introduced in 4.2.4. As suggested in #1712 (comment), this code should be explicitly annotated. However, the problem is that if I annotate it like this:

objects: ClassVar[models.Manager[BasePromotion]

Then all custom attributes defined in the SubscriptionPromotionQuerySet won't be available in SubscriptionPromotion.objects.

In other words, before django-stubs-ext used to construct on the fly the return type of QuerySet.as_manager as a mix of Manager and custom methods but now I can't use it because I have to add explicit annotations, and there is no explicit annotation for this dynamically constructed type.

System information

  • OS: linux
  • python version: 3.8
  • django version: 4.2.4
  • mypy version: 1.5.1
  • django-stubs version: 4.2.4
  • django-stubs-ext version: 4.2.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions