Skip to content

Commit

Permalink
fix: add types to DatasetReference constructor (#1601)
Browse files Browse the repository at this point in the history
* fix: add types to DatasetReference constructor

* fix: add types to DatasetReference constructor

* fix: DatasetReference.from_string test coverage

---------

Co-authored-by: Karel Serruys <karel.serruys@foodpairing.com>
Co-authored-by: Chalmer Lowe <chalmerlowe@google.com>
Co-authored-by: meredithslota <meredithslota@google.com>
Co-authored-by: Tim Sweña (Swast) <swast@google.com>
  • Loading branch information
5 people committed Apr 12, 2024
1 parent 82ae908 commit bf8861c
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions google/cloud/bigquery/dataset.py
Expand Up @@ -92,7 +92,7 @@ class DatasetReference(object):
ValueError: If either argument is not of type ``str``.
"""

def __init__(self, project, dataset_id):
def __init__(self, project: str, dataset_id: str):
if not isinstance(project, str):
raise ValueError("Pass a string for project")
if not isinstance(dataset_id, str):
Expand Down Expand Up @@ -166,22 +166,24 @@ def from_string(
standard SQL format.
"""
output_dataset_id = dataset_id
output_project_id = default_project
parts = _helpers._split_id(dataset_id)

if len(parts) == 1 and not default_project:
raise ValueError(
"When default_project is not set, dataset_id must be a "
"fully-qualified dataset ID in standard SQL format, "
'e.g., "project.dataset_id" got {}'.format(dataset_id)
)
if len(parts) == 1:
if default_project is not None:
output_project_id = default_project
else:
raise ValueError(
"When default_project is not set, dataset_id must be a "
"fully-qualified dataset ID in standard SQL format, "
'e.g., "project.dataset_id" got {}'.format(dataset_id)
)
elif len(parts) == 2:
output_project_id, output_dataset_id = parts
elif len(parts) > 2:
else:
raise ValueError(
"Too many parts in dataset_id. Expected a fully-qualified "
"dataset ID in standard SQL format. e.g. "
'"project.dataset_id", got {}'.format(dataset_id)
"dataset ID in standard SQL format, "
'e.g. "project.dataset_id", got {}'.format(dataset_id)
)

return cls(output_project_id, output_dataset_id)
Expand Down

0 comments on commit bf8861c

Please sign in to comment.