FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[grpcio] Fix AioRpcError.trailing_metadata and Metadata iteration by pboulos · Pull Request #15899 · python/typeshed · GitHub

[grpcio] Fix AioRpcError.trailing_metadata and Metadata iteration - #15899

Merged
srittau merged 1 commit into
python:mainfrom
pboulos:pboulos/grpcio-aiorpcerror-trailing-metadata
Jun 13, 2026
Merged

[grpcio] Fix AioRpcError.trailing_metadata and Metadata iteration#15899
srittau merged 1 commit into
python:mainfrom
pboulos:pboulos/grpcio-aiorpcerror-trailing-metadata

Conversation

pboulos commented Jun 12, 2026
edited
Loading

Copy link
Copy Markdown
Contributor

I encountered a typing issue when using grpc.aio and figured I would write a PR to solve it at source.

Two related fixes to the async (grpc.aio) metadata types:

  1. AioRpcError.trailing_metadata() inherited the synchronous
    RpcError.trailing_metadata() -> tuple[_Metadatum, ...], but at runtime returns a
    grpc.aio.Metadata
    (_call.py).
    Added the override.

  2. grpc.aio.Metadata was modelled as a Mapping, but the runtime class is a
    Collection that iterates (key, value) tuples
    (_metadata.py).
    Re-based on Collection[_MetadatumType] with an item-yielding __iter__; keyed
    access (m[key], m.get(...)) is unchanged.

Together these let for key, value in err.trailing_metadata() type-check — it
previously errored with "_Metadatum is not iterable".

import grpc.aio

def reason(err: grpc.aio.AioRpcError) -> None:
    for key, value in err.trailing_metadata():  # before: error "_Metadatum is not iterable"
...
>>> import grpc
>>> from grpc.aio import Metadata, AioRpcError
>>> m = Metadata(("x-request-id", "abc"))
>>> [type(x).__name__ for x in m]            # iterates items, not keys
['tuple']
>>> err = AioRpcError(grpc.StatusCode.NOT_FOUND, Metadata(), m)
>>> type(err.trailing_metadata()).__name__   # not tuple[_Metadatum, ...]
'Metadata'

Please let me know if I've overlooked something in the contribution process.

pboulos force-pushed the pboulos/grpcio-aiorpcerror-trailing-metadata branch from 2ce78ec to 576278d Compare June 12, 2026 01:53

pboulos commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

I originally included a regression test, but I saw the comments made on other PRs around performance considerations of adding tests so I opted to remove it. See it below for reference:

# Regression test for the AioRpcError.trailing_metadata() override: it must
# return the async grpc.aio.Metadata (iterating as (key, value) tuples), not the
# synchronous tuple[_Metadatum, ...] inherited from grpc.RpcError.
def check_aio_rpc_error_trailing_metadata(error: grpc.aio.AioRpcError) -> None:
    assert_type(error.trailing_metadata(), grpc.aio.Metadata)
    for key, value in error.trailing_metadata():
        assert_type(key, str)
        assert_type(value, grpc.aio._MetadataValue)

pboulos force-pushed the pboulos/grpcio-aiorpcerror-trailing-metadata branch from 5b06964 to af81a5e Compare June 12, 2026 01:59

This comment has been minimized.

Comment thread stubs/grpcio/grpc/aio/__init__.pyi Outdated
pboulos force-pushed the pboulos/grpcio-aiorpcerror-trailing-metadata branch 2 times, most recently from 88fdf54 to b965c6c Compare June 13, 2026 16:10
pboulos force-pushed the pboulos/grpcio-aiorpcerror-trailing-metadata branch from b965c6c to 6819c1f Compare June 13, 2026 16:11

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

pboulos requested a review from srittau June 13, 2026 16:20

srittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks! For future reference: Please don't squash commits as it makes it harder to review. We squash on merge anyway.

srittau merged commit 1bfb1fb into python:main Jun 13, 2026
44 checks passed

pboulos commented Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

Apologies, will do 👍. Thanks for merging.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

2 participants


Back | FazBrowse Home | New Git URL