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

select_related changes the manager class #728

Open
canassa opened this issue Oct 14, 2021 · 0 comments
Open

select_related changes the manager class #728

canassa opened this issue Oct 14, 2021 · 0 comments
Labels
bug Something isn't working

Comments

@canassa
Copy link
Contributor

canassa commented Oct 14, 2021

Bug report

What's wrong

select_related breaks custom managers.

Take the following example:

class TransactionQuerySet(models.QuerySet):
    def with_status(self):
        return self.filter(status=...)


class TransactionManager(models.Manager):

    def get_queryset(self) -> TransactionQuerySet:
        return TransactionQuerySet(self.model, using=self._db)

    def with_status(self):
        return self.get_queryset().with_status()

class Transaction(models.Model):
    ...
    objects = TransactionManager()

The following code has no type errors:

Transaction.objects.with_status()

But the following fails:

Transaction.objects.select_related().with_status()

error: "_QuerySet[Transaction, Transaction]" has no attribute "with_status"

The revealed types:

reveal_type(Transaction.objects)
reveal_type(Transaction.objects.select_related())

note: Revealed type is "core.core.models.Transaction_TransactionManager5[core.core.models.Transaction]"
note: Revealed type is "django.db.models.query._QuerySet[core.core.models.Transaction*, core.core.models.Transaction*]"

This problem started after I updated django-stubs from 1.8.0 to 1.9.0

System information

  • OS:
  • python version: 3.9.7
  • django version: 3.2.8
  • mypy version: 0.910
  • django-stubs version: 1.9.0
  • django-stubs-ext version: Not using it
@canassa canassa added the bug Something isn't working label Oct 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

1 participant