-
-
Notifications
You must be signed in to change notification settings - Fork 485
Fallback to checking kwargs of function call when looking for arguments of a call #2044
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
base: master
Are you sure you want to change the base?
Changes from all commits
5ae8419
5cd6f20
8b15253
31f6740
94858d7
f35b7f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
- case: test_custom_model_fields_with_passthrough_constructor | ||
main: | | ||
from myapp.models import User | ||
user = User() | ||
reveal_type(user.id) # N: Revealed type is "builtins.int" | ||
reveal_type(user.my_custom_field1) # N: Revealed type is "Union[builtins.int, None]" | ||
reveal_type(user.my_custom_field2) # N: Revealed type is "builtins.int" | ||
reveal_type(user.my_custom_field3) # N: Revealed type is "builtins.int" | ||
reveal_type(user.my_custom_field4) # N: Revealed type is "Union[builtins.int, None]" | ||
reveal_type(user.my_custom_field5) # N: Revealed type is "builtins.int" | ||
reveal_type(user.my_custom_field6) # N: Revealed type is "builtins.int" | ||
monkeypatch: true | ||
installed_apps: | ||
- myapp | ||
out: | | ||
myapp/models:15: error: "__init__" of "Field" gets multiple values for keyword argument "blank" [misc] | ||
files: | ||
- path: myapp/__init__.py | ||
- path: myapp/models.py | ||
content: | | ||
from django.db import models | ||
from django.db.models import fields | ||
|
||
from typing import Any, TypeVar | ||
|
||
_ST = TypeVar("_ST", contravariant=True) | ||
_GT = TypeVar("_GT", covariant=True) | ||
|
||
class MyIntegerField(fields.IntegerField[_ST, _GT]): | ||
def __init__(self, *args: Any, **kwargs: Any) -> None: | ||
super().__init__(*args, **kwargs) | ||
|
||
class MyIntegerFieldBlank(fields.IntegerField[_ST, _GT]): | ||
def __init__(self, *args: Any, blank: bool = True, **kwargs: Any) -> None: | ||
super().__init__(*args, blank=blank, **kwargs) | ||
|
||
class User(models.Model): | ||
id = models.AutoField(primary_key=True) | ||
my_custom_field1 = MyIntegerField(null=True) | ||
my_custom_field2 = MyIntegerField(null=False) | ||
my_custom_field3 = MyIntegerField() | ||
my_custom_field4 = MyIntegerFieldBlank(null=True) | ||
my_custom_field5 = MyIntegerFieldBlank(null=False) | ||
my_custom_field6 = MyIntegerFieldBlank() |
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 | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stupid question: what will happen if we do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
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) |
Uh oh!
There was an error while loading. Please reload this page.