Skip to content

fix(proto-plus): annotate Message.serialize and Message.deserialize - #18553

Closed
NorthIsUp wants to merge 1 commit into
googleapis:mainfrom
NorthIsUp:proto-plus-serialize-types
Closed

NorthIsUp wants to merge 1 commit into
googleapis:mainfrom
NorthIsUp:proto-plus-serialize-types

Conversation

@NorthIsUp

Copy link
Copy Markdown

MessageMeta.serialize() leaves instance unannotated, so pyright in strict mode reports Type of "serialize" is partially unknown at every call site. MessageMeta.deserialize() is annotated -> "Message", so MyMessage.deserialize(b) types as the base Message and callers have to isinstance-narrow it back to MyMessage before touching its fields.

  • serialize(cls, instance: Union["Message", message.Message, Mapping[str, Any]]): the three shapes the docstring's "accepted by the type's constructor" covers.
  • deserialize(cls: Type[_MessageT], payload: bytes) -> _MessageT: the same metaclass self-type pattern typeshed uses for EnumMeta. MyMessage.deserialize(b) now types as MyMessage.

Annotation-only, no runtime change. pytest tests in packages/proto-plus: 274 passed, 2 skipped. Checked with pyright strict against a generated google-ads message: GoogleAdsFailure.deserialize(b"") reveals GoogleAdsFailure, and serialize is fully known.

serialize() left its instance parameter unannotated, so strict type
checkers report the method as partially unknown on every call.
deserialize() returned the base Message, so callers had to narrow
the result back to the class they called it on.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@NorthIsUp
NorthIsUp requested a review from a team as a code owner October 2, 2026 18:48
@google-cla

google-cla Bot commented Oct 2, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the type annotations in packages/proto-plus/proto/message.py. Specifically, it introduces a generic type variable _MessageT bound to Message and applies it to the deserialize method. It also adds type hints to the serialize method's instance parameter, allowing Message, message.Message, or a Mapping[str, Any]. There are no review comments, and I have no feedback to provide.

@NorthIsUp NorthIsUp closed this Oct 2, 2026
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.

1 participant