Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add types to DatasetReference constructor #1601

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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:
Comment on lines -180 to +182
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here else: covers the same cases as elif len(parts) > 2:

Parameter dataset_id is a string, because of that list parts will always have at least 1 item.
By using else we can reassure pytest-cov that everything is ok.

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