Skip to content

Commit bfa4590

Browse files
authored
Unify plugin check for model type info (#1853)
Replace a couple of repetitions with an auxiliary call
1 parent 5d54ac4 commit bfa4590

File tree

4 files changed

+7
-19
lines changed

4 files changed

+7
-19
lines changed

mypy_django_plugin/lib/helpers.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def add_new_manager_base(api: SemanticAnalyzerPluginInterface, fullname: str) ->
416416

417417

418418
def is_abstract_model(model: TypeInfo) -> bool:
419-
if model.metaclass_type is None or model.metaclass_type.type.fullname != fullnames.MODEL_METACLASS_FULLNAME:
419+
if not is_model_type(model):
420420
return False
421421

422422
metadata = get_django_metadata(model)
@@ -473,8 +473,5 @@ def resolve_lazy_reference(
473473
return None
474474

475475

476-
def is_model_instance(instance: Instance) -> bool:
477-
return (
478-
instance.type.metaclass_type is not None
479-
and instance.type.metaclass_type.type.fullname == fullnames.MODEL_METACLASS_FULLNAME
480-
)
476+
def is_model_type(info: TypeInfo) -> bool:
477+
return info.metaclass_type is not None and info.metaclass_type.type.fullname == fullnames.MODEL_METACLASS_FULLNAME

mypy_django_plugin/main.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,7 @@ def get_customize_class_mro_hook(self, fullname: str) -> Optional[Callable[[Clas
245245
def get_base_class_hook(self, fullname: str) -> Optional[Callable[[ClassDefContext], None]]:
246246
# Base class is a Model class definition
247247
sym = self.lookup_fully_qualified(fullname)
248-
if (
249-
sym is not None
250-
and isinstance(sym.node, TypeInfo)
251-
and sym.node.metaclass_type is not None
252-
and sym.node.metaclass_type.type.fullname == fullnames.MODEL_METACLASS_FULLNAME
253-
):
248+
if sym is not None and isinstance(sym.node, TypeInfo) and helpers.is_model_type(sym.node):
254249
return partial(process_model_class, django_context=self.django_context)
255250

256251
# Base class is a Manager class definition

mypy_django_plugin/transformers/manytomany.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,7 @@ def get_model_from_expression(
130130
argument(e.g. "<app_label>.<object_name>") to a Django model is also attempted.
131131
"""
132132
if isinstance(expr, NameExpr) and isinstance(expr.node, TypeInfo):
133-
if (
134-
expr.node.metaclass_type is not None
135-
and expr.node.metaclass_type.type.fullname == fullnames.MODEL_METACLASS_FULLNAME
136-
):
133+
if helpers.is_model_type(expr.node):
137134
return Instance(expr.node, [])
138135

139136
lazy_reference = None
@@ -166,7 +163,7 @@ def get_related_manager_and_model(ctx: MethodContext) -> Optional[Tuple[Instance
166163
if (
167164
many_related_manager.args
168165
and isinstance(many_related_manager.args[0], Instance)
169-
and helpers.is_model_instance(many_related_manager.args[0])
166+
and helpers.is_model_type(many_related_manager.args[0].type)
170167
):
171168
return many_related_manager, many_related_manager.args[0]
172169

mypy_django_plugin/transformers/models.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,8 +850,7 @@ def get_exception_bases(self, name: str) -> List[Instance]:
850850
exception_base_sym = model_base.names.get(name)
851851
if (
852852
# Base class is a Model
853-
model_base.metaclass_type is not None
854-
and model_base.metaclass_type.type.fullname == fullnames.MODEL_METACLASS_FULLNAME
853+
helpers.is_model_type(model_base)
855854
# But base class is not 'models.Model'
856855
and model_base.fullname != fullnames.MODEL_CLASS_FULLNAME
857856
# Base class also has a generated exception base e.g. 'DoesNotExist'

0 commit comments

Comments
 (0)