diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-03 11:20:46 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-03 11:38:46 +0100 |
| commit | d55a1e5809b424907528af42bfdfc2991ef11651 (patch) | |
| tree | 605247aa8b905c148d6e5f480da3f0d2a2eec586 /django/db | |
| parent | 76c80d96f3828a5a3f66842932a5624674ba99a2 (diff) | |
[4.0.x] Refs #33476 -- Refactored problematic code before reformatting by Black.
In these cases Black produces unexpected results, e.g.
def make_random_password(
self,
length=10,
allowed_chars='abcdefghjkmnpqrstuvwxyz' 'ABCDEFGHJKLMNPQRSTUVWXYZ' '23456789',
):
or
cursor.execute("""
SELECT ...
""",
[table name],
)
Backport of c5cd8783825b5f6384417dac5f3889b4210b7d08 from main.
Diffstat (limited to 'django/db')
| -rw-r--r-- | django/db/backends/mysql/base.py | 3 | ||||
| -rw-r--r-- | django/db/backends/mysql/introspection.py | 36 | ||||
| -rw-r--r-- | django/db/backends/mysql/operations.py | 5 | ||||
| -rw-r--r-- | django/db/backends/oracle/introspection.py | 42 | ||||
| -rw-r--r-- | django/db/backends/oracle/operations.py | 14 | ||||
| -rw-r--r-- | django/db/backends/oracle/schema.py | 14 | ||||
| -rw-r--r-- | django/db/migrations/utils.py | 3 | ||||
| -rw-r--r-- | django/db/models/constraints.py | 3 | ||||
| -rw-r--r-- | django/db/models/fields/__init__.py | 3 | ||||
| -rw-r--r-- | django/db/models/indexes.py | 3 | ||||
| -rw-r--r-- | django/db/models/lookups.py | 3 | ||||
| -rw-r--r-- | django/db/models/query.py | 6 | ||||
| -rw-r--r-- | django/db/models/sql/query.py | 8 |
13 files changed, 91 insertions, 52 deletions
diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index 8b28a60485..e4c7313123 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -15,8 +15,7 @@ try: import MySQLdb as Database except ImportError as err: raise ImproperlyConfigured( - 'Error loading MySQLdb module.\n' - 'Did you install mysqlclient?' + 'Error loading MySQLdb module.\nDid you install mysqlclient?' ) from err from MySQLdb.constants import CLIENT, FIELD_TYPE diff --git a/django/db/backends/mysql/introspection.py b/django/db/backends/mysql/introspection.py index 2383c9ca1b..1e15fae383 100644 --- a/django/db/backends/mysql/introspection.py +++ b/django/db/backends/mysql/introspection.py @@ -79,22 +79,28 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): if self.connection.mysql_is_mariadb and self.connection.features.can_introspect_json_field: # JSON data type is an alias for LONGTEXT in MariaDB, select # JSON_VALID() constraints to introspect JSONField. - cursor.execute(""" + cursor.execute( + """ SELECT c.constraint_name AS column_name FROM information_schema.check_constraints AS c WHERE c.table_name = %s AND LOWER(c.check_clause) = 'json_valid(`' + LOWER(c.constraint_name) + '`)' AND c.constraint_schema = DATABASE() - """, [table_name]) + """, + [table_name], + ) json_constraints = {row[0] for row in cursor.fetchall()} # A default collation for the given table. - cursor.execute(""" + cursor.execute( + """ SELECT table_collation FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = %s - """, [table_name]) + """, + [table_name], + ) row = cursor.fetchone() default_column_collation = row[0] if row else '' # information_schema database gives more accurate results for some figures: @@ -102,7 +108,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): # not visible length (#5725) # - precision and scale (for decimal fields) (#5014) # - auto_increment is not available in cursor.description - cursor.execute(""" + cursor.execute( + """ SELECT column_name, data_type, character_maximum_length, numeric_precision, numeric_scale, extra, column_default, @@ -116,7 +123,9 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): END AS is_unsigned FROM information_schema.columns WHERE table_name = %s AND table_schema = DATABASE() - """, [default_column_collation, table_name]) + """, + [default_column_collation, table_name], + ) field_info = {line[0]: InfoLine(*line) for line in cursor.fetchall()} cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name)) @@ -165,13 +174,17 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): for all key columns in the given table. """ key_columns = [] - cursor.execute(""" + cursor.execute( + """ SELECT column_name, referenced_table_name, referenced_column_name FROM information_schema.key_column_usage WHERE table_name = %s AND table_schema = DATABASE() AND referenced_table_name IS NOT NULL - AND referenced_column_name IS NOT NULL""", [table_name]) + AND referenced_column_name IS NOT NULL + """, + [table_name], + ) key_columns.extend(cursor.fetchall()) return key_columns @@ -180,13 +193,16 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): Retrieve the storage engine for a given table. Return the default storage engine if the table doesn't exist. """ - cursor.execute(""" + cursor.execute( + """ SELECT engine FROM information_schema.tables WHERE table_name = %s AND table_schema = DATABASE() - """, [table_name]) + """, + [table_name], + ) result = cursor.fetchone() if not result: return self.connection.features._mysql_storage_engine diff --git a/django/db/backends/mysql/operations.py b/django/db/backends/mysql/operations.py index 3e1faad41b..5eede7cc84 100644 --- a/django/db/backends/mysql/operations.py +++ b/django/db/backends/mysql/operations.py @@ -231,8 +231,9 @@ class DatabaseOperations(BaseDatabaseOperations): # Zero in AUTO_INCREMENT field does not work without the # NO_AUTO_VALUE_ON_ZERO SQL mode. if value == 0 and not self.connection.features.allows_auto_pk_0: - raise ValueError('The database backend does not accept 0 as a ' - 'value for AutoField.') + raise ValueError( + 'The database backend does not accept 0 as a value for AutoField.' + ) return value def adapt_datetimefield_value(self, value): diff --git a/django/db/backends/oracle/introspection.py b/django/db/backends/oracle/introspection.py index fa7a34ed0a..4ea8162a63 100644 --- a/django/db/backends/oracle/introspection.py +++ b/django/db/backends/oracle/introspection.py @@ -93,7 +93,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): interface. """ # user_tab_columns gives data default for columns - cursor.execute(""" + cursor.execute( + """ SELECT user_tab_cols.column_name, user_tab_cols.data_default, @@ -126,7 +127,9 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): LEFT OUTER JOIN user_tables ON user_tables.table_name = user_tab_cols.table_name WHERE user_tab_cols.table_name = UPPER(%s) - """, [table_name]) + """, + [table_name], + ) field_map = { column: (internal_size, default if default != 'NULL' else None, collation, is_autofield, is_json) for column, default, collation, internal_size, is_autofield, is_json in cursor.fetchall() @@ -151,7 +154,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): return name.lower() def get_sequences(self, cursor, table_name, table_fields=()): - cursor.execute(""" + cursor.execute( + """ SELECT user_tab_identity_cols.sequence_name, user_tab_identity_cols.column_name @@ -165,7 +169,9 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): AND cols.column_name = user_tab_identity_cols.column_name AND user_constraints.constraint_type = 'P' AND user_tab_identity_cols.table_name = UPPER(%s) - """, [table_name]) + """, + [table_name], + ) # Oracle allows only one identity column per table. row = cursor.fetchone() if row: @@ -217,7 +223,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): ] def get_primary_key_column(self, cursor, table_name): - cursor.execute(""" + cursor.execute( + """ SELECT cols.column_name FROM @@ -228,7 +235,9 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): user_constraints.constraint_type = 'P' AND user_constraints.table_name = UPPER(%s) AND cols.position = 1 - """, [table_name]) + """, + [table_name], + ) row = cursor.fetchone() return self.identifier_converter(row[0]) if row else None @@ -239,7 +248,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): """ constraints = {} # Loop over the constraints, getting PKs, uniques, and checks - cursor.execute(""" + cursor.execute( + """ SELECT user_constraints.constraint_name, LISTAGG(LOWER(cols.column_name), ',') WITHIN GROUP (ORDER BY cols.position), @@ -263,7 +273,9 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): user_constraints.constraint_type = ANY('P', 'U', 'C') AND user_constraints.table_name = UPPER(%s) GROUP BY user_constraints.constraint_name, user_constraints.constraint_type - """, [table_name]) + """, + [table_name], + ) for constraint, columns, pk, unique, check in cursor.fetchall(): constraint = self.identifier_converter(constraint) constraints[constraint] = { @@ -275,7 +287,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): 'index': unique, # All uniques come with an index } # Foreign key constraints - cursor.execute(""" + cursor.execute( + """ SELECT cons.constraint_name, LISTAGG(LOWER(cols.column_name), ',') WITHIN GROUP (ORDER BY cols.position), @@ -291,7 +304,9 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): cons.constraint_type = 'R' AND cons.table_name = UPPER(%s) GROUP BY cons.constraint_name, rcols.table_name, rcols.column_name - """, [table_name]) + """, + [table_name], + ) for constraint, columns, other_table, other_column in cursor.fetchall(): constraint = self.identifier_converter(constraint) constraints[constraint] = { @@ -303,7 +318,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): 'columns': columns.split(','), } # Now get indexes - cursor.execute(""" + cursor.execute( + """ SELECT ind.index_name, LOWER(ind.index_type), @@ -320,7 +336,9 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): WHERE ind.index_name = cons.index_name ) AND cols.index_name = ind.index_name GROUP BY ind.index_name, ind.index_type, ind.uniqueness - """, [table_name]) + """, + [table_name], + ) for constraint, type_, unique, columns, orders in cursor.fetchall(): constraint = self.identifier_converter(constraint) constraints[constraint] = { diff --git a/django/db/backends/oracle/operations.py b/django/db/backends/oracle/operations.py index f497390bea..e530028cbf 100644 --- a/django/db/backends/oracle/operations.py +++ b/django/db/backends/oracle/operations.py @@ -372,7 +372,8 @@ END; def __foreign_key_constraints(self, table_name, recursive): with self.connection.cursor() as cursor: if recursive: - cursor.execute(""" + cursor.execute( + """ SELECT user_tables.table_name, rcons.constraint_name FROM @@ -389,9 +390,12 @@ END; user_tables.table_name, rcons.constraint_name HAVING user_tables.table_name != UPPER(%s) ORDER BY MAX(level) DESC - """, (table_name, table_name)) + """, + (table_name, table_name), + ) else: - cursor.execute(""" + cursor.execute( + """ SELECT cons.table_name, cons.constraint_name FROM @@ -399,7 +403,9 @@ END; WHERE cons.constraint_type = 'R' AND cons.table_name = UPPER(%s) - """, (table_name,)) + """, + (table_name,), + ) return cursor.fetchall() @cached_property diff --git a/django/db/backends/oracle/schema.py b/django/db/backends/oracle/schema.py index 0d1c7f5d4b..da64fc31a5 100644 --- a/django/db/backends/oracle/schema.py +++ b/django/db/backends/oracle/schema.py @@ -182,13 +182,16 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): def _is_identity_column(self, table_name, column_name): with self.connection.cursor() as cursor: - cursor.execute(""" + cursor.execute( + """ SELECT CASE WHEN identity_column = 'YES' THEN 1 ELSE 0 END FROM user_tab_cols WHERE table_name = %s AND column_name = %s - """, [self.normalize_name(table_name), self.normalize_name(column_name)]) + """, + [self.normalize_name(table_name), self.normalize_name(column_name)], + ) row = cursor.fetchone() return row[0] if row else False @@ -200,9 +203,12 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): def _get_default_collation(self, table_name): with self.connection.cursor() as cursor: - cursor.execute(""" + cursor.execute( + """ SELECT default_collation FROM user_tables WHERE table_name = %s - """, [self.normalize_name(table_name)]) + """, + [self.normalize_name(table_name)], + ) return cursor.fetchone()[0] def _alter_column_collation_sql(self, model, new_field, new_type, new_collation): diff --git a/django/db/migrations/utils.py b/django/db/migrations/utils.py index 97bd96a90a..42a4d90340 100644 --- a/django/db/migrations/utils.py +++ b/django/db/migrations/utils.py @@ -42,8 +42,7 @@ def resolve_relation(model, app_label=None, model_name=None): return app_label, model_name.lower() if app_label is None: raise TypeError( - 'app_label must be provided to resolve unscoped model ' - 'relationships.' + 'app_label must be provided to resolve unscoped model relationships.' ) return app_label, model.lower() return model._meta.app_label, model._meta.model_name diff --git a/django/db/models/constraints.py b/django/db/models/constraints.py index d36d076346..5abedaf3d1 100644 --- a/django/db/models/constraints.py +++ b/django/db/models/constraints.py @@ -40,8 +40,7 @@ class CheckConstraint(BaseConstraint): self.check = check if not getattr(check, 'conditional', False): raise TypeError( - 'CheckConstraint.check must be a Q instance or boolean ' - 'expression.' + 'CheckConstraint.check must be a Q instance or boolean expression.' ) super().__init__(name) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 0e72a09e59..4d2ac900f8 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -101,8 +101,7 @@ class Field(RegisterLookupMixin): 'invalid_choice': _('Value %(value)r is not a valid choice.'), 'null': _('This field cannot be null.'), 'blank': _('This field cannot be blank.'), - 'unique': _('%(model_name)s with this %(field_label)s ' - 'already exists.'), + 'unique': _('%(model_name)s with this %(field_label)s already exists.'), # Translators: The 'lookup_type' is one of 'date', 'year' or 'month'. # Eg: "Title must be unique for pub_date year" 'unique_for_date': _("%(field_label)s must be unique for " diff --git a/django/db/models/indexes.py b/django/db/models/indexes.py index 9c393ca2c0..e843f9a8cb 100644 --- a/django/db/models/indexes.py +++ b/django/db/models/indexes.py @@ -36,8 +36,7 @@ class Index: raise ValueError('Index.opclasses must be a list or tuple.') if not expressions and not fields: raise ValueError( - 'At least one field or expression is required to define an ' - 'index.' + 'At least one field or expression is required to define an index.' ) if expressions and fields: raise ValueError( diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py index 0f104416de..04c4a4863c 100644 --- a/django/db/models/lookups.py +++ b/django/db/models/lookups.py @@ -541,8 +541,7 @@ class IsNull(BuiltinLookup): def as_sql(self, compiler, connection): if not isinstance(self.rhs, bool): raise ValueError( - 'The QuerySet value for an isnull lookup must be True or ' - 'False.' + 'The QuerySet value for an isnull lookup must be True or False.' ) sql, params = compiler.compile(self.lhs) if self.rhs: diff --git a/django/db/models/query.py b/django/db/models/query.py index 1bc8e2ed2a..b3b81c22f2 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -820,8 +820,7 @@ class QuerySet: self._not_support_combined_queries('contains') if self._fields is not None: raise TypeError( - 'Cannot call QuerySet.contains() after .values() or ' - '.values_list().' + 'Cannot call QuerySet.contains() after .values() or .values_list().' ) try: if obj._meta.concrete_model != self.model._meta.concrete_model: @@ -1611,8 +1610,7 @@ class Prefetch: ) ): raise ValueError( - 'Prefetch querysets cannot use raw(), values(), and ' - 'values_list().' + 'Prefetch querysets cannot use raw(), values(), and values_list().' ) if to_attr: self.prefetch_to = LOOKUP_SEP.join(lookup.split(LOOKUP_SEP)[:-1] + [to_attr]) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 6436b25bae..3c18fcb4c7 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1706,8 +1706,7 @@ class Query(BaseExpression): for alias in self._gen_col_aliases([annotation]): if isinstance(self.alias_map[alias], Join): raise FieldError( - 'Joined field references are not permitted in ' - 'this query' + 'Joined field references are not permitted in this query' ) if summarize: # Summarize currently means we are doing an aggregate() query @@ -1734,8 +1733,9 @@ class Query(BaseExpression): if not allow_joins and len(join_list) > 1: raise FieldError('Joined field references are not permitted in this query') if len(targets) > 1: - raise FieldError("Referencing multicolumn fields with F() objects " - "isn't supported") + raise FieldError( + "Referencing multicolumn fields with F() objects isn't supported" + ) # Verify that the last lookup in name is a field or a transform: # transform_function() raises FieldError if not. transform = join_info.transform_function(targets[0], final_alias) |
