Skip to content

Commit 27470a7

Browse files
authored
Add better documentation for support types in datastore Entity. (#3363)
H/T to @gustavorps for bringing this up in #3361. Also snuck in a change in `google.cloud.datastore.helpers` to use `six.binary_type` in place of `(str, bytes)`. (It wasn't a Py3 error before because that check came **after** a `six.text_type` check.)
1 parent 9870057 commit 27470a7

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

datastore/google/cloud/datastore/entity.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class Entity(dict):
2424
An entity storing the actual instance of data.
2525
2626
Each entity is officially represented with a
27-
:class:`google.cloud.datastore.key.Key` class, however it is possible that
28-
you might create an Entity with only a partial Key (that is, a Key
29-
with a Kind, and possibly a parent, but without an ID). In such a
27+
:class:`~google.cloud.datastore.key.Key`, however it is possible that
28+
you might create an entity with only a partial key (that is, a key
29+
with a kind, and possibly a parent, but without an ID). In such a
3030
case, the datastore service will automatically assign an ID to the
3131
partial key.
3232
@@ -66,6 +66,31 @@ class Entity(dict):
6666
>>> entity['age'] = 20
6767
>>> entity['name'] = 'JJ'
6868
69+
However, not all types are allowed as a value for a Google Cloud Datastore
70+
entity. The following basic types are supported by the API:
71+
72+
* :class:`datetime.datetime`
73+
* :class:`~google.cloud.datastore.key.Key`
74+
* :class:`bool`
75+
* :class:`float`
76+
* :class:`int` (as well as :class:`long` in Python 2)
77+
* ``unicode`` (called ``str`` in Python 3)
78+
* ``bytes`` (called ``str`` in Python 2)
79+
* :class:`~google.cloud.datastore.helpers.GeoPoint`
80+
* :data:`None`
81+
82+
In addition, two container types are supported:
83+
84+
* :class:`list`
85+
* :class:`~google.cloud.datastore.entity.Entity`
86+
87+
Each entry in a list must be one of the value types (basic or
88+
container) and each value in an
89+
:class:`~google.cloud.datastore.entity.Entity` must as well. In
90+
this case an :class:`~google.cloud.datastore.entity.Entity` **as a
91+
container** acts as a :class:`dict`, but also has the special annotations
92+
of ``key`` and ``exclude_from_indexes``.
93+
6994
And you can treat an entity like a regular Python dictionary:
7095
7196
.. testsetup:: entity-dict

datastore/google/cloud/datastore/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def _pb_attr_value(val):
311311
name, value = 'integer', val
312312
elif isinstance(val, six.text_type):
313313
name, value = 'string', val
314-
elif isinstance(val, (bytes, str)):
314+
elif isinstance(val, six.binary_type):
315315
name, value = 'blob', val
316316
elif isinstance(val, Entity):
317317
name, value = 'entity', val

0 commit comments

Comments
 (0)