Skip to content

Commit f87b618

Browse files
committed
only allow enums values
1 parent c7c2b47 commit f87b618

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

google/cloud/bigquery/enums.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ class SourceColumnMatch(str, enum.Enum):
482482
columns to match the field names in the schema."""
483483

484484

485-
class TimestampPrecision(object):
485+
class TimestampPrecision(enum.Enum):
486486
"""Precision (maximum number of total digits in base 10) for seconds of
487487
TIMESTAMP type."""
488488

google/cloud/bigquery/schema.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def __init__(
221221
range_element_type: Union[FieldElementType, str, None] = None,
222222
rounding_mode: Union[enums.RoundingMode, str, None] = None,
223223
foreign_type_definition: Optional[str] = None,
224-
timestamp_precision: Union[enums.TimestampPrecision, int, None] = None,
224+
timestamp_precision: Optional[enums.TimestampPrecision] = None,
225225
):
226226
self._properties: Dict[str, Any] = {
227227
"name": name,
@@ -246,8 +246,13 @@ def __init__(
246246
if isinstance(policy_tags, PolicyTagList)
247247
else None
248248
)
249-
if timestamp_precision is not None:
250-
self._properties["timestampPrecision"] = timestamp_precision
249+
if isinstance(timestamp_precision, enums.TimestampPrecision):
250+
self._properties["timestampPrecision"] = timestamp_precision.value
251+
elif timestamp_precision is not None:
252+
raise ValueError(
253+
"timestamp_precision must be class enums.TimestampPrecision "
254+
f"or None, got {type(timestamp_precision)} instead."
255+
)
251256
if isinstance(range_element_type, str):
252257
self._properties["rangeElementType"] = {"type": range_element_type}
253258
if isinstance(range_element_type, FieldElementType):
@@ -390,7 +395,7 @@ def timestamp_precision(self):
390395
"""Union[enums.TimestampPrecision, int, None]: Precision (maximum number
391396
of total digits in base 10) for seconds of TIMESTAMP type.
392397
"""
393-
return _helpers._int_or_none(self._properties.get("timestampPrecision"))
398+
return enums.TimestampPrecision(self._properties.get("timestampPrecision"))
394399

395400
def to_api_repr(self) -> dict:
396401
"""Return a dictionary representing this schema field.
@@ -426,6 +431,8 @@ def _key(self):
426431
None if self.policy_tags is None else tuple(sorted(self.policy_tags.names))
427432
)
428433

434+
timestamp_precision = self._properties.get("timestampPrecision")
435+
429436
return (
430437
self.name,
431438
field_type,
@@ -435,7 +442,7 @@ def _key(self):
435442
self.description,
436443
self.fields,
437444
policy_tags,
438-
self.timestamp_precision,
445+
timestamp_precision,
439446
)
440447

441448
def to_standard_sql(self) -> standard_sql.StandardSqlField:
@@ -548,9 +555,11 @@ def _to_schema_fields(schema):
548555
if isinstance(schema, Sequence):
549556
# Input is a Sequence (e.g. a list): Process and return a list of SchemaFields
550557
return [
551-
field
552-
if isinstance(field, SchemaField)
553-
else SchemaField.from_api_repr(field)
558+
(
559+
field
560+
if isinstance(field, SchemaField)
561+
else SchemaField.from_api_repr(field)
562+
)
554563
for field in schema
555564
]
556565

tests/unit/test_schema.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ def test_constructor_defaults(self):
5252
self.assertIsNone(field.default_value_expression)
5353
self.assertEqual(field.rounding_mode, None)
5454
self.assertEqual(field.foreign_type_definition, None)
55+
self.assertEqual(
56+
field.timestamp_precision, enums.TimestampPrecision.MICROSECOND
57+
)
5558

5659
def test_constructor_explicit(self):
5760
FIELD_DEFAULT_VALUE_EXPRESSION = "This is the default value for this field"
@@ -89,7 +92,7 @@ def test_constructor_explicit(self):
8992
self.assertEqual(field.rounding_mode, "ROUNDING_MODE_UNSPECIFIED")
9093
self.assertEqual(field.foreign_type_definition, "INTEGER")
9194
self.assertEqual(
92-
field._properties["timestampPrecision"],
95+
field.timestamp_precision,
9396
enums.TimestampPrecision.PICOSECOND,
9497
)
9598

@@ -207,7 +210,7 @@ def test_to_api_repr_w_timestamp_precision(self):
207210
"mode": "NULLABLE",
208211
"name": "foo",
209212
"type": "TIMESTAMP",
210-
"timestampPrecision": enums.TimestampPrecision.PICOSECOND,
213+
"timestampPrecision": 12,
211214
},
212215
)
213216

0 commit comments

Comments
 (0)