Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mypy_django_plugin/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def get_class_fullname(klass: type) -> str:
def get_call_argument_by_name(ctx: Union[FunctionContext, MethodContext], name: str) -> Optional[Expression]:
"""
Return the expression for the specific argument.
This helper should only be used with non-star arguments.
"""
# first check for named arg on function definition
if name in ctx.callee_arg_names:
Expand Down
42 changes: 42 additions & 0 deletions tests/typecheck/managers/querysets/test_custom_queryset.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
- case: test_custom_queryset_with_passthrough_values_list
main: |
from typing import Any, TypeVar, Self
from django.db.models.base import Model
from django.db.models.query import QuerySet
from myapp.models import MyUser

_Model = TypeVar("_Model", bound=Model, covariant=True)

class CustomQuerySet(QuerySet[_Model]):
def values_list(self, *args: Any, **kwargs: Any) -> QuerySet[_Model]:
return super().values_list(*args, **kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stupid question: what will happen if we do kwargs.pop('flat'); super().values_list(*args, flat=True, **kwargs)?

Copy link
Contributor Author

@md384 md384 Apr 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flat=True doesn't change the return type of the get call in the tests. This seems like a separate bug and is beyond my knowledge of mypy or mypy plugins -there doesn't seem to be anything usable in the context.


qs = CustomQuerySet[MyUser](model=MyUser)

# checking that the CustomQuerySet returns same types as MyUser's qs when using "flat" and "named" args which use
# "get_call_argument_by_name" helper function in plugin
reveal_type(MyUser.objects.values_list('name').get()) # N: Revealed type is "Tuple[builtins.str]"
reveal_type(qs.values_list('name').get()) # N: Revealed type is "Tuple[builtins.str]"

reveal_type(MyUser.objects.values_list('name', flat=True).get()) # N: Revealed type is "builtins.str"
reveal_type(qs.values_list('name', flat=True).get()) # N: Revealed type is "builtins.str"

reveal_type(MyUser.objects.values_list('name', named=True).get()) # N: Revealed type is "Tuple[builtins.str, fallback=main.Row]"
reveal_type(qs.values_list('name', named=True).get()) # N: Revealed type is "Tuple[builtins.str, fallback=main.Row1]"

reveal_type(MyUser.objects.values_list('name', flat=True, named=True).get())
reveal_type(qs.values_list('name', flat=True, named=True).get())
out: |
main:25: error: 'flat' and 'named' can't be used together [misc]
main:25: note: Revealed type is "Any"
main:26: error: 'flat' and 'named' can't be used together [misc]
main:26: note: Revealed type is "Any"
installed_apps:
- myapp
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from django.db import models
class MyUser(models.Model):
name = models.CharField(max_length=100)