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

Better error message while getting an invalid dictionary #432

Open
ZhenyiQ opened this issue Feb 12, 2024 · 0 comments
Open

Better error message while getting an invalid dictionary #432

ZhenyiQ opened this issue Feb 12, 2024 · 0 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

@ZhenyiQ
Copy link

ZhenyiQ commented Feb 12, 2024

Thanks for stopping by to let us know something could be better!

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

Is this a client library issue or a product issue? We will only be able to assist with issues that pertain to the behaviors of this library. If the issue you're experiencing is due to the behavior of the product itself, please visit the Support page to reach the most relevant engineers.

If the support paths suggested above still do not result in a resolution, please provide the following details.

Environment details

  • Programming language: Python
  • OS: Linux
  • Language runtime version: Python 3.11
  • Package version: 1.23.0

Steps to reproduce

  1. minimal python example
from collections.abc import Sequence

from absl import app
import proto

class Composer(proto.Message):
given_name: str = proto.Field(proto.STRING, number=1)
family_name: str = proto.Field(proto.STRING, number=2)

class Song(proto.Message):
composer: Composer = proto.Field(Composer, number=1)


def main(argv: Sequence[str]) -> None:
if len(argv) > 1:
  raise app.UsageError("Too many command-line arguments.")
song = Song({"composer": "john scott"})
print(song)


if __name__ == "__main__":
app.run(main)
  1. run the script with python file_name.py, one gets the following error message
Traceback (most recent call last):
  File "/usr/local/google/home/zhenyiqi/vertexai/python/proto-plus-issues/proto-plus-example.py", line 27, in <module>
    app.run(main)
  File "/usr/local/google/home/zhenyiqi/venv_vertexai/lib/python3.11/site-packages/absl/app.py", line 308, in run
    _run_main(main, args)
  File "/usr/local/google/home/zhenyiqi/venv_vertexai/lib/python3.11/site-packages/absl/app.py", line 254, in _run_main
    sys.exit(main(argv))
             ^^^^^^^^^^
  File "/usr/local/google/home/zhenyiqi/vertexai/python/proto-plus-issues/proto-plus-example.py", line 22, in main
    song = Song({"composer": "john scott"})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/google/home/zhenyiqi/venv_vertexai/lib/python3.11/site-packages/proto/message.py", line 615, in __init__
    super().__setattr__("_pb", self._meta.pb(**params))
                               ^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Message must be initialized with a dict: _default_package.Song

It is true that it's the code itself that's problematic (users should use {"given_name": "john", "family_name": "scott"}) but from the error message, it's really hard to realize what was wrong especially when a proto object has many nested proto objects. It would be nice to be clear about 1) what is the field that's causing this issue (in this case, it's the Composer that's problematic, not the Song) 2) what the user is using to initialize that field ("john scott" is the user input, but we expect a dict here).

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

@ZhenyiQ ZhenyiQ 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 Feb 12, 2024
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

1 participant