Skip to content

Commit 1faf12b

Browse files
authored
Refactor: Unify plugin check for model type info (#2286)
1 parent e7611b3 commit 1faf12b

File tree

3 files changed

+7
-14
lines changed

3 files changed

+7
-14
lines changed

mypy_django_plugin/transformers/managers.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,7 @@ def _process_dynamic_method(
138138
args_types = method_type.arg_types[1:]
139139
if _has_compatible_type_vars(base_that_has_method):
140140
typed_var = manager_instance.args or queryset_info.bases[0].args
141-
if (
142-
typed_var
143-
and isinstance(typed_var[0], Instance)
144-
and typed_var[0].type.has_base(fullnames.MODEL_CLASS_FULLNAME)
145-
):
141+
if typed_var and isinstance(typed_var[0], Instance) and helpers.is_model_type(typed_var[0].type):
146142
ret_type = _replace_type_var(ret_type, base_that_has_method.defn.type_vars[0].fullname, typed_var[0])
147143
args_types = [
148144
_replace_type_var(arg_type, base_that_has_method.defn.type_vars[0].fullname, typed_var[0])

mypy_django_plugin/transformers/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
from mypy_django_plugin.errorcodes import MANAGER_MISSING
4545
from mypy_django_plugin.exceptions import UnregisteredModelError
4646
from mypy_django_plugin.lib import fullnames, helpers
47-
from mypy_django_plugin.lib.fullnames import ANNOTATIONS_FULLNAME, MODEL_CLASS_FULLNAME
47+
from mypy_django_plugin.lib.fullnames import ANNOTATIONS_FULLNAME
4848
from mypy_django_plugin.transformers.fields import FieldDescriptorTypes, get_field_descriptor_types
4949
from mypy_django_plugin.transformers.managers import (
5050
MANAGER_METHODS_RETURNING_QUERYSET,
@@ -1124,7 +1124,7 @@ def handle_annotated_type(ctx: AnalyzeTypeContext, fullname: str) -> MypyType:
11241124
if not args:
11251125
return AnyType(TypeOfAny.from_omitted_generics) if is_with_annotations else ctx.type
11261126
type_arg = ctx.api.analyze_type(args[0])
1127-
if not isinstance(type_arg, Instance) or not type_arg.type.has_base(MODEL_CLASS_FULLNAME):
1127+
if not isinstance(type_arg, Instance) or not helpers.is_model_type(type_arg.type):
11281128
return type_arg
11291129

11301130
fields_dict = None

mypy_django_plugin/transformers/querysets.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from mypy_django_plugin.django.context import DjangoContext, LookupsAreUnsupported
1616
from mypy_django_plugin.lib import fullnames, helpers
17-
from mypy_django_plugin.lib.fullnames import ANY_ATTR_ALLOWED_CLASS_FULLNAME
1817
from mypy_django_plugin.lib.helpers import parse_bool
1918
from mypy_django_plugin.transformers.models import get_annotated_type
2019

@@ -33,7 +32,7 @@ def _extract_model_type_from_queryset(queryset_type: Instance, api: TypeChecker)
3332
if (
3433
len(base_type.args)
3534
and isinstance(base_type.args[0], Instance)
36-
and base_type.args[0].type.has_base(fullnames.MODEL_CLASS_FULLNAME)
35+
and helpers.is_model_type(base_type.args[0].type)
3736
):
3837
return base_type.args[0]
3938
return None
@@ -46,7 +45,7 @@ def determine_proper_manager_type(ctx: FunctionContext) -> MypyType:
4645
outer_model_info = helpers.get_typechecker_api(ctx).scope.active_class()
4746
if (
4847
outer_model_info is None
49-
or not outer_model_info.has_base(fullnames.MODEL_CLASS_FULLNAME)
48+
or not helpers.is_model_type(outer_model_info)
5049
or outer_model_info.self_type is None
5150
or not default_return_type.type.is_generic()
5251
):
@@ -123,7 +122,7 @@ def get_values_list_row_type(
123122
typechecker_api,
124123
"Row",
125124
column_types,
126-
extra_bases=[typechecker_api.named_generic_type(ANY_ATTR_ALLOWED_CLASS_FULLNAME, [])],
125+
extra_bases=[typechecker_api.named_generic_type(fullnames.ANY_ATTR_ALLOWED_CLASS_FULLNAME, [])],
127126
)
128127
else:
129128
return helpers.make_oneoff_named_tuple(typechecker_api, "Row", column_types)
@@ -300,9 +299,7 @@ def extract_proper_type_queryset_annotate(ctx: MethodContext, django_context: Dj
300299
row_type = AnyType(TypeOfAny.implementation_artifact)
301300
else:
302301
row_type = api.named_generic_type("builtins.tuple", [AnyType(TypeOfAny.from_omitted_generics)])
303-
elif isinstance(original_row_type, Instance) and original_row_type.type.has_base(
304-
fullnames.MODEL_CLASS_FULLNAME
305-
):
302+
elif isinstance(original_row_type, Instance) and helpers.is_model_type(original_row_type.type):
306303
row_type = annotated_type
307304
else:
308305
row_type = annotated_type

0 commit comments

Comments
 (0)