Skip to content

Commit 5ab974d

Browse files
aakash-saravanan-dbIsaac
andcommitted
refactor: raise the too-many-values error explicitly in _create_rows
_create_rows called fields(*values) only so that Row.__call__ would raise. Build the ValueError in one helper that both Row.__call__ and _create_rows raise, so the message stays identical. Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 70157ed commit 5ab974d

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

‎src/databricks/sql/types.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,7 @@ def __contains__(self, item: Any) -> bool:
253253
def __call__(self, *args: Any) -> "Row":
254254
"""create new Row object"""
255255
if len(args) > len(self):
256-
raise ValueError(
257-
"Can not create Row with fields %s, expected %d values "
258-
"but got %s" % (self, len(self), args)
259-
)
256+
raise _too_many_values_error(self, args)
260257
return _create_row(self, args)
261258

262259
def __getitem__(self, item: Any) -> Any:
@@ -317,22 +314,29 @@ def _create_row(
317314
return row
318315

319316

317+
def _too_many_values_error(fields: "Row", values: Tuple[Any, ...]) -> ValueError:
318+
return ValueError(
319+
"Can not create Row with fields %s, expected %d values "
320+
"but got %s" % (fields, len(fields), values)
321+
)
322+
323+
320324
def _create_rows(fields: "Row", rows: Iterable[Sequence[Any]]) -> List["Row"]:
321325
"""Return ``[fields(*values) for values in rows]``, built without per-row Python calls.
322326
323327
``fields(*values)`` creates the row through ``Row.__call__``, ``_create_row``,
324328
``Row.__new__`` and ``Row.__setattr__``. Result fetches build one row per result row,
325329
so this builds the same object directly: the values as a ``Row`` tuple, with
326330
``__fields__`` stored where ``Row.__setattr__`` stores it. A row with more values than
327-
``fields`` goes through ``fields(*values)`` to raise the same ``ValueError``.
331+
``fields`` raises the same ``ValueError`` as ``fields(*values)``.
328332
"""
329333
width = len(fields)
330334
new_row = tuple.__new__
331335
result: List[Row] = []
332336
append = result.append
333337
for values in rows:
334338
if len(values) > width:
335-
fields(*values)
339+
raise _too_many_values_error(fields, tuple(values))
336340
row = new_row(Row, values)
337341
row.__dict__["__fields__"] = fields
338342
append(row)

‎tests/unit/test_create_rows.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,20 @@ def test_create_rows_pickles_like_row_factory():
7272
assert restored.asDict() == want.asDict()
7373

7474

75-
def test_create_rows_rejects_more_values_than_fields_like_row_factory():
75+
@pytest.mark.parametrize(
76+
"extra_row",
77+
[
78+
(1, "a", None, "extra"),
79+
[1, "a", None, "extra"],
80+
numpy.array([1, "a", None, "extra"], dtype="object"),
81+
],
82+
ids=["tuple", "list", "numpy_object_row"],
83+
)
84+
def test_create_rows_rejects_more_values_than_fields_like_row_factory(extra_row):
7685
with pytest.raises(ValueError) as expected:
77-
FIELDS(1, "a", None, "extra")
86+
FIELDS(*extra_row)
7887
with pytest.raises(ValueError) as actual:
79-
_create_rows(FIELDS, [(1, "a", None), (1, "a", None, "extra")])
88+
_create_rows(FIELDS, [(1, "a", None), extra_row])
8089
assert str(actual.value) == str(expected.value)
8190

8291

0 commit comments

Comments
 (0)