From 87e1b5179320b2afec7b57ddadd1354b2fcc805f Mon Sep 17 00:00:00 2001 From: abhishekacharya200 Date: Sun, 1 Nov 2026 00:07:20 +1100 Subject: [PATCH] fix(bigquery): pass timeout to the table lookup in load_table_from_dataframe and load_table_from_json Both methods call get_table before uploading, without the caller's timeout. BigQuery's default timeout is None, so that request could hang however short a timeout was set. --- .../google/cloud/bigquery/client.py | 4 +- .../tests/unit/test_client.py | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/packages/google-cloud-bigquery/google/cloud/bigquery/client.py b/packages/google-cloud-bigquery/google/cloud/bigquery/client.py index 4967bcdb1134..ab6846d5f0d4 100644 --- a/packages/google-cloud-bigquery/google/cloud/bigquery/client.py +++ b/packages/google-cloud-bigquery/google/cloud/bigquery/client.py @@ -2984,7 +2984,7 @@ def load_table_from_dataframe( and new_job_config.write_disposition != job.WriteDisposition.WRITE_TRUNCATE ): try: - table = self.get_table(destination) + table = self.get_table(destination, timeout=timeout) except core_exceptions.NotFound: pass else: @@ -3172,7 +3172,7 @@ def load_table_from_json( new_job_config.autodetect = True else: try: - self.get_table(destination) + self.get_table(destination, timeout=timeout) except core_exceptions.NotFound: new_job_config.autodetect = True else: diff --git a/packages/google-cloud-bigquery/tests/unit/test_client.py b/packages/google-cloud-bigquery/tests/unit/test_client.py index a6f6b921af07..e47277a08cdd 100644 --- a/packages/google-cloud-bigquery/tests/unit/test_client.py +++ b/packages/google-cloud-bigquery/tests/unit/test_client.py @@ -9797,6 +9797,44 @@ def test_load_table_from_json_basic_use(self): assert sent_config.schema is None assert sent_config.autodetect + def test_load_table_from_dataframe_passes_timeout_to_table_lookup(self): + pandas = pytest.importorskip("pandas") + pytest.importorskip("pyarrow") + + client = self._make_client() + dataframe = pandas.DataFrame({"id": [1]}) + + load_patch = mock.patch( + "google.cloud.bigquery.client.Client.load_table_from_file", autospec=True + ) + get_table_patch = mock.patch( + "google.cloud.bigquery.client.Client.get_table", + autospec=True, + side_effect=google.api_core.exceptions.NotFound("Table not found"), + ) + + with load_patch, get_table_patch as get_table: + client.load_table_from_dataframe(dataframe, self.TABLE_REF, timeout=7.5) + + get_table.assert_called_once_with(client, self.TABLE_REF, timeout=7.5) + + def test_load_table_from_json_passes_timeout_to_table_lookup(self): + client = self._make_client() + + load_patch = mock.patch( + "google.cloud.bigquery.client.Client.load_table_from_file", autospec=True + ) + get_table_patch = mock.patch( + "google.cloud.bigquery.client.Client.get_table", + autospec=True, + side_effect=google.api_core.exceptions.NotFound("Table not found"), + ) + + with load_patch, get_table_patch as get_table: + client.load_table_from_json([{"id": 1}], self.TABLE_REF, timeout=7.5) + + get_table.assert_called_once_with(client, self.TABLE_REF, timeout=7.5) + def test_load_table_from_json_non_default_args(self): from google.cloud.bigquery import job from google.cloud.bigquery.client import _DEFAULT_NUM_RETRIES