Skip to content

Custom queryset typing is lost #2602

@malo-malo

Description

@malo-malo

Bug report

What's wrong

When defining managers with a custom queryset, the typing of said queryset is lost. Instead of having MyQuerySet we have QuerySet[MyModel, MyModel].
This happens whether the manager is created manually, or using Manager.from_queryset(...)

from django.db import models


class MyQuerySet(models.QuerySet):
    pass


class MyManager(models.Manager):
    def get_queryset(self) -> MyQuerySet:
        return MyQuerySet(self.model, using=self._db)


class MyModel(models.Model):
    objects_my_manager = MyManager()
    objects_from_queryset = models.Manager.from_queryset(MyQuerySet)()


reveal_type(MyModel.objects_my_manager.all())  # Revealed type is "QuerySet[MyModel, MyModel]"
reveal_type(MyModel.objects_from_queryset.all())  # Revealed type is "QuerySet[MyModel, MyModel]"

How is that should be

I would like all manager methods that return a queryset (.all(), .filter(...), .only(...) ....) to return MyQuerySet.
We are currently using a workaround very similar to the one described here #863

System information

  • OS: Ubuntu 22.04.5 LTS
  • python version: 3.11.12
  • django version: 4.2.11
  • mypy version: 1.15.0
  • django-stubs version: 5.1.3
  • django-stubs-ext version: 5.1.3

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