-
-
Notifications
You must be signed in to change notification settings - Fork 454
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
Fallback to checking kwargs of function call when looking for arguments of a call #2044
base: master
Are you sure you want to change the base?
Fallback to checking kwargs of function call when looking for arguments of a call #2044
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have several usages of this function: https://github.com/search?q=repo%3Atypeddjango%2Fdjango-stubs%20get_call_argument_by_name&type=code
Can you please also test the QuerySet
one?
|
||
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 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)
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm suspecting there's more cases to cover for here.
mypy_django_plugin/lib/helpers.py
Outdated
return args[0] | ||
|
||
# check for named arg in function call keyword args | ||
if name in ctx.arg_names[1]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think usage of ctx.arg_names[1]
will work like this here.
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: SecondField(blank=True, null=True)
ctx.arg_names
could look like:
[[], ['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: ThirdField()
I get an IndexError
crash on ctx.arg_names[1]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
Did you see my comment: #2043 (comment)? |
Yes, but that means the developer has to know about these "magic" kwargs in a number of functions. IMO, the plugin should be able to handle the |
I disagree with that the plugin should try to resolve I can agree with that it might be tedious to declare any or all overridden arguments, especially since there are quite a few.. But we want the plugin to do as little as possible and fallback on mypy to do the rest. At least that's my opinion. @intgr or @sobolevn might have different thoughts here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm torn on this.
From a typing perspective, *args: Any, **kwargs: Any
is problematic, since it suppresses all typechecking of the call. You can write MyIntegerField(1, 2, 3, null=Ellipsis, no_such_arg=123)
and mypy won't bat an eyelid. From this perspective it seems consistent to also reject inspecting null=
if the function definition is vague.
On the other hand, this PR does seem like an improvement in making django-stubs less finnicky. The implementation makes sense and is self-contained in the helper function; other callers of this function may benefit from the changes as well.
If we can't think of any downsides of this approach, seems unfair to block it. So a weak vote for merging from me.
I have made things!
Related issues
Closes #2043