Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

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

@blk-josecols

Description

@blk-josecols

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!

Metadata

Metadata

Assignees

Labels

🚨This issue needs some love.priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.status: investigatingThe issue is under investigation, which is determined to be non-trivial.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions