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

Custom classes inheriting Entity cannot be constructed from entities #396

Closed
ghost opened this issue Dec 1, 2014 · 7 comments
Closed

Custom classes inheriting Entity cannot be constructed from entities #396

ghost opened this issue Dec 1, 2014 · 7 comments
Labels
api: datastore Issues related to the Datastore API. 🚨 This issue needs some love. triage me I really want to be triaged.

Comments

@ghost
Copy link

ghost commented Dec 1, 2014

Currently the following doesn't work:

class Foo(Entity):
    def bar(self):
        """ behavior dependent on internal properties such as _key """
ent = dataset.entity()
baz = Foo(ent)
baz.bar()

This is inconsistent with Entity inheriting dict. Besides style issues, I would think sub-classing Entity would be the preferred way to introduce custom behavior, or enforce an entity schema.

Current workaround is:

class Foo(Entity):
    def __init__(self, ent):
         self._entity = ent
    def bar(self):
         """ behavior dependent on _entity """
@dhermes
Copy link
Contributor

dhermes commented Dec 1, 2014

@ebbixby Why do you need to use dataset.entity?

Just use

ent = Foo(dataset=dataset)

and go on your merry way?

The constructor takes:

    def __init__(self, dataset=None, kind=None, exclude_from_indexes=()):
        super(Entity, self).__init__()

It's not typical that a constructor accepts an entity of its own type, though in some cases makes sense.

I am inclined to close this as "Working as Intended" but maybe I am missing something?

FWIW We are always discussing removing Dataset.entity and friends.

@ghost
Copy link
Author

ghost commented Dec 1, 2014

My mistake. I meant dataset.get_entity(key)

It would be very desirable to get an entity back from a query or fetch by key and immediately cast it to an object that inherits entity. Right?

@dhermes
Copy link
Contributor

dhermes commented Dec 1, 2014

Yes, that does seem more reasonable.

This seems to fall under the umbrella of building an ndb-like ORM built on top of this library.

It's unclear that built-in extensibility should also be at this low-level API. I'm inclined to say it should not and that we are happy to support use-cases like this until ndb-like support is available (or ndb itself).

A change that comes to mind which could make Entity easier to extend. No longer inherit from dict and instead redirect __getitem__ into a data attribute:

class Entity(object):
    def __init__(self, dataset=None, kind=None, exclude_from_indexes=(), data=None):
        self.data = data or {}
    def __getitem__(self, name):
        return self.data[name]

@tseaver WDYT?

@elibixby
Copy link

elibixby commented Dec 3, 2014

I'm ghost. Just consolidating github accounts.

@elibixby
Copy link

elibixby commented Dec 3, 2014

Just wanna throw my support behind Entity no longer subclassing dict. As it is currently implemented, Entity is much more like an object with a dict as a value than a dict itself. Perhaps an ent.properties() would be more appropriate?

@tseaver
Copy link
Contributor

tseaver commented Dec 23, 2014

I don't get the issue behind subclassing dict: if we do that, we will need to provide most of the equivalent behvior (from collections.abc.MutableMapping) in order to allow folks to use it "like a dict". There isn't anything wrong with having extra state or methods on a class that aren't present in the base.

@dhermes
Copy link
Contributor

dhermes commented Dec 30, 2014

Ditto here. Closing since new API (hopefully) reduces friction.

@dhermes dhermes closed this as completed Dec 30, 2014
@dhermes dhermes added the api: datastore Issues related to the Datastore API. label Dec 30, 2014
@jgeewax jgeewax modified the milestone: Datastore Stable Jan 30, 2015
@yoshi-automation yoshi-automation added 🚨 This issue needs some love. triage me I really want to be triaged. labels Apr 7, 2020
parthea added a commit that referenced this issue Jun 4, 2023
parthea pushed a commit that referenced this issue Jun 4, 2023
* chore: Update gapic-generator-python to v1.8.4

PiperOrigin-RevId: 507808936

Source-Link: googleapis/googleapis@64cf849

Source-Link: googleapis/googleapis-gen@53c48ca
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNTNjNDhjYWMxNTNkM2IzN2YzZDJjMmRlYzQ4MzBjZmQ5MWVjNDE1MyJ9

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
parthea pushed a commit that referenced this issue Jul 6, 2023
* chore: add release-please manifest

* linter

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
parthea added a commit that referenced this issue Sep 20, 2023
Source-Link: googleapis/synthtool@5f2a608
Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:8555f0e37e6261408f792bfd6635102d2da5ad73f8f09bcb24f25e6afb5fac97

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Anthonios Partheniou <[email protected]>
parthea pushed a commit that referenced this issue Sep 22, 2023
Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
parthea added a commit that referenced this issue Sep 22, 2023
parthea pushed a commit that referenced this issue Oct 21, 2023
Source-Link: googleapis/synthtool@6fab84a
Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:7cffbc10910c3ab1b852c05114a08d374c195a81cdec1d4a67a1d129331d0bfe
parthea pushed a commit that referenced this issue Oct 21, 2023
Source-Link: https://togithub.com/googleapis/synthtool/commit/dede53ff326079b457cfb1aae5bbdc82cbb51dc3
Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:fac304457974bb530cc5396abd4ab25d26a469cd3bc97cbfb18c8d4324c584eb
parthea pushed a commit that referenced this issue Oct 21, 2023
* feat: add interoperable symmetric encryption system

PiperOrigin-RevId: 544660001

Source-Link: googleapis/googleapis@511319c

Source-Link: googleapis/googleapis-gen@812def9
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODEyZGVmOTU5NGU5ZmEwODc2ZTBlMDExOTUxZGMwYmVjN2EwYTVmZCJ9

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
parthea added a commit that referenced this issue Oct 21, 2023
parthea added a commit that referenced this issue Oct 21, 2023
* chore: Update gapic-generator-python to v1.11.9

PiperOrigin-RevId: 574520922

Source-Link: googleapis/googleapis@5183984

Source-Link: googleapis/googleapis-gen@a59af19
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiYTU5YWYxOWQ0YWM2NTA5ZmFlZGYxY2MzOTAyOTE0MWI2YTViODk2OCJ9

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* update post processor image; remove unused files

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Anthonios Partheniou <[email protected]>
parthea added a commit that referenced this issue Oct 21, 2023
* chore(deps): update all dependencies

* revert protobuf

Co-authored-by: Anthonios Partheniou <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: datastore Issues related to the Datastore API. 🚨 This issue needs some love. triage me I really want to be triaged.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants