diff options
| author | Mads Jensen <mje@inducks.org> | 2017-03-09 16:17:41 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-06-28 14:07:55 -0400 |
| commit | 550cb3a365dee4edfdd1563224d5304de2a57fda (patch) | |
| tree | fb532f38774ff7619edd2a4532c3daae1ee0ac5a /django/db/models/options.py | |
| parent | 43a4835edf32c57eb74c0eb207c276734a34abcf (diff) | |
Fixed #27818 -- Replaced try/except/pass with contextlib.suppress().
Diffstat (limited to 'django/db/models/options.py')
| -rw-r--r-- | django/db/models/options.py | 17 |
1 files changed, 5 insertions, 12 deletions
diff --git a/django/db/models/options.py b/django/db/models/options.py index 90462278d9..9ed2915edb 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -3,6 +3,7 @@ import inspect import warnings from bisect import bisect from collections import OrderedDict, defaultdict +from contextlib import suppress from itertools import chain from django.apps import apps @@ -269,10 +270,8 @@ class Options: # is a cached property, and all the models haven't been loaded yet, so # we need to make sure we don't cache a string reference. if field.is_relation and hasattr(field.remote_field, 'model') and field.remote_field.model: - try: + with suppress(AttributeError): field.remote_field.model._meta._expire_cache(forward=False) - except AttributeError: - pass self._expire_cache() else: self._expire_cache(reverse=False) @@ -520,10 +519,8 @@ class Options: # Due to the way Django's internals work, get_field() should also # be able to fetch a field by attname. In the case of a concrete # field with relation, includes the *_id name too - try: + with suppress(AttributeError): res[field.attname] = field - except AttributeError: - pass return res @cached_property @@ -535,10 +532,8 @@ class Options: # Due to the way Django's internals work, get_field() should also # be able to fetch a field by attname. In the case of a concrete # field with relation, includes the *_id name too - try: + with suppress(AttributeError): res[field.attname] = field - except AttributeError: - pass return res def get_field(self, field_name): @@ -755,12 +750,10 @@ class Options: # Creates a cache key composed of all arguments cache_key = (forward, reverse, include_parents, include_hidden, topmost_call) - try: + with suppress(KeyError): # In order to avoid list manipulation. Always return a shallow copy # of the results. return self._get_fields_cache[cache_key] - except KeyError: - pass fields = [] # Recursively call _get_fields() on each parent, with the same |
