Skip to content

Commit 4f117a7

Browse files
committed
updates from_api_repr and a number of tests and cleans up miscellaneous cruft'
1 parent 43dc45e commit 4f117a7

File tree

7 files changed

+151
-121
lines changed

7 files changed

+151
-121
lines changed

google/cloud/bigquery/_helpers.py

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,35 +1007,11 @@ def _verify_job_config_type(job_config, expected_type, param_name="job_config"):
10071007
)
10081008

10091009

1010-
class ResourceBase:
1011-
"""Base class providing the from_api_repr method."""
1012-
1013-
def __init__(self):
1014-
self._properties = {}
1015-
1016-
@classmethod
1017-
def from_api_repr(cls, resource: dict):
1018-
"""Factory: constructs an instance of the class (cls)
1019-
given its API representation.
1020-
1021-
Args:
1022-
resource (Dict[str, Any]):
1023-
API representation of the object to be instantiated.
1024-
1025-
Returns:
1026-
ResourceBase: An instance of the class initialized with data
1027-
from 'resource'.
1028-
"""
1029-
config = cls()
1030-
config._properties = copy.deepcopy(resource)
1031-
return config
1032-
1033-
10341010
def _isinstance_or_raise(
1035-
value: Any,
1036-
dtype: Union[Type, Tuple[Type, ...]],
1037-
none_allowed: Optional[bool]=False,
1038-
) -> Any:
1011+
value: Any,
1012+
dtype: Union[Type, Tuple[Type, ...], None],
1013+
none_allowed: Optional[bool] = False,
1014+
) -> Any:
10391015
"""Determine whether a value type matches a given datatype or None.
10401016
10411017
Args:
@@ -1055,10 +1031,26 @@ def _isinstance_or_raise(
10551031

10561032
if isinstance(value, dtype):
10571033
return value
1058-
1059-
or_none = ''
1034+
1035+
or_none = ""
10601036
if none_allowed:
1061-
or_none = ' (or None)'
1037+
or_none = " (or None)"
10621038

10631039
msg = f"Pass {value} as a '{dtype}'{or_none}. Got {type(value)}."
10641040
raise TypeError(msg)
1041+
1042+
1043+
def _from_api_repr(obj, resource: dict):
1044+
"""Factory: constructs an instance of the class (cls)
1045+
given its API representation.
1046+
1047+
Args:
1048+
resource (Dict[str, Any]):
1049+
API representation of the object to be instantiated.
1050+
1051+
Returns:
1052+
An instance of the class initialized with data from 'resource'.
1053+
"""
1054+
config = obj
1055+
config._properties = copy.deepcopy(resource)
1056+
return config

google/cloud/bigquery/external_config.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
_int_or_none,
3131
_str_or_none,
3232
_isinstance_or_raise,
33-
ResourceBase,
33+
_from_api_repr,
3434
)
3535
from google.cloud.bigquery.format_options import AvroOptions, ParquetOptions
3636
from google.cloud.bigquery.schema import SchemaField
@@ -1009,7 +1009,7 @@ def from_api_repr(cls, resource: dict) -> "ExternalConfig":
10091009
return config
10101010

10111011

1012-
class ExternalCatalogDatasetOptions(ResourceBase):
1012+
class ExternalCatalogDatasetOptions:
10131013
"""Options defining open source compatible datasets living in the BigQuery catalog.
10141014
Contains metadata of open source database, schema or namespace represented
10151015
by the current dataset.
@@ -1041,7 +1041,7 @@ def default_storage_location_uri(self) -> Any:
10411041

10421042
@default_storage_location_uri.setter
10431043
def default_storage_location_uri(self, value: str):
1044-
value = _isinstance_or_raise(value, (str, None))
1044+
value = _isinstance_or_raise(value, str, none_allowed=True)
10451045
self._properties["defaultStorageLocationUri"] = value
10461046

10471047
@property
@@ -1053,7 +1053,7 @@ def parameters(self) -> Any:
10531053

10541054
@parameters.setter
10551055
def parameters(self, value: dict[str, Any]):
1056-
value = _isinstance_or_raise(value, (dict, None))
1056+
value = _isinstance_or_raise(value, dict, none_allowed=True)
10571057
self._properties["parameters"] = value
10581058

10591059
def to_api_repr(self) -> dict:
@@ -1066,8 +1066,11 @@ def to_api_repr(self) -> dict:
10661066
config = copy.deepcopy(self._properties)
10671067
return config
10681068

1069+
def from_api_repr(self, resource):
1070+
return _from_api_repr(self, resource)
10691071

1070-
class ExternalCatalogTableOptions(ResourceBase):
1072+
1073+
class ExternalCatalogTableOptions:
10711074
"""Metadata about open source compatible table. The fields contained in these
10721075
options correspond to hive metastore's table level properties.
10731076
@@ -1109,7 +1112,7 @@ def connection_id(self):
11091112

11101113
@connection_id.setter
11111114
def connection_id(self, value: Optional[str]):
1112-
value = _isinstance_or_raise(value, (str, None))
1115+
value = _isinstance_or_raise(value, str, none_allowed=True)
11131116
self._properties["connectionId"] = value
11141117

11151118
@property
@@ -1123,7 +1126,7 @@ def parameters(self) -> Any:
11231126

11241127
@parameters.setter
11251128
def parameters(self, value: Union[Dict[str, Any], None]):
1126-
value = _isinstance_or_raise(value, (dict, None))
1129+
value = _isinstance_or_raise(value, dict, none_allowed=True)
11271130
self._properties["parameters"] = value
11281131

11291132
@property
@@ -1135,9 +1138,8 @@ def storage_descriptor(self) -> Any:
11351138

11361139
@storage_descriptor.setter
11371140
def storage_descriptor(self, value: Optional[str]):
1138-
value = _isinstance_or_raise(value, (str, None))
1141+
value = _isinstance_or_raise(value, str, none_allowed=True)
11391142
self._properties["storageDescriptor"] = value
1140-
11411143

11421144
def to_api_repr(self) -> dict:
11431145
"""Build an API representation of this object.
@@ -1148,3 +1150,6 @@ def to_api_repr(self) -> dict:
11481150
"""
11491151
config = copy.deepcopy(self._properties)
11501152
return config
1153+
1154+
def from_api_repr(self, resource):
1155+
return _from_api_repr(self, resource)

google/cloud/bigquery/query.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,6 @@ def __init__(
10031003
):
10041004
self.name = name
10051005
self.range_element_type = self._parse_range_element_type(range_element_type)
1006-
print(self.range_element_type.type_._type)
10071006
self.start = start
10081007
self.end = end
10091008

0 commit comments

Comments
 (0)