diff options
| author | Jaap Roes <jroes@leukeleu.nl> | 2024-06-26 23:45:49 +0200 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-06-27 13:08:14 +0200 |
| commit | f0d05a747f7a099e6c6bc58c42a787546d2212e7 (patch) | |
| tree | 1b8bb58edb9c66ac9dfcb1f927c7664c284c3a52 | |
| parent | e56a32b89bb7fadffdfaa2cdf12b4863ccd5af9b (diff) | |
Fixed #35564 -- Improved readability of subclass identification.
| -rw-r--r-- | django/contrib/admin/checks.py | 16 | ||||
| -rw-r--r-- | django/contrib/auth/checks.py | 12 |
2 files changed, 11 insertions, 17 deletions
diff --git a/django/contrib/admin/checks.py b/django/contrib/admin/checks.py index 94e700cf68..9937273c28 100644 --- a/django/contrib/admin/checks.py +++ b/django/contrib/admin/checks.py @@ -1,4 +1,5 @@ import collections +import contextlib from itertools import chain from django.apps import apps @@ -21,10 +22,9 @@ def _issubclass(cls, classinfo): issubclass() variant that doesn't raise an exception if cls isn't a class. """ - try: + with contextlib.suppress(TypeError): return issubclass(cls, classinfo) - except TypeError: - return False + return False def _contains_subclass(class_path, candidate_paths): @@ -34,13 +34,9 @@ def _contains_subclass(class_path, candidate_paths): """ cls = import_string(class_path) for path in candidate_paths: - try: - candidate_cls = import_string(path) - except ImportError: - # ImportErrors are raised elsewhere. - continue - if _issubclass(candidate_cls, cls): - return True + with contextlib.suppress(ImportError, TypeError): + if issubclass(import_string(path), cls): + return True return False diff --git a/django/contrib/auth/checks.py b/django/contrib/auth/checks.py index f2f9a74a6c..61da4f293a 100644 --- a/django/contrib/auth/checks.py +++ b/django/contrib/auth/checks.py @@ -1,3 +1,4 @@ +import contextlib from itertools import chain 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. """ cls = import_string(class_path) - for index, path in enumerate(candidate_paths): - try: - candidate_cls = import_string(path) - if issubclass(candidate_cls, cls): - return index - except (ImportError, TypeError): - continue + for i, path in enumerate(candidate_paths): + with contextlib.suppress(ImportError, TypeError): + if issubclass(import_string(path), cls): + return i return -1 |
