Skip to content

Commit

Permalink
Update to newer django-stubs and remove hack
Browse files Browse the repository at this point in the history
  • Loading branch information
delfick committed May 28, 2024
1 parent 3385fd0 commit b4958ba
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 95 deletions.
23 changes: 0 additions & 23 deletions extended_mypy_django_plugin/plugin/_helpers.py

This file was deleted.

72 changes: 1 addition & 71 deletions extended_mypy_django_plugin/plugin/_plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import collections
import sys
from typing import Generic

Expand All @@ -9,7 +8,6 @@
from mypy.plugin import (
AnalyzeTypeContext,
AttributeContext,
ClassDefContext,
DynamicClassDefContext,
FunctionContext,
FunctionSigContext,
Expand All @@ -28,16 +26,7 @@
)
from typing_extensions import assert_never

from . import (
_config,
_dependencies,
_helpers,
_hook,
_known_annotations,
_reports,
_store,
actions,
)
from . import _config, _dependencies, _hook, _known_annotations, _reports, _store, actions


class Hook(
Expand Down Expand Up @@ -151,65 +140,6 @@ def get_additional_deps(self, file: MypyFile) -> list[tuple[int, str, int]]:
)
return results

@_hook.hook
class get_base_class_hook(Hook[ClassDefContext, None]):
"""
We need to make up for a bug in django-stubs
"""

def choose(self) -> bool:
if self.super_hook is None:
return False

if _helpers.get_is_abstract_model() is None:
return False

sym = self.plugin.lookup_fully_qualified(self.fullname)
return bool(
sym is not None
and isinstance(sym.node, TypeInfo)
and _helpers.is_model_type(sym.node)
)

def run(self, ctx: ClassDefContext) -> None:
if self.super_hook is None:
return None

# Copy the code in django-stubs that crashes
# And fill in the missing information before continuing
processed_models = set()
model_bases = collections.deque([ctx.cls])
while model_bases:
model = model_bases.popleft()

try:
# Whether this causes an AssertionError or an AttributeError depends
# on whether mypy is compiled or not
# Note that this only appears to trigger on followup changes with a cache
# in very specific situations
for base in model.info.bases:
break
except AssertionError as exc:
if str(exc) == "ClassDef is lacking info":
sym = self.plugin.lookup_fully_qualified(model.fullname)
if sym and isinstance(sym.node, TypeInfo):
model.info = sym.node
except AttributeError as exc:
if str(exc) == "attribute 'bases' of 'TypeInfo' undefined":
sym = self.plugin.lookup_fully_qualified(model.fullname)
if sym and isinstance(sym.node, TypeInfo):
model.info = sym.node

for base in model.info.bases:
if (
_helpers.is_abstract_model(base.type)
and base.type.fullname not in processed_models
):
model_bases.append(base.type.defn)
processed_models.add(base.type.fullname)

return self.super_hook(ctx)

@_hook.hook
class get_dynamic_class_hook(Hook[DynamicClassDefContext, None]):
"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ authors = [
[project.optional-dependencies]
stubs-latest = [
"mypy==1.10.0",
"django-stubs==5.0.0",
"django-stubs==5.0.2",
]
stubs-older = [
"mypy==1.4.0",
Expand Down

0 comments on commit b4958ba

Please sign in to comment.