diff --git a/google/cloud/bigquery/schema.py b/google/cloud/bigquery/schema.py index 56736dd45..157db7ce6 100644 --- a/google/cloud/bigquery/schema.py +++ b/google/cloud/bigquery/schema.py @@ -15,6 +15,7 @@ """Schemas for BigQuery tables / queries.""" import collections +from typing import Optional from google.cloud.bigquery_v2 import types @@ -106,11 +107,25 @@ def __init__( self._properties["maxLength"] = max_length self._fields = tuple(fields) - if policy_tags is None: - is_record = field_type is not None and field_type.upper() in _STRUCT_TYPES - self._policy_tags = None if is_record else PolicyTagList() - else: - self._policy_tags = policy_tags + self._policy_tags = self._determine_policy_tags(field_type, policy_tags) + + @staticmethod + def _determine_policy_tags( + field_type: str, given_policy_tags: Optional["PolicyTagList"] + ) -> Optional["PolicyTagList"]: + """Return the given policy tags, or their suitable representation if `None`. + + Args: + field_type: The type of the schema field. + given_policy_tags: The policy tags to maybe ajdust. + """ + if given_policy_tags is not None: + return given_policy_tags + + if field_type is not None and field_type.upper() in _STRUCT_TYPES: + return None + + return PolicyTagList() @staticmethod def __get_int(api_repr, name): @@ -138,9 +153,9 @@ def from_api_repr(cls, api_repr: dict) -> "SchemaField": description = api_repr.get("description", _DEFAULT_VALUE) fields = api_repr.get("fields", ()) - policy_tags = PolicyTagList.from_api_repr(api_repr.get("policyTags")) - if policy_tags is None and field_type not in _STRUCT_TYPES: - policy_tags = PolicyTagList() + policy_tags = cls._determine_policy_tags( + field_type, PolicyTagList.from_api_repr(api_repr.get("policyTags")) + ) return cls( field_type=field_type,