Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify error codes to ignore for plugin type ignores #1726

Merged
merged 3 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
9 changes: 5 additions & 4 deletions mypy_django_plugin/transformers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from mypy.plugin import AnalyzeTypeContext, AttributeContext, CheckerPluginInterface, ClassDefContext
from mypy.plugins import common
from mypy.semanal import SemanticAnalyzer
from mypy.typeanal import TypeAnalyser
from mypy.types import AnyType, Instance, LiteralType, ProperType, TypedDictType, TypeOfAny, TypeType, get_proper_type
from mypy.types import Type as MypyType
from mypy.typevars import fill_typevars
Expand Down Expand Up @@ -753,8 +754,6 @@ 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
flaeppe marked this conversation as resolved.
Show resolved Hide resolved

if not isinstance(type_arg, Instance) or not type_arg.type.has_base(MODEL_CLASS_FULLNAME):
return type_arg

Expand All @@ -770,7 +769,9 @@ def handle_annotated_type(ctx: AnalyzeTypeContext, django_context: DjangoContext
elif not isinstance(annotations_type_arg, AnyType):
ctx.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)
assert isinstance(ctx.api, TypeAnalyser)
assert isinstance(ctx.api.api, SemanticAnalyzer)
return get_or_create_annotated_type(ctx.api.api, type_arg, fields_dict=fields_dict)


def get_or_create_annotated_type(
Expand Down Expand Up @@ -800,7 +801,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