Skip to content

Commit

Permalink
try and use named arguments from caller for matching name (#2294)
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile authored Jul 30, 2024
1 parent 9b50861 commit 7b7ddae
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mypy_django_plugin/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from mypy.nodes import (
GDEF,
MDEF,
ArgKind,
AssignmentStmt,
Block,
ClassDef,
Expand Down Expand Up @@ -182,6 +183,12 @@ 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.
"""
# try and pull the named argument from the caller first
for kinds, argnames, args in zip(ctx.arg_kinds, ctx.arg_names, ctx.args):
for kind, argname, arg in zip(kinds, argnames, args):
if kind == ArgKind.ARG_NAMED and argname == name:
return arg

if name not in ctx.callee_arg_names:
return None
idx = ctx.callee_arg_names.index(name)
Expand Down
25 changes: 25 additions & 0 deletions tests/typecheck/fields/test_related.yml
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,31 @@
class User(AbstractUser):
pass
- case: nullable_foreign_key_with_init_overridden
main: |
from myapp.models import A
reveal_type(A.objects.get().b) # N: Revealed type is "Union[myapp.models.B, None]"
installed_apps:
- myapp
files:
- path: myapp/__init__.py
- path: myapp/models.py
content: |
from typing import Any, TypeVar
from django.db import models
_ST = TypeVar("_ST", contravariant=True)
_GT = TypeVar("_GT", covariant=True)
class FK(models.ForeignKey[_ST, _GT]):
def __init__(self, *args: Any, **kwargs: Any) -> None:
kwargs.setdefault('on_delete', models.CASCADE)
super().__init__(*args, **kwargs)
class B(models.Model): ...
class A(models.Model):
b = FK(B, null=True, on_delete=models.CASCADE)
- case: related_manager_is_a_subclass_of_default_manager
main: |
Expand Down

0 comments on commit 7b7ddae

Please sign in to comment.