Skip to content

Commit 144bdc2

Browse files
authored
fix: Fix black, isort compatibility (#469)
1 parent e017901 commit 144bdc2

31 files changed

+2973
-2472
lines changed

django_spanner/__init__.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
# https://developers.google.com/open-source/licenses/bsd
66

77
import datetime
8+
89
# Monkey-patch AutoField to generate a random value since Cloud Spanner can't
910
# do that.
1011
from uuid import uuid4
1112

1213
import pkg_resources
1314
from django.db.models.fields import AutoField, Field
14-
# Monkey-patch google.DatetimeWithNanoseconds's __eq__ compare against datetime.datetime.
15+
16+
# Monkey-patch google.DatetimeWithNanoseconds's __eq__ compare against
17+
# datetime.datetime.
1518
from google.api_core.datetime_helpers import DatetimeWithNanoseconds
1619

1720
from .expressions import register_expressions
@@ -33,14 +36,16 @@ def gen_rand_int64():
3336

3437

3538
def autofield_init(self, *args, **kwargs):
36-
kwargs['blank'] = True
39+
kwargs["blank"] = True
3740
Field.__init__(self, *args, **kwargs)
3841
self.default = gen_rand_int64
3942

4043

4144
AutoField.__init__ = autofield_init
4245

43-
old_datetimewithnanoseconds_eq = getattr(DatetimeWithNanoseconds, '__eq__', None)
46+
old_datetimewithnanoseconds_eq = getattr(
47+
DatetimeWithNanoseconds, "__eq__", None
48+
)
4449

4550

4651
def datetimewithnanoseconds_eq(self, other):
@@ -62,12 +67,13 @@ def datetimewithnanoseconds_eq(self, other):
6267
DatetimeWithNanoseconds.__eq__ = datetimewithnanoseconds_eq
6368

6469
# Sanity check here since tests can't easily be run for this file:
65-
if __name__ == '__main__':
70+
if __name__ == "__main__":
6671
from django.utils import timezone
72+
6773
UTC = timezone.utc
6874

6975
dt = datetime.datetime(2020, 1, 10, 2, 44, 57, 999, UTC)
7076
dtns = DatetimeWithNanoseconds(2020, 1, 10, 2, 44, 57, 999, UTC)
7177
equal = dtns == dt
7278
if not equal:
73-
raise Exception('%s\n!=\n%s' % (dtns, dt))
79+
raise Exception("%s\n!=\n%s" % (dtns, dt))

django_spanner/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ def instance(self):
110110

111111
@property
112112
def _nodb_connection(self):
113-
raise NotImplementedError('Spanner does not have a "no db" connection.')
113+
raise NotImplementedError(
114+
'Spanner does not have a "no db" connection.'
115+
)
114116

115117
def get_connection_params(self):
116118
return {

django_spanner/client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99

1010
class DatabaseClient(BaseDatabaseClient):
1111
def runshell(self):
12-
raise NotImplementedError('dbshell is not implemented.')
12+
raise NotImplementedError("dbshell is not implemented.")

django_spanner/compiler.py

+42-17
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@
77
from django.core.exceptions import EmptyResultSet
88
from django.db.models.sql.compiler import (
99
SQLAggregateCompiler as BaseSQLAggregateCompiler,
10-
SQLCompiler as BaseSQLCompiler, SQLDeleteCompiler as BaseSQLDeleteCompiler,
10+
)
11+
from django.db.models.sql.compiler import SQLCompiler as BaseSQLCompiler
12+
from django.db.models.sql.compiler import (
13+
SQLDeleteCompiler as BaseSQLDeleteCompiler,
14+
)
15+
from django.db.models.sql.compiler import (
1116
SQLInsertCompiler as BaseSQLInsertCompiler,
17+
)
18+
from django.db.models.sql.compiler import (
1219
SQLUpdateCompiler as BaseSQLUpdateCompiler,
1320
)
1421
from django.db.utils import DatabaseError
@@ -24,50 +31,68 @@ def get_combinator_sql(self, combinator, all):
2431
features = self.connection.features
2532
compilers = [
2633
query.get_compiler(self.using, self.connection)
27-
for query in self.query.combined_queries if not query.is_empty()
34+
for query in self.query.combined_queries
35+
if not query.is_empty()
2836
]
2937
if not features.supports_slicing_ordering_in_compound:
3038
for query, compiler in zip(self.query.combined_queries, compilers):
3139
if query.low_mark or query.high_mark:
32-
raise DatabaseError('LIMIT/OFFSET not allowed in subqueries of compound statements.')
40+
raise DatabaseError(
41+
"LIMIT/OFFSET not allowed in subqueries of compound "
42+
"statements."
43+
)
3344
if compiler.get_order_by():
34-
raise DatabaseError('ORDER BY not allowed in subqueries of compound statements.')
45+
raise DatabaseError(
46+
"ORDER BY not allowed in subqueries of compound "
47+
"statements."
48+
)
3549
parts = ()
3650
for compiler in compilers:
3751
try:
3852
# If the columns list is limited, then all combined queries
3953
# must have the same columns list. Set the selects defined on
4054
# the query on all combined queries, if not already set.
41-
if not compiler.query.values_select and self.query.values_select:
42-
compiler.query.set_values((
43-
*self.query.extra_select,
44-
*self.query.values_select,
45-
*self.query.annotation_select,
46-
))
55+
if (
56+
not compiler.query.values_select
57+
and self.query.values_select
58+
):
59+
compiler.query.set_values(
60+
(
61+
*self.query.extra_select,
62+
*self.query.values_select,
63+
*self.query.annotation_select,
64+
)
65+
)
4766
part_sql, part_args = compiler.as_sql()
4867
if compiler.query.combinator:
4968
# Wrap in a subquery if wrapping in parentheses isn't
5069
# supported.
5170
if not features.supports_parentheses_in_compound:
52-
part_sql = 'SELECT * FROM ({})'.format(part_sql)
71+
part_sql = "SELECT * FROM ({})".format(part_sql)
5372
# Add parentheses when combining with compound query if not
5473
# already added for all compound queries.
5574
elif not features.supports_slicing_ordering_in_compound:
56-
part_sql = '({})'.format(part_sql)
75+
part_sql = "({})".format(part_sql)
5776
parts += ((part_sql, part_args),)
5877
except EmptyResultSet:
5978
# Omit the empty queryset with UNION and with DIFFERENCE if the
6079
# first queryset is nonempty.
61-
if combinator == 'union' or (combinator == 'difference' and parts):
80+
if combinator == "union" or (
81+
combinator == "difference" and parts
82+
):
6283
continue
6384
raise
6485
if not parts:
6586
raise EmptyResultSet
6687
combinator_sql = self.connection.ops.set_operators[combinator]
67-
combinator_sql += ' ALL' if all else ' DISTINCT'
68-
braces = '({})' if features.supports_slicing_ordering_in_compound else '{}'
69-
sql_parts, args_parts = zip(*((braces.format(sql), args) for sql, args in parts))
70-
result = [' {} '.format(combinator_sql).join(sql_parts)]
88+
combinator_sql += " ALL" if all else " DISTINCT"
89+
braces = (
90+
"({})" if features.supports_slicing_ordering_in_compound else "{}"
91+
)
92+
sql_parts, args_parts = zip(
93+
*((braces.format(sql), args) for sql, args in parts)
94+
)
95+
result = [" {} ".format(combinator_sql).join(sql_parts)]
7196
params = []
7297
for part in args_parts:
7398
params.extend(part)

django_spanner/creation.py

+26-14
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@ class DatabaseCreation(BaseDatabaseCreation):
1717
def mark_skips(self):
1818
"""Skip tests that don't work on Spanner."""
1919
for test_name in self.connection.features.skip_tests:
20-
test_case_name, _, method_name = test_name.rpartition('.')
21-
test_app = test_name.split('.')[0]
20+
test_case_name, _, method_name = test_name.rpartition(".")
21+
test_app = test_name.split(".")[0]
2222
# Importing a test app that isn't installed raises RuntimeError.
2323
if test_app in settings.INSTALLED_APPS:
2424
test_case = import_string(test_case_name)
2525
method = getattr(test_case, method_name)
26-
setattr(test_case, method_name, skip('unsupported by Spanner')(method))
26+
setattr(
27+
test_case,
28+
method_name,
29+
skip("unsupported by Spanner")(method),
30+
)
2731

2832
def create_test_db(self, *args, **kwargs):
2933
# This environment variable is set by the Travis build script or
3034
# by a developer running the tests locally.
31-
if os.environ.get('RUNNING_SPANNER_BACKEND_TESTS') == '1':
35+
if os.environ.get("RUNNING_SPANNER_BACKEND_TESTS") == "1":
3236
self.mark_skips()
3337
super().create_test_db(*args, **kwargs)
3438

@@ -38,7 +42,7 @@ def _create_test_db(self, verbosity, autoclobber, keepdb=False):
3842
test_database_name = self._get_test_db_name()
3943
# Don't quote the test database name because google.cloud.spanner_v1
4044
# does it.
41-
test_db_params = {'dbname': test_database_name}
45+
test_db_params = {"dbname": test_database_name}
4246
# Create the test database.
4347
try:
4448
self._execute_create_test_db(None, test_db_params, keepdb)
@@ -47,29 +51,37 @@ def _create_test_db(self, verbosity, autoclobber, keepdb=False):
4751
# just return and skip it all.
4852
if keepdb:
4953
return test_database_name
50-
self.log('Got an error creating the test database: %s' % e)
54+
self.log("Got an error creating the test database: %s" % e)
5155
if not autoclobber:
5256
confirm = input(
5357
"Type 'yes' if you would like to try deleting the test "
54-
"database '%s', or 'no' to cancel: " % test_database_name)
55-
if autoclobber or confirm == 'yes':
58+
"database '%s', or 'no' to cancel: " % test_database_name
59+
)
60+
if autoclobber or confirm == "yes":
5661
try:
5762
if verbosity >= 1:
58-
self.log('Destroying old test database for alias %s...' % (
59-
self._get_database_display_str(verbosity, test_database_name),
60-
))
63+
self.log(
64+
"Destroying old test database for alias %s..."
65+
% (
66+
self._get_database_display_str(
67+
verbosity, test_database_name
68+
),
69+
)
70+
)
6171
self._destroy_test_db(test_database_name, verbosity)
6272
self._execute_create_test_db(None, test_db_params, keepdb)
6373
except Exception as e:
64-
self.log('Got an error recreating the test database: %s' % e)
74+
self.log(
75+
"Got an error recreating the test database: %s" % e
76+
)
6577
sys.exit(2)
6678
else:
67-
self.log('Tests cancelled.')
79+
self.log("Tests cancelled.")
6880
sys.exit(1)
6981
return test_database_name
7082

7183
def _execute_create_test_db(self, cursor, parameters, keepdb=False):
72-
self.connection.instance.database(parameters['dbname']).create()
84+
self.connection.instance.database(parameters["dbname"]).create()
7385

7486
def _destroy_test_db(self, test_database_name, verbosity):
7587
self.connection.instance.database(test_database_name).drop()

django_spanner/expressions.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ def order_by(self, compiler, connection, **extra_context):
1212
# DatabaseFeatures.supports_order_by_nulls_modifier = False.
1313
template = None
1414
if self.nulls_last:
15-
template = '%(expression)s IS NULL, %(expression)s %(ordering)s'
15+
template = "%(expression)s IS NULL, %(expression)s %(ordering)s"
1616
elif self.nulls_first:
17-
template = '%(expression)s IS NOT NULL, %(expression)s %(ordering)s'
18-
return self.as_sql(compiler, connection, template=template, **extra_context)
17+
template = "%(expression)s IS NOT NULL, %(expression)s %(ordering)s"
18+
return self.as_sql(
19+
compiler, connection, template=template, **extra_context
20+
)
1921

2022

2123
def register_expressions():

0 commit comments

Comments
 (0)