Fixed #28985 -- Removed unneeded None checks before hasattr().

This commit is contained in:
Дилян Палаузов 2018-01-03 11:34:10 -05:00 committed by Tim Graham
parent b3cd9fb18b
commit d79cf1e9e2
8 changed files with 14 additions and 17 deletions

View File

@ -281,7 +281,7 @@ def lookup_field(name, obj, model_admin=None):
if callable(name):
attr = name
value = attr(obj)
elif model_admin is not None and hasattr(model_admin, name) and name != '__str__':
elif hasattr(model_admin, name) and name != '__str__':
attr = getattr(model_admin, name)
value = attr(obj)
else:
@ -341,7 +341,7 @@ def label_for_field(name, model, model_admin=None, return_attr=False):
else:
if callable(name):
attr = name
elif model_admin is not None and hasattr(model_admin, name):
elif hasattr(model_admin, name):
attr = getattr(model_admin, name)
elif hasattr(model, name):
attr = getattr(model, name)

View File

@ -137,7 +137,7 @@ def logout(request):
# Dispatch the signal before the user is logged out so the receivers have a
# chance to find out *who* logged out.
user = getattr(request, 'user', None)
if hasattr(user, 'is_authenticated') and not user.is_authenticated:
if not getattr(user, 'is_authenticated', True):
user = None
user_logged_out.send(sender=user.__class__, request=request, user=user)

View File

@ -70,10 +70,10 @@ class FieldFile(File):
def open(self, mode='rb'):
self._require_file()
if hasattr(self, '_file') and self._file is not None:
self.file.open(mode)
else:
if getattr(self, '_file', None) is None:
self.file = self.storage.open(self.name, mode)
else:
self.file.open(mode)
return self
# open() doesn't alter the file's contents, but it does reset the pointer
open.alters_data = True

View File

@ -1349,8 +1349,7 @@ class ModelMultipleChoiceField(ModelChoiceField):
def modelform_defines_fields(form_class):
return (form_class is not None and (
hasattr(form_class, '_meta') and
(form_class._meta.fields is not None or
form_class._meta.exclude is not None)
))
return hasattr(form_class, '_meta') and (
form_class._meta.fields is not None or
form_class._meta.exclude is not None
)

View File

@ -52,9 +52,7 @@ class Element:
child.finalize()
def __eq__(self, element):
if not hasattr(element, 'name'):
return False
if hasattr(element, 'name') and self.name != element.name:
if not hasattr(element, 'name') or self.name != element.name:
return False
if len(self.attributes) != len(element.attributes):
return False

View File

@ -346,7 +346,7 @@ class ExceptionReporter:
Return (pre_context_lineno, pre_context, context_line, post_context).
"""
source = None
if loader is not None and hasattr(loader, "get_source"):
if hasattr(loader, 'get_source'):
try:
source = loader.get_source(module_name)
except ImportError:

View File

@ -145,7 +145,7 @@ class SingleObjectTemplateResponseMixin(TemplateResponseMixin):
object_meta.model_name,
self.template_name_suffix
))
elif hasattr(self, 'model') and self.model is not None and issubclass(self.model, models.Model):
elif getattr(self, 'model', None) is not None and issubclass(self.model, models.Model):
names.append("%s/%s%s.html" % (
self.model._meta.app_label,
self.model._meta.model_name,

View File

@ -83,7 +83,7 @@ class ModelFormMixin(FormMixin, SingleObjectMixin):
if self.model is not None:
# If a model has been explicitly provided, use it
model = self.model
elif hasattr(self, 'object') and self.object is not None:
elif getattr(self, 'object', None) is not None:
# If this view is operating on a single object, use
# the class of that object
model = self.object.__class__