From b6821b445cf921b49c0153dc855c3d3e79543656 Mon Sep 17 00:00:00 2001 From: Petter Friberg Date: Thu, 21 Sep 2023 20:53:40 +0200 Subject: [PATCH] Specify error codes to ignore for plugin type ignores --- mypy_django_plugin/django/context.py | 8 ++++---- mypy_django_plugin/lib/helpers.py | 3 ++- mypy_django_plugin/transformers/fields.py | 8 +++++--- mypy_django_plugin/transformers/models.py | 6 +++--- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/mypy_django_plugin/django/context.py b/mypy_django_plugin/django/context.py index 18c8834c8..79d5e8fdd 100644 --- a/mypy_django_plugin/django/context.py +++ b/mypy_django_plugin/django/context.py @@ -29,7 +29,7 @@ from django.contrib.postgres.fields import ArrayField except ImportError: - class ArrayField: # type: ignore + class ArrayField: # type: ignore[no-redef] pass @@ -60,11 +60,11 @@ def initialize_django(settings_module: str) -> Tuple["Apps", "LazySettings"]: from django.apps import apps from django.conf import settings - apps.get_swappable_settings_name.cache_clear() # type: ignore + apps.get_swappable_settings_name.cache_clear() # type: ignore[attr-defined] apps.clear_cache() if not settings.configured: - settings._setup() # type: ignore + settings._setup() # type: ignore[misc] apps.populate(settings.INSTALLED_APPS) assert apps.apps_ready, "Apps are not ready" @@ -338,7 +338,7 @@ def get_field_related_model_cls(self, field: Union["RelatedField[Any, Any]", For related_model_cls = field.field.model if isinstance(related_model_cls, str): - if related_model_cls == "self": # type: ignore + if related_model_cls == "self": # type: ignore[unreachable] # same model related_model_cls = field.model elif "." not in related_model_cls: diff --git a/mypy_django_plugin/lib/helpers.py b/mypy_django_plugin/lib/helpers.py index 9f5289970..824b2d872 100644 --- a/mypy_django_plugin/lib/helpers.py +++ b/mypy_django_plugin/lib/helpers.py @@ -24,6 +24,7 @@ Var, ) from mypy.plugin import ( + AnalyzeTypeContext, AttributeContext, CheckerPluginInterface, ClassDefContext, @@ -341,7 +342,7 @@ def resolve_string_attribute_value(attr_expr: Expression, django_context: "Djang return None -def get_semanal_api(ctx: Union[ClassDefContext, DynamicClassDefContext]) -> SemanticAnalyzer: +def get_semanal_api(ctx: Union[AnalyzeTypeContext, ClassDefContext, DynamicClassDefContext]) -> SemanticAnalyzer: if not isinstance(ctx.api, SemanticAnalyzer): raise ValueError("Not a SemanticAnalyzer") return ctx.api diff --git a/mypy_django_plugin/transformers/fields.py b/mypy_django_plugin/transformers/fields.py index 6a68207c3..ff8c09646 100644 --- a/mypy_django_plugin/transformers/fields.py +++ b/mypy_django_plugin/transformers/fields.py @@ -7,7 +7,7 @@ from mypy.nodes import AssignmentStmt, NameExpr, TypeInfo from mypy.plugin import FunctionContext from mypy.semanal_shared import parse_bool -from mypy.types import AnyType, Instance, TypeOfAny, UnionType +from mypy.types import AnyType, Instance, ProperType, TypeOfAny, UnionType from mypy.types import Type as MypyType from mypy_django_plugin.django.context import DjangoContext @@ -91,18 +91,20 @@ def fill_descriptor_types_for_related_field(ctx: FunctionContext, django_context typechecker_api = helpers.get_typechecker_api(ctx) related_model_info = helpers.lookup_class_typeinfo(typechecker_api, related_model) + related_model_type: ProperType if related_model_info is None: # maybe no type stub related_model_type = AnyType(TypeOfAny.unannotated) else: - related_model_type = Instance(related_model_info, []) # type: ignore + related_model_type = Instance(related_model_info, []) related_model_to_set_info = helpers.lookup_class_typeinfo(typechecker_api, related_model_to_set) + related_model_to_set_type: ProperType if related_model_to_set_info is None: # maybe no type stub related_model_to_set_type = AnyType(TypeOfAny.unannotated) else: - related_model_to_set_type = Instance(related_model_to_set_info, []) # type: ignore + related_model_to_set_type = Instance(related_model_to_set_info, []) # replace Any with referred_to_type return reparametrize_related_field_type( diff --git a/mypy_django_plugin/transformers/models.py b/mypy_django_plugin/transformers/models.py index ed6077822..b9cba721f 100644 --- a/mypy_django_plugin/transformers/models.py +++ b/mypy_django_plugin/transformers/models.py @@ -753,7 +753,7 @@ def set_auth_user_model_boolean_fields(ctx: AttributeContext, django_context: Dj def handle_annotated_type(ctx: AnalyzeTypeContext, django_context: DjangoContext) -> MypyType: args = ctx.type.args type_arg = ctx.api.analyze_type(args[0]) - api = cast(SemanticAnalyzer, ctx.api.api) # type: ignore + api = helpers.get_semanal_api(ctx) if not isinstance(type_arg, Instance) or not type_arg.type.has_base(MODEL_CLASS_FULLNAME): return type_arg @@ -768,7 +768,7 @@ def handle_annotated_type(ctx: AnalyzeTypeContext, django_context: DjangoContext if isinstance(annotations_type_arg, TypedDictType): fields_dict = annotations_type_arg elif not isinstance(annotations_type_arg, AnyType): - ctx.api.fail("Only TypedDicts are supported as type arguments to Annotations", ctx.context) + api.fail("Only TypedDicts are supported as type arguments to Annotations", ctx.context) return get_or_create_annotated_type(api, type_arg, fields_dict=fields_dict) @@ -800,7 +800,7 @@ def get_or_create_annotated_type( cast(TypeChecker, api), model_module_name + "." + type_name ) if annotated_typeinfo is None: - model_module_file = api.modules.get(model_module_name) # type: ignore + model_module_file = api.modules.get(model_module_name) # type: ignore[union-attr] if model_module_file is None: return AnyType(TypeOfAny.from_error)