Skip to content

Commit

Permalink
Fixed W503 flake8 warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Apr 4, 2016
1 parent d356bb6 commit 2cd2d18
Show file tree
Hide file tree
Showing 45 changed files with 166 additions and 187 deletions.
9 changes: 4 additions & 5 deletions django/contrib/admin/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ def check_dependencies(**kwargs):
pass
else:
if ('django.contrib.auth.context_processors.auth'
not in default_template_engine.context_processors
and 'django.contrib.auth.backends.ModelBackend'
in settings.AUTHENTICATION_BACKENDS):
not in default_template_engine.context_processors and
'django.contrib.auth.backends.ModelBackend' in settings.AUTHENTICATION_BACKENDS):
missing_template = checks.Error(
"'django.contrib.auth.context_processors.auth' must be in "
"TEMPLATES in order to use the admin application.",
Expand Down Expand Up @@ -801,8 +800,8 @@ def _check_list_editable_item(self, obj, model, field_name, label):
]
# If list_display[0] is in list_editable, check that
# list_display_links is set. See #22792 and #26229 for use cases.
elif (obj.list_display[0] == field_name and not obj.list_display_links
and obj.list_display_links is not None):
elif (obj.list_display[0] == field_name and not obj.list_display_links and
obj.list_display_links is not None):
return [
checks.Error(
"The value of '%s' refers to the first field in 'list_display' ('%s'), "
Expand Down
3 changes: 1 addition & 2 deletions django/contrib/admin/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,7 @@ def expected_parameters(self):

def choices(self, changelist):
yield {
'selected': (self.lookup_val is None
and self.lookup_val_isnull is None),
'selected': self.lookup_val is None and self.lookup_val_isnull is None,
'query_string': changelist.get_query_string({},
[self.lookup_kwarg, self.lookup_kwarg_isnull]),
'display': _('All'),
Expand Down
3 changes: 1 addition & 2 deletions django/contrib/admin/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,7 @@ def get_changelist_form(self, request, **kwargs):
"formfield_callback": partial(self.formfield_for_dbfield, request=request),
}
defaults.update(kwargs)
if (defaults.get('fields') is None
and not modelform_defines_fields(defaults.get('form'))):
if defaults.get('fields') is None and not modelform_defines_fields(defaults.get('form')):
defaults['fields'] = forms.ALL_FIELDS

return modelform_factory(self.model, **defaults)
Expand Down
3 changes: 1 addition & 2 deletions django/contrib/admin/views/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ def get_ordering(self, request, queryset):
ordering field.
"""
params = self.params
ordering = list(self.model_admin.get_ordering(request)
or self._get_default_ordering())
ordering = list(self.model_admin.get_ordering(request) or self._get_default_ordering())
if ORDER_VAR in params:
# Clear ordering and used params
ordering = []
Expand Down
3 changes: 1 addition & 2 deletions django/contrib/gis/db/backends/postgis/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@ def get_distance(self, f, dist_val, lookup_type, handle_spheroid=True):
# also handles it (#25524).
if handle_spheroid and len(dist_val) > 1:
option = dist_val[1]
if (not geography and geodetic and lookup_type != 'dwithin'
and option == 'spheroid'):
if not geography and geodetic and lookup_type != 'dwithin' and option == 'spheroid':
# using distance_spheroid requires the spheroid of the field as
# a parameter.
params.insert(0, f._spheroid)
Expand Down
4 changes: 2 additions & 2 deletions django/contrib/gis/serializers/geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def _init_options(self):
super(Serializer, self)._init_options()
self.geometry_field = self.json_kwargs.pop('geometry_field', None)
self.srid = self.json_kwargs.pop('srid', 4326)
if (self.selected_fields is not None and self.geometry_field is not None
and self.geometry_field not in self.selected_fields):
if (self.selected_fields is not None and self.geometry_field is not None and
self.geometry_field not in self.selected_fields):
self.selected_fields = list(self.selected_fields) + [self.geometry_field]

def start_serialization(self):
Expand Down
8 changes: 4 additions & 4 deletions django/contrib/postgres/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def __call__(self, value):

def __eq__(self, other):
return (
isinstance(other, self.__class__)
and (self.keys == other.keys)
and (self.messages == other.messages)
and (self.strict == other.strict)
isinstance(other, self.__class__) and
self.keys == other.keys and
self.messages == other.messages and
self.strict == other.strict
)

def __ne__(self, other):
Expand Down
16 changes: 6 additions & 10 deletions django/contrib/staticfiles/management/commands/collectstatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,12 @@ def delete_file(self, path, prefixed_path, source_storage):
full_path = None
# Skip the file if the source file is younger
# Avoid sub-second precision (see #14665, #19540)
if (target_last_modified.replace(microsecond=0)
>= source_last_modified.replace(microsecond=0)):
if not ((self.symlink and full_path
and not os.path.islink(full_path)) or
(not self.symlink and full_path
and os.path.islink(full_path))):
if prefixed_path not in self.unmodified_files:
self.unmodified_files.append(prefixed_path)
self.log("Skipping '%s' (not modified)" % path)
return False
if (target_last_modified.replace(microsecond=0) >= source_last_modified.replace(microsecond=0) and
full_path and not (self.symlink ^ os.path.islink(full_path))):
if prefixed_path not in self.unmodified_files:
self.unmodified_files.append(prefixed_path)
self.log("Skipping '%s' (not modified)" % path)
return False
# Then delete the existing file if really needed
if self.dry_run:
self.log("Pretending to delete '%s'" % path)
Expand Down
3 changes: 1 addition & 2 deletions django/core/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ def load_middleware(self):
def make_view_atomic(self, view):
non_atomic_requests = getattr(view, '_non_atomic_requests', set())
for db in connections.all():
if (db.settings_dict['ATOMIC_REQUESTS']
and db.alias not in non_atomic_requests):
if db.settings_dict['ATOMIC_REQUESTS'] and db.alias not in non_atomic_requests:
view = transaction.atomic(using=db.alias)(view)
return view

Expand Down
10 changes: 5 additions & 5 deletions django/core/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ def __call__(self, value):
def __eq__(self, other):
return (
isinstance(other, self.__class__) and
(self.limit_value == other.limit_value)
and (self.message == other.message)
and (self.code == other.code)
self.limit_value == other.limit_value and
self.message == other.message and
self.code == other.code
)

def compare(self, a, b):
Expand Down Expand Up @@ -435,8 +435,8 @@ def __call__(self, value):
code='max_decimal_places',
params={'max': self.decimal_places},
)
if (self.max_digits is not None and self.decimal_places is not None
and whole_digits > (self.max_digits - self.decimal_places)):
if (self.max_digits is not None and self.decimal_places is not None and
whole_digits > (self.max_digits - self.decimal_places)):
raise ValidationError(
self.messages['max_whole_digits'],
code='max_whole_digits',
Expand Down
14 changes: 7 additions & 7 deletions django/db/backends/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,8 @@ def set_autocommit(self, autocommit, force_begin_transaction_with_broken_autocom
self.ensure_connection()

start_transaction_under_autocommit = (
force_begin_transaction_with_broken_autocommit
and not autocommit
and self.features.autocommits_when_autocommit_is_off
force_begin_transaction_with_broken_autocommit and not autocommit and
self.features.autocommits_when_autocommit_is_off
)

if start_transaction_under_autocommit:
Expand Down Expand Up @@ -514,13 +513,14 @@ def validate_thread_sharing(self):
authorized to be shared between threads (via the `allow_thread_sharing`
property). Raises an exception if the validation fails.
"""
if not (self.allow_thread_sharing
or self._thread_ident == thread.get_ident()):
raise DatabaseError("DatabaseWrapper objects created in a "
if not (self.allow_thread_sharing or self._thread_ident == thread.get_ident()):
raise DatabaseError(
"DatabaseWrapper objects created in a "
"thread can only be used in that same thread. The object "
"with alias '%s' was created in thread id %s and this is "
"thread id %s."
% (self.alias, self._thread_ident, thread.get_ident()))
% (self.alias, self._thread_ident, thread.get_ident())
)

# ##### Miscellaneous #####

Expand Down
20 changes: 12 additions & 8 deletions django/db/backends/mysql/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,18 @@ def to_int(i):
for line in cursor.description:
col_name = force_text(line[0])
fields.append(
FieldInfo(*((col_name,)
+ line[1:3]
+ (to_int(field_info[col_name].max_len) or line[3],
to_int(field_info[col_name].num_prec) or line[4],
to_int(field_info[col_name].num_scale) or line[5])
+ (line[6],)
+ (field_info[col_name].extra,)
+ (field_info[col_name].column_default,)))
FieldInfo(*(
(col_name,) +
line[1:3] +
(
to_int(field_info[col_name].max_len) or line[3],
to_int(field_info[col_name].num_prec) or line[4],
to_int(field_info[col_name].num_scale) or line[5],
line[6],
field_info[col_name].extra,
field_info[col_name].column_default,
)
))
)
return fields

Expand Down
5 changes: 2 additions & 3 deletions django/db/backends/mysql/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ def check_field(self, field, **kwargs):
if field_type is None:
return errors

if (field_type.startswith('varchar') # Look for CharFields...
and field.unique # ... that are unique
and (field.max_length is None or int(field.max_length) > 255)):
if (field_type.startswith('varchar') and field.unique and
(field.max_length is None or int(field.max_length) > 255)):
errors.append(
checks.Error(
'MySQL does not allow unique CharFields to have a max_length > 255.',
Expand Down
5 changes: 3 additions & 2 deletions django/db/backends/oracle/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ def init_connection_state(self):
# TO_CHAR().
cursor.execute(
"ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
" NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'"
+ (" TIME_ZONE = 'UTC'" if settings.USE_TZ else ''))
" NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'" +
(" TIME_ZONE = 'UTC'" if settings.USE_TZ else '')
)
cursor.close()
if 'operators' not in self.__dict__:
# Ticket #14149: Check whether our LIKE implementation will
Expand Down
3 changes: 1 addition & 2 deletions django/db/backends/oracle/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def as_sql(self, with_limits=True, with_col_aliases=False, subquery=False):
"""
# The `do_offset` flag indicates whether we need to construct
# the SQL needed to use limit/offset with Oracle.
do_offset = with_limits and (self.query.high_mark is not None
or self.query.low_mark)
do_offset = with_limits and (self.query.high_mark is not None or self.query.low_mark)
if not do_offset:
sql, params = super(SQLCompiler, self).as_sql(
with_limits=False,
Expand Down
10 changes: 7 additions & 3 deletions django/db/backends/postgresql/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ def get_table_description(self, cursor, table_name):
WHERE table_name = %s""", [table_name])
field_map = {line[0]: line[1:] for line in cursor.fetchall()}
cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
return [FieldInfo(*((force_text(line[0]),) + line[1:6]
+ (field_map[force_text(line[0])][0] == 'YES', field_map[force_text(line[0])][1])))
for line in cursor.description]
return [
FieldInfo(*(
(force_text(line[0]),) +
line[1:6] +
(field_map[force_text(line[0])][0] == 'YES', field_map[force_text(line[0])][1])
)) for line in cursor.description
]

def get_relations(self, cursor, table_name):
"""
Expand Down
12 changes: 6 additions & 6 deletions django/db/migrations/autodetector.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ def _generate_through_model_map(self):
old_model_state = self.from_state.models[app_label, old_model_name]
for field_name, field in old_model_state.fields:
old_field = self.old_apps.get_model(app_label, old_model_name)._meta.get_field(field_name)
if (hasattr(old_field, "remote_field") and getattr(old_field.remote_field, "through", None)
and not old_field.remote_field.through._meta.auto_created):
if (hasattr(old_field, "remote_field") and getattr(old_field.remote_field, "through", None) and
not old_field.remote_field.through._meta.auto_created):
through_key = (
old_field.remote_field.through._meta.app_label,
old_field.remote_field.through._meta.model_name,
Expand Down Expand Up @@ -509,8 +509,8 @@ def generate_created_models(self):
related_fields[field.name] = field
# through will be none on M2Ms on swapped-out models;
# we can treat lack of through as auto_created=True, though.
if (getattr(field.remote_field, "through", None)
and not field.remote_field.through._meta.auto_created):
if (getattr(field.remote_field, "through", None) and
not field.remote_field.through._meta.auto_created):
related_fields[field.name] = field
for field in model_opts.local_many_to_many:
if field.remote_field.model:
Expand Down Expand Up @@ -671,8 +671,8 @@ def generate_deleted_models(self):
related_fields[field.name] = field
# through will be none on M2Ms on swapped-out models;
# we can treat lack of through as auto_created=True, though.
if (getattr(field.remote_field, "through", None)
and not field.remote_field.through._meta.auto_created):
if (getattr(field.remote_field, "through", None) and
not field.remote_field.through._meta.auto_created):
related_fields[field.name] = field
for field in model._meta.local_many_to_many:
if field.remote_field.model:
Expand Down
6 changes: 2 additions & 4 deletions django/db/migrations/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ def root_nodes(self, app=None):
"""
roots = set()
for node in self.nodes:
if (not any(key[0] == node[0] for key in self.node_map[node].parents)
and (not app or app == node[0])):
if not any(key[0] == node[0] for key in self.node_map[node].parents) and (not app or app == node[0]):
roots.add(node)
return sorted(roots)

Expand All @@ -221,8 +220,7 @@ def leaf_nodes(self, app=None):
"""
leaves = set()
for node in self.nodes:
if (not any(key[0] == node[0] for key in self.node_map[node].children)
and (not app or app == node[0])):
if not any(key[0] == node[0] for key in self.node_map[node].children) and (not app or app == node[0]):
leaves.add(node)
return sorted(leaves)

Expand Down
14 changes: 6 additions & 8 deletions django/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ def __init__(self, *args, **kwargs):
# data-descriptor object (DeferredAttribute) without triggering its
# __get__ method.
if (field.attname not in kwargs and
(isinstance(self.__class__.__dict__.get(field.attname), DeferredAttribute)
or field.column is None)):
(isinstance(self.__class__.__dict__.get(field.attname), DeferredAttribute) or
field.column is None)):
# This field will be populated on request.
continue
if kwargs:
Expand Down Expand Up @@ -753,8 +753,8 @@ def _save_parents(self, cls, using, update_fields):
meta = cls._meta
for parent, field in meta.parents.items():
# Make sure the link fields are synced between parent and self.
if (field and getattr(self, parent._meta.pk.attname) is None
and getattr(self, field.attname) is not None):
if (field and getattr(self, parent._meta.pk.attname) is None and
getattr(self, field.attname) is not None):
setattr(self, parent._meta.pk.attname, getattr(self, field.attname))
self._save_parents(cls=parent, using=using, update_fields=update_fields)
self._save_table(cls=parent, using=using, update_fields=update_fields)
Expand Down Expand Up @@ -1589,8 +1589,7 @@ def _check_long_column_names(cls):

# Check if auto-generated name for the field is too long
# for the database.
if (f.db_column is None and column_name is not None
and len(column_name) > allowed_len):
if f.db_column is None and column_name is not None and len(column_name) > allowed_len:
errors.append(
checks.Error(
'Autogenerated column name too long for field "%s". '
Expand All @@ -1607,8 +1606,7 @@ def _check_long_column_names(cls):
# for the database.
for m2m in f.remote_field.through._meta.local_fields:
_, rel_name = m2m.get_attname_column()
if (m2m.db_column is None and rel_name is not None
and len(rel_name) > allowed_len):
if m2m.db_column is None and rel_name is not None and len(rel_name) > allowed_len:
errors.append(
checks.Error(
'Autogenerated column name too long for M2M field '
Expand Down
6 changes: 3 additions & 3 deletions django/db/models/deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ def can_fast_delete(self, objs, from_field=None):
if not (hasattr(objs, 'model') and hasattr(objs, '_raw_delete')):
return False
model = objs.model
if (signals.pre_delete.has_listeners(model)
or signals.post_delete.has_listeners(model)
or signals.m2m_changed.has_listeners(model)):
if (signals.pre_delete.has_listeners(model) or
signals.post_delete.has_listeners(model) or
signals.m2m_changed.has_listeners(model)):
return False
# The use of from_field comes from the need to avoid cascade back to
# parent when parent delete is cascading to child.
Expand Down
4 changes: 2 additions & 2 deletions django/db/models/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,8 @@ def as_sql(self, compiler, connection):
except FieldError:
rhs_output = None
if (not connection.features.has_native_duration_field and
((lhs_output and lhs_output.get_internal_type() == 'DurationField')
or (rhs_output and rhs_output.get_internal_type() == 'DurationField'))):
((lhs_output and lhs_output.get_internal_type() == 'DurationField') or
(rhs_output and rhs_output.get_internal_type() == 'DurationField'))):
return DurationExpression(self.lhs, self.connector, self.rhs).as_sql(compiler, connection)
if (lhs_output and rhs_output and self.connector == self.SUB and
lhs_output.get_internal_type() in {'DateField', 'DateTimeField', 'TimeField'} and
Expand Down
Loading

0 comments on commit 2cd2d18

Please sign in to comment.