Skip to content

Commit

Permalink
Use dict as data structure for model_modules (#1671)
Browse files Browse the repository at this point in the history
Should give a slight performance improvement as we don't need to
iterate over all models declared in a file, but can lookup by name
instead.
  • Loading branch information
flaeppe authored Sep 2, 2023
1 parent 3a1f139 commit 218d5fd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
13 changes: 5 additions & 8 deletions mypy_django_plugin/django/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ def __init__(self, django_settings_module: str) -> None:
self.settings = settings

@cached_property
def model_modules(self) -> Dict[str, Set[Type[Model]]]:
def model_modules(self) -> Dict[str, Dict[str, Type[Model]]]:
"""All modules that contain Django models."""
modules: Dict[str, Set[Type[Model]]] = defaultdict(set)
modules: Dict[str, Dict[str, Type[Model]]] = defaultdict(dict)
for concrete_model_cls in self.apps_registry.get_models():
modules[concrete_model_cls.__module__].add(concrete_model_cls)
modules[concrete_model_cls.__module__][concrete_model_cls.__name__] = concrete_model_cls
# collect abstract=True models
for model_cls in concrete_model_cls.mro()[1:]:
if issubclass(model_cls, Model) and hasattr(model_cls, "_meta") and model_cls._meta.abstract:
modules[model_cls.__module__].add(model_cls)
modules[model_cls.__module__][model_cls.__name__] = model_cls
return modules

def get_model_class_by_fullname(self, fullname: str) -> Optional[Type[Model]]:
Expand All @@ -109,10 +109,7 @@ def get_model_class_by_fullname(self, fullname: str) -> Optional[Type[Model]]:
fullname = fullname.replace("__", ".")

module, _, model_cls_name = fullname.rpartition(".")
for model_cls in self.model_modules.get(module, set()):
if model_cls.__name__ == model_cls_name:
return model_cls
return None
return self.model_modules.get(module, {}).get(model_cls_name)

def get_model_fields(self, model_cls: Type[Model]) -> Iterator["Field[Any, Any]"]:
for field in model_cls._meta.get_fields():
Expand Down
2 changes: 1 addition & 1 deletion mypy_django_plugin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def get_additional_deps(self, file: MypyFile) -> List[Tuple[int, str, int]]:
return []
deps = set()

for model_class in defined_model_classes:
for model_class in defined_model_classes.values():
for field in itertools.chain(
# forward relations
self.django_context.get_model_related_fields(model_class),
Expand Down

0 comments on commit 218d5fd

Please sign in to comment.