diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py index 58867b4fca..f5a1ff2ec8 100644 --- a/django/contrib/contenttypes/models.py +++ b/django/contrib/contenttypes/models.py @@ -55,7 +55,10 @@ class ContentTypeManager(models.Manager): # It's possible to migrate a single app before contenttypes, # as it's not a required initial dependency (it's contrib!) # Have a nice error for this. - raise RuntimeError("Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually.") + raise RuntimeError( + "Error creating new content types. Please make sure contenttypes " + "is migrated before trying to migrate apps individually." + ) except self.model.DoesNotExist: # Not found in the database; we proceed to create it. This time we # use get_or_create to take care of any race conditions. diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py index 972c978c6f..574f99f4c6 100644 --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -162,7 +162,8 @@ class MigrationAutodetector(object): 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_by_name(field_name)[0] - if hasattr(old_field, "rel") and getattr(old_field.rel, "through", None) and not old_field.rel.through._meta.auto_created: + if (hasattr(old_field, "rel") and getattr(old_field.rel, "through", None) + and not old_field.rel.through._meta.auto_created): through_key = ( old_field.rel.through._meta.app_label, old_field.rel.through._meta.object_name.lower(), @@ -961,7 +962,8 @@ class MigrationAutodetector(object): old_model_name = self.renamed_models.get((app_label, model_name), model_name) old_model_state = self.from_state.models[app_label, old_model_name] new_model_state = self.to_state.models[app_label, model_name] - if old_model_state.options.get("order_with_respect_to", None) != new_model_state.options.get("order_with_respect_to", None): + if (old_model_state.options.get("order_with_respect_to", None) != + new_model_state.options.get("order_with_respect_to", None)): # Make sure it comes second if we're adding # (removal dependency is part of RemoveField) dependencies = [] diff --git a/django/db/migrations/graph.py b/django/db/migrations/graph.py index 4330c2b4a5..56b78d9056 100644 --- a/django/db/migrations/graph.py +++ b/django/db/migrations/graph.py @@ -37,9 +37,13 @@ class MigrationGraph(object): def add_dependency(self, migration, child, parent): if child not in self.nodes: - raise KeyError("Migration %s dependencies reference nonexistent child node %r" % (migration, child)) + raise KeyError( + "Migration %s dependencies reference nonexistent child node %r" % (migration, child) + ) if parent not in self.nodes: - raise KeyError("Migration %s dependencies reference nonexistent parent node %r" % (migration, parent)) + raise KeyError( + "Migration %s dependencies reference nonexistent parent node %r" % (migration, parent) + ) self.dependencies.setdefault(child, set()).add(parent) self.dependents.setdefault(parent, set()).add(child) @@ -72,7 +76,8 @@ class MigrationGraph(object): """ roots = set() for node in self.nodes: - if not any(key[0] == node[0] for key in self.dependencies.get(node, set())) and (not app or app == node[0]): + if (not any(key[0] == node[0] for key in self.dependencies.get(node, set())) + and (not app or app == node[0])): roots.add(node) return sorted(roots) @@ -86,7 +91,8 @@ class MigrationGraph(object): """ leaves = set() for node in self.nodes: - if not any(key[0] == node[0] for key in self.dependents.get(node, set())) and (not app or app == node[0]): + if (not any(key[0] == node[0] for key in self.dependents.get(node, set())) + and (not app or app == node[0])): leaves.add(node) return sorted(leaves) @@ -116,7 +122,10 @@ class MigrationGraph(object): return list(OrderedSet(visited)) def __str__(self): - return "Graph: %s nodes, %s edges" % (len(self.nodes), sum(len(x) for x in self.dependencies.values())) + return "Graph: %s nodes, %s edges" % ( + len(self.nodes), + sum(len(x) for x in self.dependencies.values()), + ) def make_state(self, nodes=None, at_end=True, real_apps=None): """ diff --git a/django/db/migrations/loader.py b/django/db/migrations/loader.py index 64d24adef8..408649847b 100644 --- a/django/db/migrations/loader.py +++ b/django/db/migrations/loader.py @@ -106,7 +106,9 @@ class MigrationLoader(object): break raise if not hasattr(migration_module, "Migration"): - raise BadMigrationError("Migration %s in app %s has no Migration class" % (migration_name, app_config.label)) + raise BadMigrationError( + "Migration %s in app %s has no Migration class" % (migration_name, app_config.label) + ) # Ignore South-style migrations if hasattr(migration_module.Migration, "forwards"): south_style_migrations = True @@ -127,7 +129,9 @@ class MigrationLoader(object): if l == app_label and n.startswith(name_prefix): results.append((l, n)) if len(results) > 1: - raise AmbiguityError("There is more than one migration for '%s' with the prefix '%s'" % (app_label, name_prefix)) + raise AmbiguityError( + "There is more than one migration for '%s' with the prefix '%s'" % (app_label, name_prefix) + ) elif len(results) == 0: raise KeyError("There no migrations for '%s' with the prefix '%s'" % (app_label, name_prefix)) else: diff --git a/django/db/migrations/migration.py b/django/db/migrations/migration.py index 34878b459e..80bdb2f126 100644 --- a/django/db/migrations/migration.py +++ b/django/db/migrations/migration.py @@ -90,7 +90,8 @@ class Migration(object): # there instead if collect_sql and not operation.reduces_to_sql: schema_editor.collected_sql.append("--") - schema_editor.collected_sql.append("-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE WRITTEN AS SQL:") + schema_editor.collected_sql.append("-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE " + "WRITTEN AS SQL:") schema_editor.collected_sql.append("-- %s" % operation.describe()) schema_editor.collected_sql.append("--") continue @@ -122,7 +123,8 @@ class Migration(object): # there instead if collect_sql and not operation.reduces_to_sql: schema_editor.collected_sql.append("--") - schema_editor.collected_sql.append("-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE WRITTEN AS SQL:") + schema_editor.collected_sql.append("-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE " + "WRITTEN AS SQL:") schema_editor.collected_sql.append("-- %s" % operation.describe()) schema_editor.collected_sql.append("--") continue diff --git a/django/db/migrations/operations/fields.py b/django/db/migrations/operations/fields.py index 500a8397cb..650d532c2c 100644 --- a/django/db/migrations/operations/fields.py +++ b/django/db/migrations/operations/fields.py @@ -174,7 +174,8 @@ class RenameField(Operation): def state_forwards(self, app_label, state): # Rename the field state.models[app_label, self.model_name.lower()].fields = [ - (self.new_name if n == self.old_name else n, f) for n, f in state.models[app_label, self.model_name.lower()].fields + (self.new_name if n == self.old_name else n, f) + for n, f in state.models[app_label, self.model_name.lower()].fields ] # Fix unique_together to refer to the new field options = state.models[app_label, self.model_name.lower()].options diff --git a/django/db/migrations/optimizer.py b/django/db/migrations/optimizer.py index 6c5dd8cbf7..d48ebcf90c 100644 --- a/django/db/migrations/optimizer.py +++ b/django/db/migrations/optimizer.py @@ -300,7 +300,8 @@ class MigrationOptimizer(object): return [other] def reduce_add_field_rename_field(self, operation, other, in_between): - if operation.model_name.lower() == other.model_name.lower() and operation.name.lower() == other.old_name.lower(): + if (operation.model_name.lower() == other.model_name.lower() and + operation.name.lower() == other.old_name.lower()): return [ migrations.AddField( model_name=operation.model_name, @@ -310,7 +311,8 @@ class MigrationOptimizer(object): ] def reduce_alter_field_rename_field(self, operation, other, in_between): - if operation.model_name.lower() == other.model_name.lower() and operation.name.lower() == other.old_name.lower(): + if (operation.model_name.lower() == other.model_name.lower() and + operation.name.lower() == other.old_name.lower()): return [ other, migrations.AlterField( @@ -321,7 +323,8 @@ class MigrationOptimizer(object): ] def reduce_rename_field_self(self, operation, other, in_between): - if operation.model_name.lower() == other.model_name.lower() and operation.new_name.lower() == other.old_name.lower(): + if (operation.model_name.lower() == other.model_name.lower() and + operation.new_name.lower() == other.old_name.lower()): return [ migrations.RenameField( operation.model_name, diff --git a/django/db/migrations/questioner.py b/django/db/migrations/questioner.py index 54a4c80635..5928e3dfab 100644 --- a/django/db/migrations/questioner.py +++ b/django/db/migrations/questioner.py @@ -163,11 +163,15 @@ class InteractiveMigrationQuestioner(MigrationQuestioner): def ask_rename(self, model_name, old_name, new_name, field_instance): "Was this field really renamed?" - return self._boolean_input("Did you rename %s.%s to %s.%s (a %s)? [y/N]" % (model_name, old_name, model_name, new_name, field_instance.__class__.__name__), False) + msg = "Did you rename %s.%s to %s.%s (a %s)? [y/N]" + return self._boolean_input(msg % (model_name, old_name, model_name, new_name, + field_instance.__class__.__name__), False) def ask_rename_model(self, old_model_state, new_model_state): "Was this model really renamed?" - return self._boolean_input("Did you rename the %s.%s model to %s? [y/N]" % (old_model_state.app_label, old_model_state.name, new_model_state.name), False) + msg = "Did you rename the %s.%s model to %s? [y/N]" + return self._boolean_input(msg % (old_model_state.app_label, old_model_state.name, + new_model_state.name), False) def ask_merge(self, app_label): return self._boolean_input( diff --git a/django/db/migrations/state.py b/django/db/migrations/state.py index c6386d2b9b..39da247eca 100644 --- a/django/db/migrations/state.py +++ b/django/db/migrations/state.py @@ -68,7 +68,12 @@ class ProjectState(object): except InvalidBasesError: new_unrendered_models.append(model) if len(new_unrendered_models) == len(unrendered_models): - raise InvalidBasesError("Cannot resolve bases for %r\nThis can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)\n in an app with no migrations; see https://docs.djangoproject.com/en/1.7/topics/migrations/#dependencies for more" % new_unrendered_models) + raise InvalidBasesError( + "Cannot resolve bases for %r\nThis can happen if you are inheriting models from an " + "app with migrations (e.g. contrib.auth)\n in an app with no migrations; see " + "https://docs.djangoproject.com/en/1.7/topics/migrations/#dependencies " + "for more" % new_unrendered_models + ) unrendered_models = new_unrendered_models # make sure apps has no dangling references if self.apps._pending_lookups: @@ -78,16 +83,15 @@ class ProjectState(object): try: model = self.apps.get_model(lookup_model[0], lookup_model[1]) except LookupError: - if "%s.%s" % (lookup_model[0], lookup_model[1]) == settings.AUTH_USER_MODEL and ignore_swappable: + app_label = "%s.%s" % (lookup_model[0], lookup_model[1]) + if app_label == settings.AUTH_USER_MODEL and ignore_swappable: continue # Raise an error with a best-effort helpful message # (only for the first issue). Error message should look like: # "ValueError: Lookup failed for model referenced by # field migrations.Book.author: migrations.Author" - raise ValueError("Lookup failed for model referenced by field {field}: {model[0]}.{model[1]}".format( - field=operations[0][1], - model=lookup_model, - )) + msg = "Lookup failed for model referenced by field {field}: {model[0]}.{model[1]}" + raise ValueError(msg.format(field=operations[0][1], model=lookup_model)) else: do_pending_lookups(model) try: @@ -329,7 +333,8 @@ class ModelState(object): (self.app_label == other.app_label) and (self.name == other.name) and (len(self.fields) == len(other.fields)) and - all((k1 == k2 and (f1.deconstruct()[1:] == f2.deconstruct()[1:])) for (k1, f1), (k2, f2) in zip(self.fields, other.fields)) and + all((k1 == k2 and (f1.deconstruct()[1:] == f2.deconstruct()[1:])) + for (k1, f1), (k2, f2) in zip(self.fields, other.fields)) and (self.options == other.options) and (self.bases == other.bases) ) diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index 17975409d1..3e4faca833 100644 --- a/django/db/migrations/writer.py +++ b/django/db/migrations/writer.py @@ -155,10 +155,12 @@ class MigrationWriter(object): imports.discard("from django.db import models") items["imports"] = "\n".join(imports) + "\n" if imports else "" if migration_imports: - items["imports"] += "\n\n# Functions from the following migrations need manual copying.\n# Move them and any dependencies into this file, then update the\n# RunPython operations to refer to the local versions:\n# %s" % ( - "\n# ".join(migration_imports) - ) - + items["imports"] += ( + "\n\n# Functions from the following migrations need manual " + "copying.\n# Move them and any dependencies into this file, " + "then update the\n# RunPython operations to refer to the local " + "versions:\n# %s" + ) % "\n# ".join(migration_imports) # If there's a replaces, make a string for it if self.migration.replaces: items['replaces_str'] = "\n replaces = %s\n" % self.serialize(self.migration.replaces)[0] @@ -396,7 +398,11 @@ class MigrationWriter(object): return "re.compile(%s)" % ', '.join(args), imports # Uh oh. else: - raise ValueError("Cannot serialize: %r\nThere are some values Django cannot serialize into migration files.\nFor more, see https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing" % value) + raise ValueError( + "Cannot serialize: %r\nThere are some values Django cannot serialize into " + "migration files.\nFor more, see https://docs.djangoproject.com/en/dev/" + "topics/migrations/#migration-serializing" % value + ) MIGRATION_TEMPLATE = """\