Skip to content

Commit 0529b18

Browse files
committed
format code using ruff
1 parent 8d6fa78 commit 0529b18

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+263
-428
lines changed

packages/django-google-spanner/django_spanner/__init__.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,32 @@
66

77
import datetime
88
import os
9-
import django
109

1110
# Monkey-patch AutoField to generate a random value since Cloud Spanner can't
1211
# do that.
1312
from uuid import uuid4
1413

14+
import django
15+
1516
RANDOM_ID_GENERATION_ENABLED_SETTING = "RANDOM_ID_GENERATION_ENABLED"
1617

1718
from django.conf.global_settings import DATABASES
1819
from django.db import DEFAULT_DB_ALIAS
19-
from google.cloud.spanner_v1 import JsonObject
2020
from django.db.models.fields import (
2121
NOT_PROVIDED,
2222
AutoField,
2323
Field,
2424
)
2525

26-
from .functions import register_functions
27-
from .lookups import register_lookups
28-
from .utils import check_django_compatability
29-
from .version import __version__
30-
3126
# Monkey-patch google.DatetimeWithNanoseconds's __eq__ compare against
3227
# datetime.datetime.
3328
from google.api_core.datetime_helpers import DatetimeWithNanoseconds
29+
from google.cloud.spanner_v1 import JsonObject
3430

31+
from .functions import register_functions
32+
from .lookups import register_lookups
33+
from .utils import check_django_compatability
34+
from .version import __version__
3535

3636
USING_DJANGO_3 = False
3737
if django.VERSION[:2] == (3, 2):
@@ -41,11 +41,11 @@
4141
if django.VERSION[:2] == (4, 2):
4242
USING_DJANGO_4 = True
4343

44+
from django.db.models import JSONField
4445
from django.db.models.fields import (
45-
SmallAutoField,
4646
BigAutoField,
47+
SmallAutoField,
4748
)
48-
from django.db.models import JSONField
4949

5050
USE_EMULATOR = os.getenv("SPANNER_EMULATOR_HOST") is not None
5151

@@ -109,9 +109,7 @@ def get_prep_value(self, value):
109109

110110
JSONField.get_prep_value = get_prep_value
111111

112-
old_datetimewithnanoseconds_eq = getattr(
113-
DatetimeWithNanoseconds, "__eq__", None
114-
)
112+
old_datetimewithnanoseconds_eq = getattr(DatetimeWithNanoseconds, "__eq__", None)
115113

116114

117115
def datetimewithnanoseconds_eq(self, other):

packages/django-google-spanner/django_spanner/base.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@
66

77
import os
88

9-
from google.cloud import spanner
10-
119
from django.db.backends.base.base import BaseDatabaseWrapper
12-
from google.cloud import spanner_dbapi
10+
from google.cloud import spanner, spanner_dbapi
11+
12+
from django_spanner import USING_DJANGO_3
1313

1414
from .client import DatabaseClient
1515
from .creation import DatabaseCreation
1616
from .features import DatabaseFeatures
1717
from .introspection import DatabaseIntrospection
1818
from .operations import DatabaseOperations
1919
from .schema import DatabaseSchemaEditor
20-
from django_spanner import USING_DJANGO_3
2120

2221

2322
class DatabaseWrapper(BaseDatabaseWrapper):
@@ -86,7 +85,9 @@ class DatabaseWrapper(BaseDatabaseWrapper):
8685
# expression or the result of a bilateral transformation). In those cases,
8786
# special characters for REGEXP_CONTAINS operators (e.g. \, *, _) must be
8887
# escaped on database side.
89-
pattern_esc = r'REPLACE(REPLACE(REPLACE({}, "\\", "\\\\"), "%%", r"\%%"), "_", r"\_")'
88+
pattern_esc = (
89+
r'REPLACE(REPLACE(REPLACE({}, "\\", "\\\\"), "%%", r"\%%"), "_", r"\_")'
90+
)
9091

9192
# These are all no-ops in favor of using REGEXP_CONTAINS in the customized
9293
# lookups.
@@ -123,7 +124,9 @@ def instance(self):
123124
if "client" in self.settings_dict["OPTIONS"]:
124125
client = self.settings_dict["OPTIONS"]["client"]
125126
else:
126-
client = spanner.Client(project=os.environ.get("GOOGLE_CLOUD_PROJECT", "test-project"))
127+
client = spanner.Client(
128+
project=os.environ.get("GOOGLE_CLOUD_PROJECT", "test-project")
129+
)
127130
return client.instance(self.settings_dict["INSTANCE"])
128131

129132
@property
@@ -137,9 +140,7 @@ def allow_transactions_in_auto_commit(self):
137140

138141
@property
139142
def _nodb_connection(self):
140-
raise NotImplementedError(
141-
'Spanner does not have a "no db" connection.'
142-
)
143+
raise NotImplementedError('Spanner does not have a "no db" connection.')
143144

144145
def get_connection_params(self):
145146
"""Retrieve the connection parameters.

packages/django-google-spanner/django_spanner/compiler.py

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77
from django.core.exceptions import EmptyResultSet
88
from django.db.models.sql.compiler import (
99
SQLAggregateCompiler as BaseSQLAggregateCompiler,
10+
)
11+
from django.db.models.sql.compiler import (
1012
SQLCompiler as BaseSQLCompiler,
13+
)
14+
from django.db.models.sql.compiler import (
1115
SQLDeleteCompiler as BaseSQLDeleteCompiler,
16+
)
17+
from django.db.models.sql.compiler import (
1218
SQLInsertCompiler as BaseSQLInsertCompiler,
19+
)
20+
from django.db.models.sql.compiler import (
1321
SQLUpdateCompiler as BaseSQLUpdateCompiler,
1422
)
1523
from django.db.utils import DatabaseError
24+
1625
from django_spanner import USING_DJANGO_3
1726

1827

@@ -49,29 +58,23 @@ def get_combinator_sql(self, combinator, all):
4958
if not query.is_empty()
5059
]
5160
if not features.supports_slicing_ordering_in_compound:
52-
for query, compiler in zip(
53-
self.query.combined_queries, compilers
54-
):
61+
for query, compiler in zip(self.query.combined_queries, compilers):
5562
if query.low_mark or query.high_mark:
5663
raise DatabaseError(
5764
"LIMIT/OFFSET not allowed in subqueries of compound "
5865
"statements."
5966
)
6067
if compiler.get_order_by():
6168
raise DatabaseError(
62-
"ORDER BY not allowed in subqueries of compound "
63-
"statements."
69+
"ORDER BY not allowed in subqueries of compound statements."
6470
)
6571
parts = ()
6672
for compiler in compilers:
6773
try:
6874
# If the columns list is limited, then all combined queries
6975
# must have the same columns list. Set the selects defined on
7076
# the query on all combined queries, if not already set.
71-
if (
72-
not compiler.query.values_select
73-
and self.query.values_select
74-
):
77+
if not compiler.query.values_select and self.query.values_select:
7578
compiler.query.set_values(
7679
(
7780
*self.query.extra_select,
@@ -87,17 +90,13 @@ def get_combinator_sql(self, combinator, all):
8790
part_sql = "SELECT * FROM ({})".format(part_sql)
8891
# Add parentheses when combining with compound query if not
8992
# already added for all compound queries.
90-
elif (
91-
not features.supports_slicing_ordering_in_compound
92-
):
93+
elif not features.supports_slicing_ordering_in_compound:
9394
part_sql = "({})".format(part_sql)
9495
parts += ((part_sql, part_args),)
9596
except EmptyResultSet:
9697
# Omit the empty queryset with UNION and with DIFFERENCE if the
9798
# first queryset is nonempty.
98-
if combinator == "union" or (
99-
combinator == "difference" and parts
100-
):
99+
if combinator == "union" or (combinator == "difference" and parts):
101100
continue
102101
raise
103102
if not parts:
@@ -106,11 +105,7 @@ def get_combinator_sql(self, combinator, all):
106105
# This is the only line that is changed from the Django core
107106
# implementation of this method
108107
combinator_sql += " ALL" if all else " DISTINCT"
109-
braces = (
110-
"({})"
111-
if features.supports_slicing_ordering_in_compound
112-
else "{}"
113-
)
108+
braces = "({})" if features.supports_slicing_ordering_in_compound else "{}"
114109
sql_parts, args_parts = zip(
115110
*((braces.format(sql), args) for sql, args in parts)
116111
)
@@ -126,9 +121,7 @@ def get_combinator_sql(self, combinator, all):
126121
else:
127122
features = self.connection.features
128123
compilers = [
129-
query.get_compiler(
130-
self.using, self.connection, self.elide_empty
131-
)
124+
query.get_compiler(self.using, self.connection, self.elide_empty)
132125
for query in self.query.combined_queries
133126
]
134127
if not features.supports_slicing_ordering_in_compound:
@@ -154,10 +147,7 @@ def get_combinator_sql(self, combinator, all):
154147
# If the columns list is limited, then all combined queries
155148
# must have the same columns list. Set the selects defined on
156149
# the query on all combined queries, if not already set.
157-
if (
158-
not compiler.query.values_select
159-
and self.query.values_select
160-
):
150+
if not compiler.query.values_select and self.query.values_select:
161151
compiler.query = compiler.query.clone()
162152
compiler.query.set_values(
163153
(
@@ -166,9 +156,7 @@ def get_combinator_sql(self, combinator, all):
166156
*self.query.annotation_select,
167157
)
168158
)
169-
part_sql, part_args = compiler.as_sql(
170-
with_col_aliases=True
171-
)
159+
part_sql, part_args = compiler.as_sql(with_col_aliases=True)
172160
if compiler.query.combinator:
173161
# Wrap in a subquery if wrapping in parentheses isn't
174162
# supported.
@@ -190,9 +178,7 @@ def get_combinator_sql(self, combinator, all):
190178
except EmptyResultSet:
191179
# Omit the empty queryset with UNION and with DIFFERENCE if the
192180
# first queryset is nonempty.
193-
if combinator == "union" or (
194-
combinator == "difference" and parts
195-
):
181+
if combinator == "union" or (combinator == "difference" and parts):
196182
continue
197183
raise
198184
if not parts:

packages/django-google-spanner/django_spanner/creation.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ def _create_test_db(self, verbosity, autoclobber, keepdb=False):
8686
self._destroy_test_db(test_database_name, verbosity)
8787
self._execute_create_test_db(None, test_db_params, keepdb)
8888
except Exception as e:
89-
self.log(
90-
"Got an error recreating the test database: %s" % e
91-
)
89+
self.log("Got an error recreating the test database: %s" % e)
9290
sys.exit(2)
9391
else:
9492
self.log("Tests cancelled.")

packages/django-google-spanner/django_spanner/features.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from django.db.backends.base.features import BaseDatabaseFeatures
1010
from django.db.utils import InterfaceError
11+
1112
from django_spanner import USE_EMULATOR, USING_DJANGO_3, USING_DJANGO_4
1213

1314

packages/django-google-spanner/django_spanner/functions.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ def cast(self, compiler, connection, **extra_context):
6161
template = "SUBSTR(" + self.template + ", 0, %s)" % max_length
6262
else:
6363
template = self.template
64-
return self.as_sql(
65-
compiler, connection, template=template, **extra_context
66-
)
64+
return self.as_sql(compiler, connection, template=template, **extra_context)
6765

6866

6967
def chr_(self, compiler, connection, **extra_context):
@@ -92,7 +90,7 @@ def chr_(self, compiler, connection, **extra_context):
9290
compiler,
9391
connection,
9492
template="CODE_POINTS_TO_STRING([%(expressions)s])",
95-
**extra_context
93+
**extra_context,
9694
)
9795

9896

@@ -150,10 +148,7 @@ def cot(self, compiler, connection, **extra_context):
150148
to be replaced with the elements of the `list`.
151149
"""
152150
return self.as_sql(
153-
compiler,
154-
connection,
155-
template="(1 / TAN(%(expressions)s))",
156-
**extra_context
151+
compiler, connection, template="(1 / TAN(%(expressions)s))", **extra_context
157152
)
158153

159154

@@ -183,7 +178,7 @@ def degrees(self, compiler, connection, **extra_context):
183178
compiler,
184179
connection,
185180
template="((%%(expressions)s) * 180 / %s)" % math.pi,
186-
**extra_context
181+
**extra_context,
187182
)
188183

189184

@@ -266,7 +261,7 @@ def ord_(self, compiler, connection, **extra_context):
266261
compiler,
267262
connection,
268263
template="TO_CODE_POINTS(%(expressions)s)[OFFSET(0)]",
269-
**extra_context
264+
**extra_context,
270265
)
271266

272267

@@ -292,9 +287,7 @@ def pi(self, compiler, connection, **extra_context):
292287
:returns: A tuple where `str` is a string containing ordered SQL parameters
293288
to be replaced with the elements of the `list`.
294289
"""
295-
return self.as_sql(
296-
compiler, connection, template=str(math.pi), **extra_context
297-
)
290+
return self.as_sql(compiler, connection, template=str(math.pi), **extra_context)
298291

299292

300293
def radians(self, compiler, connection, **extra_context):
@@ -323,7 +316,7 @@ def radians(self, compiler, connection, **extra_context):
323316
compiler,
324317
connection,
325318
template="((%%(expressions)s) * %s / 180)" % math.pi,
326-
**extra_context
319+
**extra_context,
327320
)
328321

329322

@@ -349,9 +342,7 @@ def strindex(self, compiler, connection, **extra_context):
349342
:returns: A tuple where `str` is a string containing ordered SQL parameters
350343
to be replaced with the elements of the `list`.
351344
"""
352-
return self.as_sql(
353-
compiler, connection, function="STRPOS", **extra_context
354-
)
345+
return self.as_sql(compiler, connection, function="STRPOS", **extra_context)
355346

356347

357348
def substr(self, compiler, connection, **extra_context):
@@ -376,9 +367,7 @@ def substr(self, compiler, connection, **extra_context):
376367
:returns: A tuple where `str` is a string containing ordered SQL parameters
377368
to be replaced with the elements of the `list`.
378369
"""
379-
return self.as_sql(
380-
compiler, connection, function="SUBSTR", **extra_context
381-
)
370+
return self.as_sql(compiler, connection, function="SUBSTR", **extra_context)
382371

383372

384373
def register_functions():

packages/django-google-spanner/django_spanner/introspection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
)
1212
from django.db.models import Index
1313
from google.cloud.spanner_v1 import TypeCode
14+
1415
from django_spanner import USE_EMULATOR
1516

1617

@@ -91,8 +92,7 @@ def get_table_description(self, cursor, table_name):
9192
cursor.description interface.
9293
"""
9394
cursor.execute(
94-
"SELECT * FROM %s LIMIT 1"
95-
% self.connection.ops.quote_name(table_name)
95+
"SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name)
9696
)
9797
column_details = cursor.get_table_column_schema(table_name)
9898
descriptions = []

packages/django-google-spanner/django_spanner/lookups.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ def startswith_endswith(self, compiler, connection):
211211
sql += ", '$'"
212212
sql += ")"
213213
return (
214-
"REGEXP_CONTAINS(%s, %s)"
215-
% (lhs_sql, connection.pattern_esc.format(sql)),
214+
"REGEXP_CONTAINS(%s, %s)" % (lhs_sql, connection.pattern_esc.format(sql)),
216215
params,
217216
)
218217

0 commit comments

Comments
 (0)