Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better documentation for support types in datastore Entity. #3363

Merged
merged 1 commit into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions datastore/google/cloud/datastore/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class Entity(dict):
An entity storing the actual instance of data.

Each entity is officially represented with a
:class:`google.cloud.datastore.key.Key` class, however it is possible that
you might create an Entity with only a partial Key (that is, a Key
with a Kind, and possibly a parent, but without an ID). In such a
:class:`~google.cloud.datastore.key.Key`, however it is possible that
you might create an entity with only a partial key (that is, a key
with a kind, and possibly a parent, but without an ID). In such a

This comment was marked as spam.

case, the datastore service will automatically assign an ID to the
partial key.

Expand Down Expand Up @@ -66,6 +66,31 @@ class Entity(dict):
>>> entity['age'] = 20
>>> entity['name'] = 'JJ'

However, not all types are allowed as a value for a Google Cloud Datastore
entity. The following basic types are supported by the API:

* :class:`datetime.datetime`
* :class:`~google.cloud.datastore.key.Key`
* :class:`bool`
* :class:`float`
* :class:`int` (as well as :class:`long` in Python 2)
* ``unicode`` (called ``str`` in Python 3)
* ``bytes`` (called ``str`` in Python 2)
* :class:`~google.cloud.datastore.helpers.GeoPoint`
* :data:`None`

In addition, two container types are supported:

* :class:`list`
* :class:`~google.cloud.datastore.entity.Entity`

Each entry in a list must be one of the value types (basic or
container) and each value in an
:class:`~google.cloud.datastore.entity.Entity` must as well. In
this case an :class:`~google.cloud.datastore.entity.Entity` **as a
container** acts as a :class:`dict`, but also has the special annotations
of ``key`` and ``exclude_from_indexes``.

And you can treat an entity like a regular Python dictionary:

.. testsetup:: entity-dict
Expand Down
2 changes: 1 addition & 1 deletion datastore/google/cloud/datastore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def _pb_attr_value(val):
name, value = 'integer', val
elif isinstance(val, six.text_type):
name, value = 'string', val
elif isinstance(val, (bytes, str)):
elif isinstance(val, six.binary_type):
name, value = 'blob', val
elif isinstance(val, Entity):
name, value = 'entity', val
Expand Down