Skip to content

Commit

Permalink
Avoid returning None from get_field_related_model_cls
Browse files Browse the repository at this point in the history
#1495 updated
`get_field_related_model_cls` to raise `UnregisteredModelError` rather
than returning `None` for failure paths. However, None can still be
returned if the initial retrieval of `related_model_cls` returns None.

This patch adds a check to see if the initial retrieval has got a `None`
and then raises the appropriate error rather than letting `None` be
returned.
  • Loading branch information
SingingTree committed Feb 19, 2024
1 parent 39f7b93 commit 7e346e3
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions mypy_django_plugin/django/context.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import sys
from collections import defaultdict
from contextlib import contextmanager
from contextlib import contextmanager, suppress
from functools import cached_property
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -374,16 +374,14 @@ def get_field_related_model_cls(self, field: Union["RelatedField[Any, Any]", For
# same model
related_model_cls = field.model
elif "." not in related_model_cls:
# same file model
related_model_fullname = f"{field.model.__module__}.{related_model_cls}"
related_model_cls = self.get_model_class_by_fullname(related_model_fullname)
if related_model_cls is None:
raise UnregisteredModelError
else:
try:
with suppress(LookupError):
related_model_cls = self.apps_registry.get_model(related_model_cls)
except LookupError as e:
raise UnregisteredModelError from e

if related_model_cls is None:
raise UnregisteredModelError

return related_model_cls

Expand Down

0 comments on commit 7e346e3

Please sign in to comment.