From bf8861c3473a1af978db7a06463ddc0bad86f326 Mon Sep 17 00:00:00 2001 From: kserruys Date: Fri, 12 Apr 2024 20:42:29 +0200 Subject: [PATCH] fix: add types to DatasetReference constructor (#1601) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: add types to DatasetReference constructor * fix: add types to DatasetReference constructor * fix: DatasetReference.from_string test coverage --------- Co-authored-by: Karel Serruys Co-authored-by: Chalmer Lowe Co-authored-by: meredithslota Co-authored-by: Tim Sweña (Swast) --- google/cloud/bigquery/dataset.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/google/cloud/bigquery/dataset.py b/google/cloud/bigquery/dataset.py index c313045ce..c49a52faf 100644 --- a/google/cloud/bigquery/dataset.py +++ b/google/cloud/bigquery/dataset.py @@ -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): @@ -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)