Skip to content

Commit 0280a94

Browse files
Gurov Ilyatswast
authored andcommitted
feat(bigquery): check rows argument type in insert_rows() (#10174)
* feat(bigquery): check rows arg type in insert_rows() * add class marking * add Iterator into if statement to pass islices * add Iterator into if statement to pass islices * black reformat
1 parent 3c4335d commit 0280a94

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

bigquery/google/cloud/bigquery/client.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ def delete_table(
12201220
raise
12211221

12221222
def _get_query_results(
1223-
self, job_id, retry, project=None, timeout_ms=None, location=None, timeout=None,
1223+
self, job_id, retry, project=None, timeout_ms=None, location=None, timeout=None
12241224
):
12251225
"""Get the query results object for a query job.
12261226
@@ -2355,7 +2355,7 @@ def insert_rows(self, table, rows, selected_fields=None, **kwargs):
23552355
str, \
23562356
]):
23572357
The destination table for the row data, or a reference to it.
2358-
rows (Union[Sequence[Tuple], Sequence[dict]]):
2358+
rows (Union[Sequence[Tuple], Sequence[Dict]]):
23592359
Row data to be inserted. If a list of tuples is given, each
23602360
tuple should contain data for each schema field on the
23612361
current table and in the same order as the schema fields. If
@@ -2376,8 +2376,11 @@ def insert_rows(self, table, rows, selected_fields=None, **kwargs):
23762376
the mappings describing one or more problems with the row.
23772377
23782378
Raises:
2379-
ValueError: if table's schema is not set
2379+
ValueError: if table's schema is not set or `rows` is not a `Sequence`.
23802380
"""
2381+
if not isinstance(rows, (collections_abc.Sequence, collections_abc.Iterator)):
2382+
raise TypeError("rows argument should be a sequence of dicts or tuples")
2383+
23812384
table = _table_arg_to_table(table, default_project=self.project)
23822385

23832386
if not isinstance(table, Table):
@@ -2505,8 +2508,13 @@ def insert_rows_json(
25052508
One mapping per row with insert errors: the "index" key
25062509
identifies the row, and the "errors" key contains a list of
25072510
the mappings describing one or more problems with the row.
2511+
2512+
Raises:
2513+
TypeError: if `json_rows` is not a `Sequence`.
25082514
"""
2509-
if not isinstance(json_rows, collections_abc.Sequence):
2515+
if not isinstance(
2516+
json_rows, (collections_abc.Sequence, collections_abc.Iterator)
2517+
):
25102518
raise TypeError("json_rows argument should be a sequence of dicts")
25112519
# Convert table to just a reference because unlike insert_rows,
25122520
# insert_rows_json doesn't need the table schema. It's not doing any

bigquery/tests/unit/test_client.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5048,6 +5048,7 @@ def _row_data(row):
50485048
)
50495049

50505050
def test_insert_rows_errors(self):
5051+
from google.cloud.bigquery.schema import SchemaField
50515052
from google.cloud.bigquery.table import Table
50525053

50535054
ROWS = [
@@ -5058,6 +5059,7 @@ def test_insert_rows_errors(self):
50585059
]
50595060
creds = _make_credentials()
50605061
http = object()
5062+
50615063
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http)
50625064

50635065
# table ref with no selected fields
@@ -5068,10 +5070,19 @@ def test_insert_rows_errors(self):
50685070
with self.assertRaises(ValueError):
50695071
client.insert_rows(Table(self.TABLE_REF), ROWS)
50705072

5071-
# neither Table nor tableReference
5073+
# neither Table nor TableReference
50725074
with self.assertRaises(TypeError):
50735075
client.insert_rows(1, ROWS)
50745076

5077+
schema = [
5078+
SchemaField("full_name", "STRING", mode="REQUIRED"),
5079+
]
5080+
table = Table(self.TABLE_REF, schema=schema)
5081+
5082+
# rows is just a dict
5083+
with self.assertRaises(TypeError):
5084+
client.insert_rows(table, {"full_name": "value"})
5085+
50755086
def test_insert_rows_w_numeric(self):
50765087
from google.cloud.bigquery import table
50775088
from google.cloud.bigquery.schema import SchemaField
@@ -5853,7 +5864,7 @@ def test_list_rows_error(self):
58535864
http = object()
58545865
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http)
58555866

5856-
# neither Table nor tableReference
5867+
# neither Table nor TableReference
58575868
with self.assertRaises(TypeError):
58585869
client.list_rows(1)
58595870

0 commit comments

Comments
 (0)