|
6 | 6 | from mypy.types import Type as MypyType |
7 | 7 |
|
8 | 8 | 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 |
10 | 10 |
|
11 | 11 |
|
12 | 12 | def get_actual_types(ctx: Union[MethodContext, FunctionContext], |
@@ -51,25 +51,27 @@ def typecheck_model_method(ctx: Union[FunctionContext, MethodContext], django_co |
51 | 51 | return ctx.default_return_type |
52 | 52 |
|
53 | 53 |
|
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) |
56 | 57 |
|
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 |
61 | 62 |
|
62 | | - return typecheck_model_method(ctx, django_context, model_cls, '__init__') |
| 63 | + return typecheck_model_method(self.ctx, self.django_context, model_cls, '__init__') |
63 | 64 |
|
64 | 65 |
|
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 |
69 | 71 |
|
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 |
74 | 76 |
|
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