From 23b6aa318ca77abbc157fa53bbeb27670f70e68a Mon Sep 17 00:00:00 2001 From: Bryce Seager van Dyk Date: Mon, 19 Feb 2024 11:27:59 +1300 Subject: [PATCH] Avoid returning None from get_field_related_model_cls https://github.com/typeddjango/django-stubs/pull/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. --- mypy_django_plugin/django/context.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mypy_django_plugin/django/context.py b/mypy_django_plugin/django/context.py index 347ec92ef..df69f95f2 100644 --- a/mypy_django_plugin/django/context.py +++ b/mypy_django_plugin/django/context.py @@ -369,6 +369,9 @@ def get_field_related_model_cls(self, field: Union["RelatedField[Any, Any]", For else: related_model_cls = field.field.model + if related_model_cls is None: + raise UnregisteredModelError + if isinstance(related_model_cls, str): if related_model_cls == "self": # type: ignore[unreachable] # same model