GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — DISCUSSION: Entity still not behaving like a dict · Issue #620 · googleapis/google-cloud-python

DISCUSSION: Entity still not behaving like a dict #620

New issue
Copy link
New issue
Copy link
Closed
Closed
DISCUSSION: Entity still not behaving like a dict#620
Copy link
Assignees
dhermes
Labels
api: datastoreIssues related to the Datastore API.
Milestone
Datastore Future

Description

@elibixby
elibixby
opened on Feb 12, 2015
Issue body actions

Since you are (optionally) associating Entities with Keys on instantiation, and since Entity inherits from dict I think it's reasonable to assume that all of the following would work.

ent1 = Entity(key=key1)
ent2 = Entity(key=key2)
assert not dict(ent1) == dict(ent2)

ent = Entity()
assert not 'key' in ent
ent['key'] = Key('Kind', 'id')
datastore.put(ent)

props={'prop1': 'value1', 'prop2':'value2'}
ent = Entity(key=key1, **props)

That said I'm still of the opinion that Entity should not be necessary as a class, using instead datastore.put(key, dict, **options) or, Entity should be completely key agnostic making implementation of the above easier.

Activity

Next
  1. elibixby
    changed the title [-][DISCUSSION] Entity still not behaving like a dict[/-] [+]DISCUSSION: Entity still not behaving like a dict[/+] on Feb 12, 2015
  2. dhermes commented on Feb 12, 2015

    @dhermes
    dhermes
    on Feb 12, 2015
    Contributor
    More actions
    1. dict(ent1) == dict(ent2) is and has been supported.
    2. We can't support
    ent['key'] = Key('Kind', 'id')

    because key is a valid property name. (__key__ is what would do this, but __*__ properties can't be set by users).
    3. I'm a fan of ent = Entity(key=key1, **props), it just isn't pressing given the ease of

    ent = Entity(key=key1)
    ent.update(props)  # or ent.update(**props)
  3. elibixby commented on Feb 12, 2015

    @elibixby
    elibixby
    on Feb 12, 2015
    Author
    More actions

    RE: 2, From https://cloud.google.com/appengine/docs/python/datastore/entities

    Don't name a property "key." This name is reserved for a special property used to store the Model key. Though it may work locally, a property named "key" will prevent deployment to App Engine.

    We should not be encouraging or possibly even allowing it. I think a good way to do that would be overwrite with the required key property.

    adding to this, supporting (2) would implicitly support simply delegating construction to the dict constructor, which would automatically give you (3). Just food for thought.

  4. dhermes commented on Feb 12, 2015

    @dhermes
    dhermes
    on Feb 12, 2015
    Contributor
    More actions

    @elibixby #532 is a discussion about things like that (things we shouldn't encourage users to do).

    Thanks for the quote about App Engine. I was actually trying to find that in the proto spec.

    By swapping for the constructor like a dict, we exchange

    ent = Entity(..., exclude_from_indexes=...)
    ent.update(**props)

    for

    ent = Entity(**props)
    ent.set_exclude_from_indexes(...)

    There is no silver bullet here.

  5. elibixby commented on Feb 12, 2015

    @elibixby
    elibixby
    on Feb 12, 2015
    Author
    More actions

    Except that if we have key specified within props:

    props={'key':datastore.Key('kind',1), 'prop1':'value1'}
    ent = Entity(**props)
    

    is decidedly cleaner and more dict-like than ent = Entity(key=Key('kind',1), **props)

    Also I prefer setting exclude from indexes after properties have been specified because it allows you to check that the property you which to exclude is actually in the entity, e.g.:

    ent = Entity(**props)
    if 'title' in ent:
        ent.exclude_from_indexes('title')
    

    Although I don't really think it's actually a problem on the protobuf side of things (i'd have to double check), it certainly makes me as a backend-naive developer feel safer =).

    Additionally: parsers which take dicts will automatically see the key property if this is the case. I think this is surprisingly convenient.

  6. elibixby commented on Feb 12, 2015

    @elibixby
    elibixby
    on Feb 12, 2015
    Author
    More actions

    I just tested (1), and it failed. Two Entitys with different keys but the same properties test as equal when converted to dicts

  7. tseaver commented on Feb 12, 2015

    @tseaver
    tseaver
    on Feb 12, 2015
    Contributor
    More actions

    @elibixby I'm puzzled: why would you expect that converting to dict didn't change the semantics? That coercion is lossy. By design, the key is not a "property" (an entry in the Entity's items), but a special attribute on the side.

  8. elibixby commented on Feb 12, 2015

    @elibixby
    elibixby
    on Feb 12, 2015
    Author
    More actions

    Right, this is the design I'm questioning. I expected it to fail, I'm arguing that more intuitive usage would be for it to succeed.

  9. elibixby commented on Feb 12, 2015

    @elibixby
    elibixby
    on Feb 12, 2015
    Author
    More actions

    To expand on this:

    If Key is not included in the dict backing entity, then entity should not BE a dict, but HAVE a dict. When extending a base data structure I tend to think only of adding behavior/restrictions, not storing additional data outside that data structure.

    However, since having Entity be a subclass of dict has already been discussed to death, and we are sticking with it, I think that Key SHOULD be a part of the entity backing dict, to be in accordance with the design principles in the first paragraph.

  10. dhermes
    modified the milestone: Datastore Future on Feb 12, 2015
  11. jgeewax commented on Feb 13, 2015

    @jgeewax
    jgeewax
    on Feb 13, 2015
    Contributor
    More actions

    This is a tricky one... Just wanted to add a couple thoughts:

    Let's assume the following...

    entity = Entity(key=same_key)
    same_entity = Entity(key=same_key)
    other_entity = Entity(key=other_key)

    Some equality checks (== means -- IMO -- "same data, same key")

    # Object == means "same data, same key"
    >>> entity == other_entity
    False
    >>> entity == same_entity
    True
    
    # dict() == means "same data, key doesn't matter"
    >>> dict(entity) == dict(other_entity)  # {} == {}
    True
    >>> dict(entity) == dict(same_entity)  # {} == {}
    
    # .key == means "same key, data doesn't matter"
    >>> entity.key == other_entity.key
    False
    >>> entity.key == same_entity.key
    True

    Make same_entity have different data:

    >>> same_entity['name'] = 'Danny'

    Some more equality tests:

    >>> entity == same_entity
    False
    >>> entity.key == same_entity.key
    True
    >>> dict(entity) == dict(same_entity)  # {} == {'name': 'Danny'}
    False

    As for the key stuff, I personally think 'key' should be treated as any other property:

    >>> entity1['key'] = 'Whatever string I feel like...'
    >>> entity1.key == entity1['key']
    False

    App Engine says not to do this, but (if I recall correctly) that's due to both ext.db and ext.ndb using an ORM style way of specifying properties (where property names are not accessed by using their string name, but instead are Python instance properties on the object).

    This makes one weird case:

    >>> properties = {'key': 'My key property'}
    >>> entity = Entity(key=Key(...), **properties)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: type object got multiple values for keyword argument 'key'

    The easy fix is...

    >>> properties = {'key': 'My key property'}
    >>> entity = Entity(key=Key(...))
    >>> entity.update(properties)

    Another option is to make it so that Entity doesn't take keyword arguments, but instead takes a dictionary of data to start, meaning the following would work exactly as expected:

    >>> entity = Entity(key=Key(...), {'key': 'My key property'})

    It breaks the dict constructor keyword argument assignment, but I'm not horrified by that, as you can still do .update(key=value, key2=value) and get the result you want...


    Regarding Entity not needing to exist as a class -- I can't say I'm against that idea... I'd need to do a lot more thinking, but it's a great point... Maybe @dhermes has some input here.

    /cc @pcostell on ... all of this...

  12. jgeewax commented on Feb 13, 2015

    @jgeewax
    jgeewax
    on Feb 13, 2015
    Contributor
    More actions

    I also forgot, if you want the prop=value style syntax on the constructor, you could do:

    >>> entity = Entity(key=Key(...), dict(key=value, key2=value, name='Danny'))
  13. elibixby commented on Feb 13, 2015

    @elibixby
    elibixby
    on Feb 13, 2015
    Author
    More actions

    Yeah, I understand the equality testing behavior you want, and it is coherent and makes sense, but IMO that behavior shouldn't be implemented by subclassing dict. It seems like a nice syntactic shortcut, but I think it is conceptually misleading, because you are really just using the backing dict as a stored element.

  14. dhermes commented on Feb 13, 2015

    @dhermes
    dhermes
    on Feb 13, 2015
    Contributor
    More actions

    @jgeewax I updated the post for you, we ditched the ent.key() style and now it's just a property.


    RE: db...who knows. RE: ndb, if a property has the name key, the object key is still available as _key. The constructor takes __init__(*args, **kwargs) so people can even use self as a property name if they like.


    As for dropping Entity, I am for it in theory but keep getting hung up on the indexes. At the end of the day, it just isn't worth trying to get rid of it. This is something more fit for an ORM.


    We've heard no other feedback about issues or confusions with subclassing dict. It seems there have been a fair amount of people trying the library out (JJ's original design subclassed dict):

    $ vanity gcloud
      gcloud-0.01.tar.gz    2014-01-28         1116
      gcloud-0.02.tar.gz    2014-01-28         1106
    gcloud-0.02.1.tar.gz    2014-01-28         1130
    gcloud-0.02.2.tar.gz    2014-02-20         2349
     gcloud-0.3.0.tar.gz    2014-11-26         1316
    -----------------------------------------------
    gcloud has been downloaded 7017 times!
    

    The __getitem__ syntax (i.e. ent['propName']) and thegetattrsyntax (i.e.ent.actual_prop`) are distinct and don't seem to cause confusion.

  15. 11 remaining items

  16. dhermes
    closed this as completedon Jun 18, 2015
  17. dhermes
    added
    api: datastoreIssues related to the Datastore API.
    on Dec 31, 2015
  18. JustinBeckwith
    assigned
    dhermes
    on Feb 1, 2021
  19. vchudnov-g
    added a commit that references this issue on Sep 20, 2023

    build(deps): bump cryptography from 38.0.3 to 39.0.1 in /synthtool/gc…

    036e586
  20. parthea
    added a commit that references this issue on Aug 21, 2025

    build(deps): [autoapprove] bump cryptography from 41.0.0 to 41.0.2 (#620

    2b73675
  21. parthea
    added 2 commits that reference this issue on Sep 16, 2025

    build(deps): [autoapprove] bump cryptography from 41.0.0 to 41.0.2 (#620

    79a115f

    docs: updates with a link to the canonical source of documentation (#620

    63a082a
  22. parthea
    added 6 commits that reference this issue on Nov 24, 2025

    chore: release 0.33.7 (#620)

    d1c74fe

    chore(python): fix prerelease session [autoapprove] (#620)

    5e51e2e

    chore(tests): update python version used for environment tests (#620)

    5766fd9

    fix: retry connection errors with memcache (#645)

    0776b14

    chore(tests): update python version used for environment tests (#620)

    9e64418

    chore: release 1.22.1 (#620)

    75eb474
  23. parthea
    added 2 commits that reference this issue on Mar 6, 2026

    feat: add decimal/numeric support (#620)

    e244a37

    chore(deps): update dependency grpcio to v1.71.0 (#620)

    f0c8560
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @dhermes
    dhermes

Labels

api: datastoreIssues related to the Datastore API.

Type

No type

Projects

No projects

    Milestone

    • Datastore Future

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.