Skip to content

Commit ca73838

Browse files
Ilya Gurovasthamohta
Ilya Gurov
authored andcommitted
fix(db_api): emit warning instead of an exception for rowcount property (googleapis#628)
See for more context: googleapis/python-spanner-sqlalchemy#134
1 parent 6d85990 commit ca73838

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

google/cloud/spanner_dbapi/cursor.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
"""Database cursor for Google Cloud Spanner DB-API."""
1616

17+
import warnings
18+
from collections import namedtuple
19+
1720
import sqlparse
1821

1922
from google.api_core.exceptions import Aborted
@@ -23,8 +26,6 @@
2326
from google.api_core.exceptions import InvalidArgument
2427
from google.api_core.exceptions import OutOfRange
2528

26-
from collections import namedtuple
27-
2829
from google.cloud import spanner_v1 as spanner
2930
from google.cloud.spanner_dbapi.checksum import ResultsChecksum
3031
from google.cloud.spanner_dbapi.exceptions import IntegrityError
@@ -120,10 +121,12 @@ def rowcount(self):
120121
121122
:raises: :class:`NotImplemented`.
122123
"""
123-
raise NotImplementedError(
124+
warnings.warn(
124125
"The `rowcount` property is non-operational. Request "
125126
"resulting rows are streamed by the `fetch*()` methods "
126-
"and can't be counted before they are all streamed."
127+
"and can't be counted before they are all streamed.",
128+
UserWarning,
129+
stacklevel=2,
127130
)
128131

129132
def _raise_if_closed(self):

tests/unit/spanner_dbapi/test_cursor.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,19 @@ def test_property_description(self):
6161
self.assertIsNotNone(cursor.description)
6262
self.assertIsInstance(cursor.description[0], ColumnInfo)
6363

64-
def test_property_rowcount(self):
64+
@mock.patch("warnings.warn")
65+
def test_property_rowcount(self, warn_mock):
6566
connection = self._make_connection(self.INSTANCE, self.DATABASE)
6667
cursor = self._make_one(connection)
67-
with self.assertRaises(NotImplementedError):
68-
cursor.rowcount
68+
69+
cursor.rowcount
70+
warn_mock.assert_called_once_with(
71+
"The `rowcount` property is non-operational. Request "
72+
"resulting rows are streamed by the `fetch*()` methods "
73+
"and can't be counted before they are all streamed.",
74+
UserWarning,
75+
stacklevel=2,
76+
)
6977

7078
def test_callproc(self):
7179
from google.cloud.spanner_dbapi.exceptions import InterfaceError

0 commit comments

Comments
 (0)