-
-
Notifications
You must be signed in to change notification settings - Fork 515
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 1 commit
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 |
|---|---|---|
|
|
@@ -173,16 +173,22 @@ 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. | ||
| """ | ||
| 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]: | ||
|
||
| 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) |
Uh oh!
There was an error while loading. Please reload this page.