Skip to content

Commit ac36393

Browse files
authored
Unify plugin check for model type info (#2263)
Replace `is_model_subclass_info` with `is_model_type` helper
1 parent b2b1afa commit ac36393

File tree

4 files changed

+3
-13
lines changed

4 files changed

+3
-13
lines changed

mypy_django_plugin/django/context.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,6 @@ def all_registered_model_classes(self) -> Set[Type[models.Model]]:
301301

302302
return all_model_bases
303303

304-
@cached_property
305-
def all_registered_model_class_fullnames(self) -> Set[str]:
306-
return {helpers.get_class_fullname(cls) for cls in self.all_registered_model_classes}
307-
308304
@cached_property
309305
def model_class_fullnames_by_label(self) -> Mapping[str, str]:
310306
return {

mypy_django_plugin/lib/helpers.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,6 @@ def get_typechecker_api(ctx: Union[AttributeContext, MethodContext, FunctionCont
401401
return ctx.api
402402

403403

404-
def is_model_subclass_info(info: TypeInfo, django_context: "DjangoContext") -> bool:
405-
return info.fullname in django_context.all_registered_model_class_fullnames or info.has_base(
406-
fullnames.MODEL_CLASS_FULLNAME
407-
)
408-
409-
410404
def check_types_compatible(
411405
ctx: Union[FunctionContext, MethodContext], *, expected_type: MypyType, actual_type: MypyType, error_message: str
412406
) -> None:

mypy_django_plugin/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def get_function_hook(self, fullname: str) -> Optional[Callable[[FunctionContext
174174
if info.has_base(fullnames.FIELD_FULLNAME):
175175
return partial(fields.transform_into_proper_return_type, django_context=self.django_context)
176176

177-
if helpers.is_model_subclass_info(info, self.django_context):
177+
if helpers.is_model_type(info):
178178
return partial(init_create.redefine_and_typecheck_model_init, django_context=self.django_context)
179179

180180
return None

mypy_django_plugin/transformers/fields.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def _get_current_field_from_assignment(
2424
ctx: FunctionContext, django_context: DjangoContext
2525
) -> Optional[Union["Field[Any, Any]", ForeignObjectRel, "GenericForeignKey"]]:
2626
outer_model_info = helpers.get_typechecker_api(ctx).scope.active_class()
27-
if outer_model_info is None or not helpers.is_model_subclass_info(outer_model_info, django_context):
27+
if outer_model_info is None or not helpers.is_model_type(outer_model_info):
2828
return None
2929

3030
field_name = None
@@ -234,7 +234,7 @@ def transform_into_proper_return_type(ctx: FunctionContext, django_context: Djan
234234
assert isinstance(default_return_type, Instance)
235235

236236
outer_model_info = helpers.get_typechecker_api(ctx).scope.active_class()
237-
if outer_model_info is None or not helpers.is_model_subclass_info(outer_model_info, django_context):
237+
if outer_model_info is None or not helpers.is_model_type(outer_model_info):
238238
return ctx.default_return_type
239239

240240
assert isinstance(outer_model_info, TypeInfo)

0 commit comments

Comments
 (0)