Skip to content

Commit 1196336

Browse files
mkurnikovb0g3r
andauthored
Perform anal_type for arguments and return type when copying methods to another class (#279)
* Found the reproducible test case * fix import resolution for method copy * remove irrelevant parts from test * fix mypy errors Co-authored-by: Boger <[email protected]>
1 parent 665f4d8 commit 1196336

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

mypy_django_plugin/lib/helpers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,14 @@ def _prepare_new_method_arguments(node: FuncDef) -> Tuple[List[Argument], MypyTy
327327
def copy_method_to_another_class(ctx: ClassDefContext, self_type: Instance,
328328
new_method_name: str, method_node: FuncDef) -> None:
329329
arguments, return_type = _prepare_new_method_arguments(method_node)
330+
331+
semanal_api = get_semanal_api(ctx)
332+
for argument in arguments:
333+
if argument.type_annotation is not None:
334+
argument.type_annotation = semanal_api.anal_type(argument.type_annotation)
335+
if return_type is not None:
336+
return_type = semanal_api.anal_type(return_type) or AnyType(TypeOfAny.unannotated)
337+
330338
add_method(ctx,
331339
new_method_name,
332340
args=arguments,

test-data/typecheck/managers/querysets/test_from_queryset.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,32 @@
117117
NewManager = BaseManager.from_queryset(ModelQuerySet)
118118
class MyModel(models.Model):
119119
objects = NewManager()
120+
121+
- case: from_queryset_with_manager_in_another_directory_and_imports
122+
main: |
123+
from myapp.models import MyModel
124+
reveal_type(MyModel().objects) # N: Revealed type is 'myapp.models.MyModel_NewManager[myapp.models.MyModel]'
125+
reveal_type(MyModel().objects.get()) # N: Revealed type is 'myapp.models.MyModel*'
126+
reveal_type(MyModel().objects.queryset_method) # N: Revealed type is 'def (param: Union[builtins.str, None] =) -> Union[builtins.str, None]'
127+
reveal_type(MyModel().objects.queryset_method('str')) # N: Revealed type is 'Union[builtins.str, None]'
128+
installed_apps:
129+
- myapp
130+
files:
131+
- path: myapp/__init__.py
132+
- path: myapp/models.py
133+
content: |
134+
from django.db import models
135+
from myapp.managers import NewManager
136+
137+
class MyModel(models.Model):
138+
objects = NewManager()
139+
- path: myapp/managers.py
140+
content: |
141+
from typing import Optional
142+
from django.db import models
143+
144+
class ModelQuerySet(models.QuerySet):
145+
def queryset_method(self, param: Optional[str] = None) -> Optional[str]:
146+
return param
147+
148+
NewManager = models.Manager.from_queryset(ModelQuerySet)

0 commit comments

Comments
 (0)