Skip to content

Commit

Permalink
Specify error codes to ignore for plugin type ignores
Browse files Browse the repository at this point in the history
  • Loading branch information
flaeppe committed Sep 23, 2023
1 parent 5ba0b06 commit b6821b4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
8 changes: 4 additions & 4 deletions mypy_django_plugin/django/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from django.contrib.postgres.fields import ArrayField
except ImportError:

class ArrayField: # type: ignore
class ArrayField: # type: ignore[no-redef]
pass


Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion mypy_django_plugin/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Var,
)
from mypy.plugin import (
AnalyzeTypeContext,
AttributeContext,
CheckerPluginInterface,
ClassDefContext,
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions mypy_django_plugin/transformers/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions mypy_django_plugin/transformers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit b6821b4

Please sign in to comment.