Skip to content

Commit 640afe3

Browse files
committed
move init_create to new API
1 parent 094d663 commit 640afe3

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

mypy_django_plugin/main.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313

1414
from mypy_django_plugin.django.context import DjangoContext
1515
from mypy_django_plugin.lib import fullnames, helpers
16-
from mypy_django_plugin.transformers import init_create, querysets
16+
from mypy_django_plugin.transformers import querysets
1717
from mypy_django_plugin.transformers2.fields import FieldContructorCallback
1818
from mypy_django_plugin.transformers2.forms import (
1919
FormCallback, GetFormCallback, GetFormClassCallback,
2020
)
21+
from mypy_django_plugin.transformers2.init_create import (
22+
ModelCreateCallback, ModelInitCallback,
23+
)
2124
from mypy_django_plugin.transformers2.meta import MetaGetFieldCallback
2225
from mypy_django_plugin.transformers2.models import ModelCallback
2326
from mypy_django_plugin.transformers2.orm_lookups import (
@@ -172,7 +175,8 @@ def get_function_hook(self, fullname: str
172175
return FieldContructorCallback(self)
173176

174177
if self.django_context.is_model_subclass(info):
175-
return partial(init_create.redefine_and_typecheck_model_init, django_context=self.django_context)
178+
return ModelInitCallback(self)
179+
# return partial(init_create.redefine_and_typecheck_model_init, django_context=self.django_context)
176180
return None
177181

178182
def get_method_hook(self, fullname: str
@@ -205,7 +209,7 @@ def get_method_hook(self, fullname: str
205209

206210
manager_classes = self._get_current_manager_bases()
207211
if class_fullname in manager_classes and method_name == 'create':
208-
return partial(init_create.redefine_and_typecheck_model_create, django_context=self.django_context)
212+
return ModelCreateCallback(self)
209213

210214
if class_fullname in manager_classes and method_name in {'filter', 'get', 'exclude'}:
211215
return QuerySetFilterTypecheckCallback(self)

mypy_django_plugin/transformers/init_create.py renamed to mypy_django_plugin/transformers2/init_create.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from mypy.types import Type as MypyType
77

88
from mypy_django_plugin.django.context import DjangoContext
9-
from mypy_django_plugin.lib import chk_helpers
9+
from mypy_django_plugin.lib import chk_helpers, helpers
1010

1111

1212
def get_actual_types(ctx: Union[MethodContext, FunctionContext],
@@ -51,25 +51,27 @@ def typecheck_model_method(ctx: Union[FunctionContext, MethodContext], django_co
5151
return ctx.default_return_type
5252

5353

54-
def redefine_and_typecheck_model_init(ctx: FunctionContext, django_context: DjangoContext) -> MypyType:
55-
assert isinstance(ctx.default_return_type, Instance)
54+
class ModelInitCallback(helpers.GetFunctionCallback):
55+
def get_function_return_type(self) -> MypyType:
56+
assert isinstance(self.default_return_type, Instance)
5657

57-
model_fullname = ctx.default_return_type.type.fullname
58-
model_cls = django_context.get_model_class_by_fullname(model_fullname)
59-
if model_cls is None:
60-
return ctx.default_return_type
58+
model_fullname = self.default_return_type.type.fullname
59+
model_cls = self.django_context.get_model_class_by_fullname(model_fullname)
60+
if model_cls is None:
61+
return self.default_return_type
6162

62-
return typecheck_model_method(ctx, django_context, model_cls, '__init__')
63+
return typecheck_model_method(self.ctx, self.django_context, model_cls, '__init__')
6364

6465

65-
def redefine_and_typecheck_model_create(ctx: MethodContext, django_context: DjangoContext) -> MypyType:
66-
if not isinstance(ctx.default_return_type, Instance):
67-
# only work with ctx.default_return_type = model Instance
68-
return ctx.default_return_type
66+
class ModelCreateCallback(helpers.GetMethodCallback):
67+
def get_method_return_type(self) -> MypyType:
68+
if not isinstance(self.default_return_type, Instance):
69+
# only work with ctx.default_return_type = model Instance
70+
return self.default_return_type
6971

70-
model_fullname = ctx.default_return_type.type.fullname
71-
model_cls = django_context.get_model_class_by_fullname(model_fullname)
72-
if model_cls is None:
73-
return ctx.default_return_type
72+
model_fullname = self.default_return_type.type.fullname
73+
model_cls = self.django_context.get_model_class_by_fullname(model_fullname)
74+
if model_cls is None:
75+
return self.default_return_type
7476

75-
return typecheck_model_method(ctx, django_context, model_cls, 'create')
77+
return typecheck_model_method(self.ctx, self.django_context, model_cls, 'create')

0 commit comments

Comments
 (0)