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

Skip to content

fix: incorrect return type annotation for Message.to_dict #516

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

Merged
merged 2 commits into from
Jan 21, 2025

Conversation

bricker
Copy link
Contributor

@bricker bricker commented Jan 17, 2025

The proto.Message.to_dict function returns a dict[str, Any], but its return type annotation is incorrectly set to Message.

The proto.Message.to_dict function returns the result of calling google.protobuf.json_format.MessageToDict(), which returns dict[str, Any] as specified in the protobuf typeshed stubs.

Additionally, runtime introspection shows that the function returns a dict.

Fixes #489

@bricker bricker requested a review from a team as a code owner January 17, 2025 21:14
@bricker bricker changed the title Fix return type annotation for Message.to_dict fix: incorrect return type annotation for Message.to_dict Jan 17, 2025
@bricker bricker force-pushed the bcr/2501/fix-to_dict-return-type branch from d57772a to 08b9f85 Compare January 17, 2025 21:19
@parthea
Copy link
Contributor

parthea commented Jan 17, 2025

Tests with Python 3.9 pass but Python 3.8 fails. We'll wait to merge this PR until Python 3.8 is dropped which I expect will be this quarter. Thank you for the fix @bricker ! We'll get this merged soon.

partheniou@partheniou-vm-3:~/git/proto-plus-python$ nox -s unit-3.9
nox > Running session unit-3.9(implementation='cpp')
nox > Creating virtual environment (virtualenv) using python3.9 in .nox/unit-3-9-implementation-cpp
nox > python -m pip install coverage pytest pytest-cov pytz
nox > python -m pip install -e '.[testing]' -c /usr/local/google/home/partheniou/git/proto-plus-python/testing/constraints-3.9.txt
nox > python -m pip install 'protobuf<4'
nox > pytest --quiet --cov=proto --cov-config=.coveragerc --cov-report=term --cov-report=html tests
............................................................................................................................................................................................................................ [ 82%]
................................................                                                                                                                                                                             [100%]

---------- coverage: platform linux, python 3.9.16-final-0 -----------
Name                                     Stmts   Miss Branch BrPart  Cover   Missing
------------------------------------------------------------------------------------
proto/__init__.py                           27      0      0      0   100%
proto/_file_info.py                         66      0     28      0   100%
proto/_package_info.py                       9      0      0      0   100%
proto/datetime_helpers.py                   72      0     20      0   100%
proto/enums.py                              68      1     24      1    98%   65
proto/fields.py                             63      0     24      0   100%
proto/marshal/__init__.py                    2      0      0      0   100%
proto/marshal/collections/__init__.py        4      0      0      0   100%
proto/marshal/collections/maps.py           29      0      2      0   100%
proto/marshal/collections/repeated.py       75      1     30      1    98%   112
proto/marshal/marshal.py                   111      0     38      0   100%
proto/marshal/rules/__init__.py              0      0      0      0   100%
proto/marshal/rules/bytes.py                10      0      2      0   100%
proto/marshal/rules/dates.py                36      0     16      0   100%
proto/marshal/rules/enums.py                19      0      6      0   100%
proto/marshal/rules/field_mask.py           10      0      2      0   100%
proto/marshal/rules/message.py              21      0      6      0   100%
proto/marshal/rules/stringy_numbers.py      24      0      2      0   100%
proto/marshal/rules/struct.py               60      0     32      0   100%
proto/marshal/rules/wrappers.py             39      0      6      0   100%
proto/message.py                           267      3     98      3    98%   389, 525, 610
proto/modules.py                             8      0      2      0   100%
proto/primitives.py                         19      0      0      0   100%
proto/utils.py                              18      1      4      0    95%   22
proto/version.py                             1      0      0      0   100%
------------------------------------------------------------------------------------
TOTAL                                     1058      6    342      5    99%
Coverage HTML written to dir htmlcov

268 passed in 3.29s

@parthea
Copy link
Contributor

parthea commented Jan 17, 2025

Blocked by googleapis/gapic-generator-python#2303

@parthea parthea added the status: blocked Resolving the issue is dependent on other work. label Jan 17, 2025
@parthea parthea removed the status: blocked Resolving the issue is dependent on other work. label Jan 17, 2025
The `proto.Message.to_dict` function returns a `dict[str, Any]`, but its
return type annotation is incorrectly set to `Message`.

The `proto.Message.to_dict` function returns the result of calling
[`google.protobuf.json_format.MessageToDict()`](https://github.com/protocolbuffers/protobuf/blob/c58621b6ff2e6145ffb14c7e5a7af7648dfc0535/python/google/protobuf/json_format.py#L125-L162),
which returns `dict[str, Any]` as specified in the [protobuf typeshed
stubs](https://github.com/python/typeshed/blob/main/stubs/protobuf/google/protobuf/json_format.pyi#L23-L30).

Additionally, runtime introspection shows that the function returns a
`dict`.
@bricker bricker force-pushed the bcr/2501/fix-to_dict-return-type branch from 08b9f85 to d8f7f1a Compare January 17, 2025 22:38
@parthea parthea added the owlbot:run Add this label to trigger the Owlbot post processor. label Jan 21, 2025
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Jan 21, 2025
@parthea parthea merged commit 72990f3 into googleapis:main Jan 21, 2025
26 checks passed
@parthea
Copy link
Contributor

parthea commented Jan 21, 2025

Thanks @bricker !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Question about the MessageMeta.to_dict() function return type annotated as Message
2 participants