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

Wrong QuerySet method typing from Iterable[T] argument #1917

Open
trecouvr opened this issue Jan 23, 2024 · 0 comments
Open

Wrong QuerySet method typing from Iterable[T] argument #1917

trecouvr opened this issue Jan 23, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@trecouvr
Copy link

Bug report

What's wrong

When I share between multiple models a custom QuerySet with methods using Iterable[T] as arguments, the revealed typing is always the one of the first evaluated model.

I tried some alternatives like defining a specific queryset for each models, defining a specific manager for each models, using Manager.from_queryset, using QuerySet.as_manager, I always ends up with the same result. If someone could point me a workaround that would be great.

My specific use case involve overriding the bulk_create and bulk_update methods of the QuerySet to add some logging to it.

class BaseQuerySet(models.QuerySet[T], Generic[T]):
    def example_process_list(self, m: Collection[T]) -> None: ...
    def example_process_one(self, m: T) -> None: ...

class MyModel(models.Model):
    objects = BaseQuerySet.as_manager()

class MyModel2(models.Model):
    objects = BaseQuerySet.as_manager()

reveal_type(MyModel.objects.example_process_one) # Revealed type is "def (m: dj_stubs_demo.models.MyModel)"
reveal_type(MyModel2.objects.example_process_one) # Revealed type is "def (m: dj_stubs_demo.models.MyModel2)"
reveal_type(MyModel.objects.example_process_list) # Revealed type is "def (m: typing.Iterable[dj_stubs_demo.models.MyModel])"
reveal_type(MyModel2.objects.example_process_list) # Revealed type is "def (m: typing.Iterable[dj_stubs_demo.models.MyModel])"

How is that should be

reveal_type(MyModel.objects.example_process_one) # Revealed type is "def (m: dj_stubs_demo.models.MyModel)"
reveal_type(MyModel2.objects.example_process_one) # Revealed type is "def (m: dj_stubs_demo.models.MyModel2)"
reveal_type(MyModel.objects.example_process_list) # Revealed type is "def (m: typing.Iterable[dj_stubs_demo.models.MyModel])"
reveal_type(MyModel2.objects.example_process_list) # this one ---> Revealed type is "def (m: typing.Iterable[dj_stubs_demo.models.MyModel2])"

System information

  • OS: osx
  • python version: 3.11.3
  • django version: Django==4.2.9
  • mypy version: mypy==1.7.1
  • django-stubs version: django-stubs==4.2.7
  • django-stubs-ext version: django-stubs-ext==4.2.7
@trecouvr trecouvr added the bug Something isn't working label Jan 23, 2024
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