Skip to content

Commit f0ef825

Browse files
committed
Error handling: Use ValueError exceptions instead of assert
1 parent 62ccb1a commit f0ef825

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Unreleased
1414
server stacktraces into exception messages.
1515
- Refactoring: The module namespace ``crate.client.test_util`` has been
1616
renamed to ``crate.testing.util``.
17+
- Error handling: At two spots in cursor / value converter handling, where
18+
``assert`` statements have been used, ``ValueError`` exceptions are raised
19+
now.
20+
1721

1822
.. _Migrate from crate.client to sqlalchemy-cratedb: https://cratedb.com/docs/sqlalchemy-cratedb/migrate-from-crate-client.html
1923
.. _sqlalchemy-cratedb: https://pypi.org/project/sqlalchemy-cratedb/

src/crate/client/cursor.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,11 @@ def _convert_rows(self):
222222
"""
223223
Iterate rows, apply type converters, and generate converted rows.
224224
"""
225-
assert ( # noqa: S101
226-
"col_types" in self._result and self._result["col_types"]
227-
), "Unable to apply type conversion without `col_types` information"
225+
if not ("col_types" in self._result and self._result["col_types"]):
226+
raise ValueError(
227+
"Unable to apply type conversion "
228+
"without `col_types` information"
229+
)
228230

229231
# Resolve `col_types` definition to converter functions. Running
230232
# the lookup redundantly on each row loop iteration would be a
@@ -302,10 +304,10 @@ def _timezone_from_utc_offset(tz) -> timezone:
302304
"""
303305
UTC offset in string format (e.g. `+0530`) to `datetime.timezone`.
304306
"""
305-
# TODO: Remove use of `assert`. Better use exceptions?
306-
assert ( # noqa: S101
307-
len(tz) == 5
308-
), f"Time zone '{tz}' is given in invalid UTC offset format"
307+
if len(tz) != 5:
308+
raise ValueError(
309+
f"Time zone '{tz}' is given in invalid UTC offset format"
310+
)
309311
try:
310312
hours = int(tz[:3])
311313
minutes = int(tz[0] + tz[3:])

tests/client/test_cursor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_create_with_timezone_as_utc_offset_failure(self):
116116
Verify the cursor trips when trying to use invalid UTC offset strings.
117117
"""
118118
connection = self.get_mocked_connection()
119-
with self.assertRaises(AssertionError) as ex:
119+
with self.assertRaises(ValueError) as ex:
120120
connection.cursor(time_zone="foobar")
121121
self.assertEqual(
122122
str(ex.exception),

0 commit comments

Comments
 (0)