Google-cloud-python: Automagically convert dicts to datastore entities.

Created on 6 Sep 2017  路  5Comments  路  Source: googleapis/google-cloud-python

I just ran into the following error for the X'th time:

    client.put(entity)
  File "lib/python3.6/site-packages/google/cloud/datastore/client.py", line 384, in put
    self.put_multi(entities=[entity])
  File "lib/python3.6/site-packages/google/cloud/datastore/client.py", line 408, in put_multi
    current.put(entity)
  File "lib/python3.6/site-packages/google/cloud/datastore/batch.py", line 199, in put
    _assign_entity_to_pb(entity_pb, entity)
  File "lib/python3.6/site-packages/google/cloud/datastore/batch.py", line 319, in _assign_entity_to_pb
    bare_entity_pb = helpers.entity_to_protobuf(entity)
  File "lib/python3.6/site-packages/google/cloud/datastore/helpers.py", line 219, in entity_to_protobuf
    _set_protobuf_value(value_pb, value)
  File "lib/python3.6/site-packages/google/cloud/datastore/helpers.py", line 420, in _set_protobuf_value
    _set_protobuf_value(i_pb, item)
  File "lib/python3.6/site-packages/google/cloud/datastore/helpers.py", line 408, in _set_protobuf_value
    attr, val = _pb_attr_value(val)
  File "lib/python3.6/site-packages/google/cloud/datastore/helpers.py", line 325, in _pb_attr_value
    raise ValueError("Unknown protobuf attr type %s" % type(val))
ValueError: Unknown protobuf attr type <class 'dict'>

The cause is pretty obvious, the following code snippet triggers this error:

entity = datastore.Entity(client.key('Resource'))
entity.update(foo={'bar': 'baz'})
client.put(entity)

The code crashes, because dict type properties are not supported. Instead they should be converted to an Entity.

I think it's cumbersome having to convert dict types to Entity types every time. I've written a function that does the conversion recursively for production code, but for simple scripts this is still an issue.

I think it would be nice if this library would just do the conversion from dict to Entity automagically.

feature request datastore

Most helpful comment

Working on it now. Hopefully it'll be quick.

All 5 comments

@remcohaszing It's definitely worth considering! Thanks for filing.

@lukesneeringer @jonparrott WDYT?

I am supportive.

Working on it now. Hopefully it'll be quick.

@dhermes thanks for the patch in https://github.com/GoogleCloudPlatform/google-cloud-python/pull/3927.

I was wondering how long would it take for this change to make it to https://pypi.org/project/google-cloud/. The latest there seems to be from Aug. 5, 2017

@sd-omkar Thanks for noticing, we'll try to get a release out soon.

Also, worth noting that the google-cloud package is less relevant than the google-cloud-datastore package.

Was this page helpful?
0 / 5 - 0 ratings