1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #35564 -- Improved readability of subclass identification.

This commit is contained in:
Jaap Roes 2024-06-26 23:45:49 +02:00 committed by Sarah Boyce
parent e56a32b89b
commit f0d05a747f
2 changed files with 11 additions and 17 deletions

View File

@ -1,4 +1,5 @@
import collections import collections
import contextlib
from itertools import chain from itertools import chain
from django.apps import apps from django.apps import apps
@ -21,9 +22,8 @@ def _issubclass(cls, classinfo):
issubclass() variant that doesn't raise an exception if cls isn't a issubclass() variant that doesn't raise an exception if cls isn't a
class. class.
""" """
try: with contextlib.suppress(TypeError):
return issubclass(cls, classinfo) return issubclass(cls, classinfo)
except TypeError:
return False return False
@ -34,12 +34,8 @@ def _contains_subclass(class_path, candidate_paths):
""" """
cls = import_string(class_path) cls = import_string(class_path)
for path in candidate_paths: for path in candidate_paths:
try: with contextlib.suppress(ImportError, TypeError):
candidate_cls = import_string(path) if issubclass(import_string(path), cls):
except ImportError:
# ImportErrors are raised elsewhere.
continue
if _issubclass(candidate_cls, cls):
return True return True
return False return False

View File

@ -1,3 +1,4 @@
import contextlib
from itertools import chain from itertools import chain
from types import MethodType from types import MethodType
@ -15,13 +16,10 @@ def _subclass_index(class_path, candidate_paths):
list of candidate paths. If it does not exist, return -1. list of candidate paths. If it does not exist, return -1.
""" """
cls = import_string(class_path) cls = import_string(class_path)
for index, path in enumerate(candidate_paths): for i, path in enumerate(candidate_paths):
try: with contextlib.suppress(ImportError, TypeError):
candidate_cls = import_string(path) if issubclass(import_string(path), cls):
if issubclass(candidate_cls, cls): return i
return index
except (ImportError, TypeError):
continue
return -1 return -1