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

NDB: Unknown protobuf attr type <class 'google.cloud.ndb.key.Key'> #284

Closed
blk-josecols opened this issue Jan 9, 2020 · 2 comments · Fixed by #287
Closed

NDB: Unknown protobuf attr type <class 'google.cloud.ndb.key.Key'> #284

blk-josecols opened this issue Jan 9, 2020 · 2 comments · Fixed by #287
Assignees
Labels
priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. 🚨 This issue needs some love. status: investigating The issue is under investigation, which is determined to be non-trivial. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@blk-josecols
Copy link

This seems related to #134 I'm not sure why the fix that was done with #136 is not working for this particular case, is failing for both Datastore and the Datastore Emulator. I created a repository for easier cloning to reproduce the issue -> https://github.com/blk-josecols/google-cloud-ndb-issue

Environment details

  1. OS type and version: macOS Catalina 10.15.1
  2. Python version and virtual environment information: 3.7.5
  3. google-cloud-ndb version: 0.2.1
  4. pip freeze:
cachetools==4.0.0
certifi==2019.11.28
chardet==3.0.4
Click==7.0
google-api-core==1.15.0
google-auth==1.10.0
google-cloud-core==1.1.0
google-cloud-datastore==1.10.0
google-cloud-ndb==0.2.1
googleapis-common-protos==1.6.0
grpcio==1.26.0
idna==2.8
pip-tools==4.3.0
protobuf==3.11.2
pyasn1==0.4.8
pyasn1-modules==0.2.7
python-dotenv==0.10.3
pytz==2019.3
redis==3.3.11
requests==2.22.0
rsa==4.0
six==1.13.0
urllib3==1.25.7

Steps to reproduce

  1. Add a ComputedProperty to a model that returns a KeyProperty for different model.
  2. Try to save the model with this ComputedProperty.

Code example

from google.cloud import ndb


class AModel(ndb.Model):
    s_foo = ndb.StringProperty()


class BModel(ndb.Model):
    s_bar = ndb.StringProperty()
    k_amodel = ndb.KeyProperty(kind="AModel", indexed=True)


class CModel(ndb.Model):
    s_foobar = ndb.StringProperty()
    k_bmodel = ndb.KeyProperty(kind="BModel", indexed=True)
    k_amodel = ndb.ComputedProperty(  # Issue here
        lambda self: self.k_bmodel.get().k_amodel if self.k_bmodel else None,
    )


if __name__ == "__main__":
    client = ndb.Client()

    with client.context():
        k_amodel = AModel(s_foo="test").put()
        k_bmodel = BModel(s_bar="test", k_amodel=k_amodel).put()
        CModel(s_foobar="test", k_bmodel=k_bmodel).put()  # Issue here

Stack trace

Traceback (most recent call last):
  File "main.py", line 14, in <module>
    CModel(s_foobar="test", k_bmodel=k_bmodel).put()
  File "/lib/python3.7/site-packages/google/cloud/ndb/_options.py", line 89, in wrapper
    return wrapped(*pass_args, **kwargs)
  File "/lib/python3.7/site-packages/google/cloud/ndb/utils.py", line 78, in wrapper
    return wrapped(*args, **new_kwargs)
  File "/lib/python3.7/site-packages/google/cloud/ndb/utils.py", line 110, in positional_wrapper
    return wrapped(*args, **kwds)
  File "/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 4935, in _put
    return self._put_async(_options=kwargs["_options"]).result()
  File "/lib/python3.7/site-packages/google/cloud/ndb/tasklets.py", line 190, in result
    self.check_success()
  File "/lib/python3.7/site-packages/google/cloud/ndb/tasklets.py", line 137, in check_success
    raise self._exception
  File "/lib/python3.7/site-packages/google/cloud/ndb/tasklets.py", line 309, in _advance_tasklet
    self.generator.throw(type(error), error, traceback)
  File "/lib/python3.7/site-packages/google/cloud/ndb/model.py", line 4996, in put
    ds_key = yield _datastore_api.put(ds_entity, kwargs["_options"])
  File "/lib/python3.7/site-packages/google/cloud/ndb/tasklets.py", line 312, in _advance_tasklet
    yielded = self.generator.send(send_value)
  File "/lib/python3.7/site-packages/google/cloud/ndb/_datastore_api.py", line 392, in put
    entity_pb = helpers.entity_to_protobuf(entity)
  File "/lib/python3.7/site-packages/google/cloud/datastore/helpers.py", line 221, in entity_to_protobuf
    _set_protobuf_value(value_pb, value)
  File "/lib/python3.7/site-packages/google/cloud/datastore/helpers.py", line 451, in _set_protobuf_value
    attr, val = _pb_attr_value(val)
  File "/lib/python3.7/site-packages/google/cloud/datastore/helpers.py", line 366, in _pb_attr_value
    raise ValueError("Unknown protobuf attr type", type(val))
ValueError: ('Unknown protobuf attr type', <class 'google.cloud.ndb.key.Key'>)

Thanks!

@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Jan 9, 2020
@chrisrossi
Copy link
Contributor

Does this work in GAE NDB?

@chrisrossi chrisrossi self-assigned this Jan 9, 2020
@chrisrossi chrisrossi added 🚨 This issue needs some love. and removed triage me I really want to be triaged. labels Jan 9, 2020
@chrisrossi
Copy link
Contributor

Does this work in GAE NDB?

Answering my own question. Yes, it does. This is a genuine regression.

chrisrossi pushed a commit to chrisrossi/python-ndb that referenced this issue Jan 9, 2020
See comments in code for design considerations. This is a compromise
solution.

Fixes googleapis#284.
chrisrossi pushed a commit that referenced this issue Jan 10, 2020
See comments in code for design considerations. This is a compromise
solution.

Fixes #284.
@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Apr 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. 🚨 This issue needs some love. status: investigating The issue is under investigation, which is determined to be non-trivial. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants