Skip to content

Commit 7083f1d

Browse files
authored
docs: Update docstrings for django_spanner (#564)
1 parent 77834f3 commit 7083f1d

13 files changed

+957
-27
lines changed

django_spanner/base.py

+44-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ class DatabaseWrapper(BaseDatabaseWrapper):
103103

104104
@property
105105
def instance(self):
106+
"""Reference to a Cloud Spanner Instance containing the Database.
107+
108+
:rtype: :class:`~google.cloud.spanner_v1.instance.Instance`
109+
:returns: A new instance owned by the existing Spanner Client.
110+
"""
106111
return spanner.Client().instance(self.settings_dict["INSTANCE"])
107112

108113
@property
@@ -112,6 +117,12 @@ def _nodb_connection(self):
112117
)
113118

114119
def get_connection_params(self):
120+
"""Retrieve the connection parameters.
121+
122+
:rtype: dict
123+
:returns: A dictionary containing the Spanner connection parameters
124+
in Django Spanner format.
125+
"""
115126
return {
116127
"project": self.settings_dict["PROJECT"],
117128
"instance_id": self.settings_dict["INSTANCE"],
@@ -120,20 +131,52 @@ def get_connection_params(self):
120131
**self.settings_dict["OPTIONS"],
121132
}
122133

123-
def get_new_connection(self, conn_params):
134+
def get_new_connection(self, **conn_params):
135+
"""Create a new connection with corresponding connection parameters.
136+
137+
:type conn_params: list
138+
:param conn_params: A List of the connection parameters for
139+
:class:`~google.cloud.spanner_dbapi.connection.Connection`
140+
141+
:rtype: :class:`google.cloud.spanner_dbapi.connection.Connection`
142+
:returns: A new Spanner DB API Connection object associated with the
143+
given Google Cloud Spanner resource.
144+
145+
:raises: :class:`ValueError` in case the given instance/database
146+
doesn't exist.
147+
"""
124148
return Database.connect(**conn_params)
125149

126150
def init_connection_state(self):
151+
"""Initialize the state of the existing connection."""
127152
pass
128153

129154
def create_cursor(self, name=None):
155+
"""Create a new Database cursor.
156+
157+
:type name: str
158+
:param name: Currently not used.
159+
160+
:rtype: :class:`~google.cloud.spanner_dbapi.cursor.Cursor`
161+
:returns: The Cursor for this connection.
162+
"""
130163
return self.connection.cursor()
131164

132165
def _set_autocommit(self, autocommit):
166+
"""Set the Spanner transaction autocommit flag.
167+
168+
:type autocommit: bool
169+
:param autocommit: The new value of the autocommit flag.
170+
"""
133171
with self.wrap_database_errors:
134172
self.connection.autocommit = autocommit
135173

136174
def is_usable(self):
175+
"""Check whether the connection is valid.
176+
177+
:rtype: bool
178+
:returns: True if the connection is open, otherwise False.
179+
"""
137180
if self.connection is None:
138181
return False
139182
try:

django_spanner/client.py

+2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99

1010

1111
class DatabaseClient(BaseDatabaseClient):
12+
"""Wrap the Django base class."""
13+
1214
def runshell(self, parameters):
1315
raise NotSupportedError("This method is not supported.")

django_spanner/compiler.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,27 @@
1717

1818

1919
class SQLCompiler(BaseSQLCompiler):
20+
"""
21+
A variation of the Django SQL compiler, adjusted for Spanner-specific
22+
functionality.
23+
"""
24+
2025
def get_combinator_sql(self, combinator, all):
21-
"""
26+
"""Override the native Django method.
27+
2228
Copied from the base class except for:
2329
combinator_sql += ' ALL' if all else ' DISTINCT'
2430
Cloud Spanner requires ALL or DISTINCT.
31+
32+
:type combinator: str
33+
:param combinator: A type of the combinator for the operation.
34+
35+
:type all: bool
36+
:param all: Bool option for the SQL statement.
37+
38+
:rtype: tuple
39+
:returns: A tuple containing SQL statement(s) with some additional
40+
parameters.
2541
"""
2642
features = self.connection.features
2743
compilers = [
@@ -97,16 +113,20 @@ def get_combinator_sql(self, combinator, all):
97113

98114

99115
class SQLInsertCompiler(BaseSQLInsertCompiler, SQLCompiler):
116+
"""A wrapper class for compatibility with Django specifications."""
100117
pass
101118

102119

103120
class SQLDeleteCompiler(BaseSQLDeleteCompiler, SQLCompiler):
121+
"""A wrapper class for compatibility with Django specifications."""
104122
pass
105123

106124

107125
class SQLUpdateCompiler(BaseSQLUpdateCompiler, SQLCompiler):
126+
"""A wrapper class for compatibility with Django specifications."""
108127
pass
109128

110129

111130
class SQLAggregateCompiler(BaseSQLAggregateCompiler, SQLCompiler):
131+
"""A wrapper class for compatibility with Django specifications."""
112132
pass

django_spanner/creation.py

+15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515

1616
class DatabaseCreation(BaseDatabaseCreation):
17+
"""
18+
Spanner-specific wrapper for Django class encapsulating methods for
19+
creation and destruction of the underlying test database.
20+
"""
21+
1722
def mark_skips(self):
1823
"""Skip tests that don't work on Spanner."""
1924
for test_name in self.connection.features.skip_tests:
@@ -30,13 +35,23 @@ def mark_skips(self):
3035
)
3136

3237
def create_test_db(self, *args, **kwargs):
38+
"""Create a test database.
39+
40+
:rtype: str
41+
:returns: The name of the newly created test Database.
42+
"""
3343
# This environment variable is set by the Travis build script or
3444
# by a developer running the tests locally.
3545
if os.environ.get("RUNNING_SPANNER_BACKEND_TESTS") == "1":
3646
self.mark_skips()
3747
super().create_test_db(*args, **kwargs)
3848

3949
def _create_test_db(self, verbosity, autoclobber, keepdb=False):
50+
"""
51+
Create dummy test tables. This method is mostly copied from the
52+
base class but removes usage of `_nodb_connection` since Spanner doesn't
53+
have or need one.
54+
"""
4055
# Mostly copied from the base class but removes usage of
4156
# _nodb_connection since Spanner doesn't have or need one.
4257
test_database_name = self._get_test_db_name()

django_spanner/expressions.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88

99

1010
def order_by(self, compiler, connection, **extra_context):
11-
# In Django 3.1, this can be replaced with
12-
# DatabaseFeatures.supports_order_by_nulls_modifier = False.
11+
"""
12+
Order expressions in the SQL query and generate a new query using
13+
Spanner-specific templates.
14+
15+
:rtype: str
16+
:returns: A SQL query.
17+
"""
18+
# TODO: In Django 3.1, this can be replaced with
19+
# DatabaseFeatures.supports_order_by_nulls_modifier = False.
20+
# Also, consider making this a class method.
1321
template = None
1422
if self.nulls_last:
1523
template = "%(expression)s IS NULL, %(expression)s %(ordering)s"
@@ -21,4 +29,5 @@ def order_by(self, compiler, connection, **extra_context):
2129

2230

2331
def register_expressions():
32+
"""Add Spanner-specific attribute to the Django OrderBy class."""
2433
OrderBy.as_spanner = order_by

django_spanner/features.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212

1313
class DatabaseFeatures(BaseDatabaseFeatures):
14+
"""An extension to Django database feature descriptor."""
1415
can_introspect_big_integer_field = False
1516
can_introspect_duration_field = False
1617
can_introspect_foreign_keys = False

0 commit comments

Comments
 (0)