Thanks to visit codestin.com
Credit goes to github.com

Skip to content

The message constructor is incompatible with message.to_dict and the actual REST API payloads #344

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

Open
Ark-kun opened this issue Dec 5, 2022 · 3 comments
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@Ark-kun
Copy link

Ark-kun commented Dec 5, 2022

The message constructor is incompatible with message.to_dict (when preserving_proto_field_name is used) and the actual REST API payloads. Meanwhile, .from_json is compatible with .to_json.

Environment details

  • Programming language: 3.7
  • OS: Linux
  • Package version: 1.22.1

Steps to reproduce

from google.cloud import batch_v1

This works:

batch_v1.Job.from_json('{"taskGroups": [{}]}')

This does not work:

batch_v1.Job({"taskGroups": [{}]})
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-145-58ddc394e1ed> in <module>
----> 1 batch_v1.Job({"taskGroups": [{}]})

/opt/conda/lib/python3.7/site-packages/proto/message.py in __init__(self, mapping, ignore_unknown_fields, **kwargs)
    564 
    565                 raise ValueError(
--> 566                     "Unknown field for {}: {}".format(self.__class__.__name__, key)
    567                 )
    568 

ValueError: Unknown field for Job: taskGroups
@Ark-kun Ark-kun added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Dec 5, 2022
@atulep
Copy link
Contributor

atulep commented Mar 2, 2023

Compatibility between constructor and message.to_dict is not an intended feature of the library. As you noted, batch_v1.Job({"task_groups": [{}]}) will work. If you'd like this as a feature, please open a feature request instead of a bug.

@atulep atulep closed this as completed Mar 2, 2023
@Ark-kun
Copy link
Author

Ark-kun commented Nov 29, 2023

What is the constructor compatible with?

@Ark-kun
Copy link
Author

Ark-kun commented Nov 30, 2023

@atulep

Compatibility between constructor and message.to_dict is not an intended feature of the library.

The documentation seems to imply this is in fact supported:
https://proto-plus-python.readthedocs.io/en/latest/messages.html#serialization

Similarly, messages can be converted into dictionaries via the to_dict() helper method. There is no from_dict() method because the Message constructor already allows construction from mapping types.

@parthea parthea reopened this Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

3 participants