Skip to content

Commit c6e68ce

Browse files
committed
replace field callback with new API
1 parent da94e2f commit c6e68ce

File tree

6 files changed

+183
-178
lines changed

6 files changed

+183
-178
lines changed

mypy_django_plugin/django/context.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from django.db.models.sql.query import Query
1717
from django.utils.functional import cached_property
1818
from mypy.checker import TypeChecker
19+
from mypy.nodes import TypeInfo
1920
from mypy.plugin import MethodContext
2021
from mypy.types import AnyType, Instance
2122
from mypy.types import Type as MypyType
@@ -214,6 +215,10 @@ def all_registered_model_classes(self) -> Set[Type[models.Model]]:
214215
def all_registered_model_class_fullnames(self) -> Set[str]:
215216
return {helpers.get_class_fullname(cls) for cls in self.all_registered_model_classes}
216217

218+
def is_model_subclass(self, info: TypeInfo) -> bool:
219+
return (info.fullname in self.all_registered_model_class_fullnames
220+
or info.has_base(fullnames.MODEL_CLASS_FULLNAME))
221+
217222
def get_attname(self, field: Field) -> str:
218223
attname = field.attname
219224
return attname

mypy_django_plugin/lib/helpers.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from mypy.types import Type as MypyType
2121
from mypy.types import TypeOfAny, UnionType
2222

23-
from mypy_django_plugin.lib import fullnames
2423
from mypy_django_plugin.transformers2 import new_helpers
2524

2625
if TYPE_CHECKING:
@@ -201,10 +200,12 @@ def get_method_return_type(self) -> MypyType:
201200

202201
class GetFunctionCallback(TypeCheckerPluginCallback):
203202
ctx: FunctionContext
203+
default_return_type: MypyType
204204

205205
def __call__(self, ctx: FunctionContext) -> MypyType:
206206
self.type_checker = cast(TypeChecker, ctx.api)
207207
self.ctx = ctx
208+
self.default_return_type = ctx.default_return_type
208209
return self.get_function_return_type()
209210

210211
@abstractmethod
@@ -403,11 +404,6 @@ def resolve_string_attribute_value(attr_expr: Expression, django_context: 'Djang
403404
return None
404405

405406

406-
def is_subclass_of_model(info: TypeInfo, django_context: 'DjangoContext') -> bool:
407-
return (info.fullname in django_context.all_registered_model_class_fullnames
408-
or info.has_base(fullnames.MODEL_CLASS_FULLNAME))
409-
410-
411407
def new_typeinfo(name: str,
412408
*,
413409
bases: List[Instance],

mypy_django_plugin/main.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
import mypy_django_plugin.transformers.orm_lookups
1515
from mypy_django_plugin.django.context import DjangoContext
1616
from mypy_django_plugin.lib import fullnames, helpers
17-
from mypy_django_plugin.transformers import (
18-
fields, init_create, meta, querysets,
19-
)
17+
from mypy_django_plugin.transformers import init_create, meta, querysets
18+
from mypy_django_plugin.transformers2.fields import FieldContructorCallback
2019
from mypy_django_plugin.transformers2.forms import (
2120
FormCallback, GetFormCallback, GetFormClassCallback,
2221
)
@@ -167,9 +166,9 @@ def get_function_hook(self, fullname: str
167166
info = self._get_typeinfo_or_none(fullname)
168167
if info:
169168
if info.has_base(fullnames.FIELD_FULLNAME):
170-
return partial(fields.transform_into_proper_return_type, django_context=self.django_context)
169+
return FieldContructorCallback(self)
171170

172-
if helpers.is_subclass_of_model(info, self.django_context):
171+
if self.django_context.is_model_subclass(info):
173172
return partial(init_create.redefine_and_typecheck_model_init, django_context=self.django_context)
174173
return None
175174

mypy_django_plugin/transformers/fields.py

Lines changed: 0 additions & 162 deletions
This file was deleted.

0 commit comments

Comments
 (0)