Skip to content

Commit f22246a

Browse files
committed
Updates some features of schema handle None more effectively.
1 parent 47474a9 commit f22246a

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

google/cloud/bigquery/schema.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ def _build_schema_resource(fields):
547547
"""
548548
return [field.to_api_repr() for field in fields]
549549

550+
550551
def _to_schema_fields(schema):
551552
"""TODO docstring
552553
QUESTION: do we want a flag to force the generation of a Schema object?
@@ -561,18 +562,24 @@ def _to_schema_fields(schema):
561562
"Schema items must either be fields or compatible "
562563
"mapping representations."
563564
)
564-
565+
565566
if isinstance(schema, Schema):
566-
schema = Schema([
567-
field if isinstance(field, SchemaField) else SchemaField.from_api_repr(field)
568-
for field in schema
569-
], foreign_type_info=schema.foreign_type_info)
567+
schema = Schema(
568+
[
569+
field
570+
if isinstance(field, SchemaField)
571+
else SchemaField.from_api_repr(field)
572+
for field in schema
573+
],
574+
foreign_type_info=schema.foreign_type_info,
575+
)
570576
return schema
571577
return [
572578
field if isinstance(field, SchemaField) else SchemaField.from_api_repr(field)
573579
for field in schema
574580
]
575581

582+
576583
# OLD TO DELETE
577584
# def _to_schema_fields(schema):
578585
# """Coerce `schema` to a list of schema field instances.
@@ -952,9 +959,9 @@ def from_api_repr(cls, resource: dict) -> SerDeInfo:
952959
class Schema:
953960
def __init__(self, fields=None, foreign_type_info=None):
954961
self._properties = {}
955-
self._fields = [] if fields is None else list(fields) #Internal List
962+
self._fields = [] if fields is None else list(fields) # Internal List
956963
self.foreign_type_info = foreign_type_info
957-
964+
958965
@property
959966
def foreign_type_info(self) -> Any:
960967
"""TODO: docstring"""
@@ -975,30 +982,29 @@ def _fields(self, value: list) -> None:
975982
value = _isinstance_or_raise(value, list, none_allowed=True)
976983
self._properties["_fields"] = value
977984

978-
979985
def __len__(self):
980-
return len(self._properties["_fields"])
986+
return len(self._fields)
981987

982988
def __getitem__(self, index):
983-
return self._properties["_fields"][index]
989+
return self._fields[index]
984990

985991
def __setitem__(self, index, value):
986-
self._properties["_fields"][index] = value
987-
992+
self._fields[index] = value
993+
988994
def __delitem__(self, index):
989-
del self._properties["_fields"][index]
995+
del self._fields[index]
990996

991997
def __iter__(self):
992-
return iter(self._properties["_fields"])
993-
998+
return iter(self._fields)
999+
9941000
def __str__(self):
995-
return str(self._properties["_fields"])
996-
1001+
return str(self._fields)
1002+
9971003
def __repr__(self):
9981004
return f"Schema({self.foreign_type_info!r}, {self._properties['_fields']!r})"
999-
1005+
10001006
def append(self, item):
1001-
self._properties["_fields"].append(item)
1007+
self._fields.append(item)
10021008

10031009
def extend(self, iterable):
1004-
self._properties["_fields"].extend(iterable)
1010+
self._fields.extend(iterable)

google/cloud/bigquery/table.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,8 @@ def schema(self):
452452
instance or a compatible mapping representation of the field.
453453
"""
454454
prop = self._properties.get(self._PROPERTY_TO_API_FIELD["schema"])
455-
if not prop: # if empty Schema, empty list, None
456-
if prop is None:
457-
return None
458-
return prop
455+
if not prop: # if empty Schema, empty list, None
456+
return []
459457
elif isinstance(prop, Schema):
460458
return prop
461459
return _parse_schema_resource(prop)

tests/unit/test_table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ def test_ctor(self):
538538
% (self.PROJECT, self.DS_ID, self.TABLE_NAME),
539539
)
540540
valid_options = [Schema(), [], None]
541-
self.assertIn(table.schema, valid_options)
541+
self.assertIn(table.schema, valid_options)
542542

543543
self.assertIsNone(table.created)
544544
self.assertIsNone(table.etag)

0 commit comments

Comments
 (0)