diff --git a/datastore/google/cloud/datastore/helpers.py b/datastore/google/cloud/datastore/helpers.py index ee4537317030..91b08452812f 100644 --- a/datastore/google/cloud/datastore/helpers.py +++ b/datastore/google/cloud/datastore/helpers.py @@ -133,7 +133,7 @@ def entity_from_protobuf(pb): # Check if ``value_pb`` was excluded from index. Lists need to be # special-cased and we require all ``exclude_from_indexes`` values # in a list agree. - if is_list: + if is_list and len(value) > 0: exclude_values = set(value_pb.exclude_from_indexes for value_pb in value_pb.array_value.values) if len(exclude_values) != 1: diff --git a/datastore/tests/unit/test_helpers.py b/datastore/tests/unit/test_helpers.py index 18ff98e64781..8529f214640c 100644 --- a/datastore/tests/unit/test_helpers.py +++ b/datastore/tests/unit/test_helpers.py @@ -135,6 +135,27 @@ def test_mismatched_value_indexed(self): with self.assertRaises(ValueError): self._call_fut(entity_pb) + def test_index_mismatch_ignores_empty_list(self): + from google.cloud.proto.datastore.v1 import entity_pb2 + from google.cloud.datastore.helpers import _new_value_pb + + _PROJECT = 'PROJECT' + _KIND = 'KIND' + _ID = 1234 + entity_pb = entity_pb2.Entity() + entity_pb.key.partition_id.project_id = _PROJECT + entity_pb.key.path.add(kind=_KIND, id=_ID) + + array_val_pb = _new_value_pb(entity_pb, 'baz') + array_pb = array_val_pb.array_value.values + + # unindexed_value_pb1 = array_pb.add() + # unindexed_value_pb1.integer_value = 10 + + entity = self._call_fut(entity_pb) + entity_dict = dict(entity) + self.assertIsInstance(entity_dict['baz'], list) + def test_entity_no_key(self): from google.cloud.proto.datastore.v1 import entity_pb2