-
-
Notifications
You must be signed in to change notification settings - Fork 490
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 3 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 |
---|---|---|
|
@@ -175,14 +175,21 @@ def get_call_argument_by_name(ctx: Union[FunctionContext, MethodContext], name: | |
Return the expression for the specific argument. | ||
This helper should only be used with non-star arguments. | ||
""" | ||
if name not in ctx.callee_arg_names: | ||
return None | ||
idx = ctx.callee_arg_names.index(name) | ||
args = ctx.args[idx] | ||
if len(args) != 1: | ||
# Either an error or no value passed. | ||
return None | ||
return args[0] | ||
# first check for named arg on function definition | ||
if name in ctx.callee_arg_names: | ||
idx = ctx.callee_arg_names.index(name) | ||
args = ctx.args[idx] | ||
if len(args) != 1: | ||
# Either an error or no value passed. | ||
return None | ||
return args[0] | ||
|
||
# check for named arg in function call keyword args | ||
intgr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if name in ctx.arg_names[1]: | ||
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. I don't think usage of For example if I instead declare my field like: class SecondField(fields.IntegerField[_ST, _GT]):
def __init__(self, *args: Any, blank: bool = True, **kwargs: Any) -> None:
super().__init__(*args, blank=blank, **kwargs) And call it with:
[[], ['blank'], ['null']] Which results in a mismatch, resulting in the issue we're trying to solve. With the above in mind. If I instead then do: class ThirdField(fields.IntegerField[_ST, _GT]):
def __init__(self) -> None:
super().__init__() And call it like: 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. I think we can do something along the lines of # check for named arg in function call keyword args
for idx, arg_group in enumerate(ctx.arg_names):
if name in arg_group:
sub_idx = arg_group.index(name)
return ctx.args[idx][sub_idx] to handle these cases. |
||
idx = ctx.arg_names[1].index(name) | ||
return ctx.args[1][idx] | ||
|
||
return None | ||
|
||
|
||
def get_call_argument_type_by_name(ctx: Union[FunctionContext, MethodContext], name: str) -> Optional[MypyType]: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
- 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_field) # N: Revealed type is "Union[builtins.int, None]" | ||
monkeypatch: true | ||
installed_apps: | ||
- myapp | ||
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 User(models.Model): | ||
id = models.AutoField(primary_key=True) | ||
my_custom_field = MyIntegerField(null=True) |
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) | ||
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.