Skip to content

Commit 53dff2a

Browse files
authored
feat: add TableReference.__str__ to get table ID in standard SQL (#405)
This is the natural inverse of the `TableReference.from_string` method. Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-bigquery/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) Fixes #354 🦕
1 parent 168f035 commit 53dff2a

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

google/cloud/bigquery/table.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ def __ne__(self, other):
262262
def __hash__(self):
263263
return hash(self._key())
264264

265+
def __str__(self):
266+
return f"{self.project}.{self.dataset_id}.{self.table_id}"
267+
265268
def __repr__(self):
266269
from google.cloud.bigquery.dataset import DatasetReference
267270

@@ -475,7 +478,7 @@ def full_table_id(self):
475478
"""Union[str, None]: ID for the table (:data:`None` until set from the
476479
server).
477480
478-
In the format ``project_id:dataset_id.table_id``.
481+
In the format ``project-id:dataset_id.table_id``.
479482
"""
480483
return self._properties.get("id")
481484

@@ -484,7 +487,8 @@ def table_type(self):
484487
"""Union[str, None]: The type of the table (:data:`None` until set from
485488
the server).
486489
487-
Possible values are ``'TABLE'``, ``'VIEW'``, or ``'EXTERNAL'``.
490+
Possible values are ``'TABLE'``, ``'VIEW'``, ``'MATERIALIZED_VIEW'`` or
491+
``'EXTERNAL'``.
488492
"""
489493
return self._properties.get("type")
490494

tests/unit/test_table.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ def test___repr__(self):
272272
)
273273
self.assertEqual(repr(table1), expected)
274274

275+
def test___str__(self):
276+
dataset = DatasetReference("project1", "dataset1")
277+
table1 = self._make_one(dataset, "table1")
278+
self.assertEqual(str(table1), "project1.dataset1.table1")
279+
275280

276281
class TestTable(unittest.TestCase, _SchemaBase):
277282

@@ -813,6 +818,9 @@ def test_from_string(self):
813818
self.assertEqual(got.project, "string-project")
814819
self.assertEqual(got.dataset_id, "string_dataset")
815820
self.assertEqual(got.table_id, "string_table")
821+
self.assertEqual(
822+
str(got.reference), "string-project.string_dataset.string_table"
823+
)
816824

817825
def test_from_string_legacy_string(self):
818826
cls = self._get_target_class()

0 commit comments

Comments
 (0)