Skip to content

Commit 94f4747

Browse files
authored
Accept a string in Table and Dataset constructors. (#7483)
This removes the another need to manually create a TableReference or DatasetReference. Instead, a developer can pass in a string to the constructor and then set the needed properties on the resource.
1 parent 66ef3c8 commit 94f4747

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

bigquery/google/cloud/bigquery/dataset.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,13 @@ class Dataset(object):
306306
https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets
307307
308308
Args:
309-
dataset_ref (google.cloud.bigquery.dataset.DatasetReference):
310-
a pointer to a dataset
309+
dataset_ref (Union[ \
310+
:class:`~google.cloud.bigquery.dataset.DatasetReference`, \
311+
str, \
312+
]):
313+
A pointer to a dataset. If ``dataset_ref`` is a string, it must
314+
include both the project ID and the dataset ID, separated by
315+
``.``.
311316
"""
312317

313318
_PROPERTY_TO_API_FIELD = {
@@ -318,6 +323,8 @@ class Dataset(object):
318323
}
319324

320325
def __init__(self, dataset_ref):
326+
if isinstance(dataset_ref, six.string_types):
327+
dataset_ref = DatasetReference.from_string(dataset_ref)
321328
self._properties = {"datasetReference": dataset_ref.to_api_repr(), "labels": {}}
322329

323330
@property

bigquery/google/cloud/bigquery/table.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,13 @@ class Table(object):
348348
https://cloud.google.com/bigquery/docs/reference/rest/v2/tables
349349
350350
Args:
351-
table_ref (google.cloud.bigquery.table.TableReference):
352-
A pointer to a table
351+
table_ref (Union[ \
352+
:class:`~google.cloud.bigquery.table.TableReference`, \
353+
str, \
354+
]):
355+
A pointer to a table. If ``table_ref`` is a string, it must
356+
included a project ID, dataset ID, and table ID, each separated
357+
by ``.``.
353358
schema (List[google.cloud.bigquery.schema.SchemaField]):
354359
The table's schema
355360
"""
@@ -367,6 +372,8 @@ class Table(object):
367372
}
368373

369374
def __init__(self, table_ref, schema=None):
375+
if isinstance(table_ref, six.string_types):
376+
table_ref = TableReference.from_string(table_ref)
370377
self._properties = {"tableReference": table_ref.to_api_repr(), "labels": {}}
371378
# Let the @property do validation.
372379
if schema is not None:

bigquery/tests/unit/test_dataset.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import unittest
1616

1717
import mock
18+
import pytest
1819

1920

2021
class TestAccessEntry(unittest.TestCase):
@@ -364,6 +365,16 @@ def test_ctor_defaults(self):
364365
self.assertIsNone(dataset.friendly_name)
365366
self.assertIsNone(dataset.location)
366367

368+
def test_ctor_string(self):
369+
dataset = self._make_one("some-project.some_dset")
370+
self.assertEqual(dataset.project, "some-project")
371+
self.assertEqual(dataset.dataset_id, "some_dset")
372+
373+
def test_ctor_string_wo_project_id(self):
374+
with pytest.raises(ValueError):
375+
# Project ID is missing.
376+
self._make_one("some_dset")
377+
367378
def test_ctor_explicit(self):
368379
from google.cloud.bigquery.dataset import DatasetReference, AccessEntry
369380

bigquery/tests/unit/test_table.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,17 @@ def test_ctor_w_schema(self):
504504

505505
self.assertEqual(table.schema, [full_name, age])
506506

507+
def test_ctor_string(self):
508+
table = self._make_one("some-project.some_dset.some_tbl")
509+
self.assertEqual(table.project, "some-project")
510+
self.assertEqual(table.dataset_id, "some_dset")
511+
self.assertEqual(table.table_id, "some_tbl")
512+
513+
def test_ctor_string_wo_project_id(self):
514+
with pytest.raises(ValueError):
515+
# Project ID is missing.
516+
self._make_one("some_dset.some_tbl")
517+
507518
def test_num_bytes_getter(self):
508519
dataset = DatasetReference(self.PROJECT, self.DS_ID)
509520
table_ref = dataset.table(self.TABLE_NAME)

0 commit comments

Comments
 (0)