aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXuan Wang <xuanwn@google.com>2024-04-08 11:04:40 -0700
committerCopybara-Service <copybara-worker@google.com>2024-04-08 11:08:16 -0700
commit6cbc7ad63db178a5b8e8520997153911b7df2607 (patch)
tree700b9f0035794a4aa0d753d4f24b25daefd69d0c
parentddb785674ca6c21f9aeee4018b9ef4594ed41cc4 (diff)
downloadgrpc-grpc-6cbc7ad63db178a5b8e8520997153911b7df2607.tar.gz
[Python Aio] Change aio Metadata inheritance (#36214)
Fix: https://github.com/grpc/grpc/issues/26498 <!-- If you know who should review your pull request, please assign it to that person, otherwise the pull request would get assigned randomly. If your pull request is for a specific language, please add the appropriate lang label. --> Closes #36214 COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/36214 from XuanWang-Amos:fix_aio_Metadata_class 90329a2bdf4d99e5c358f5c0ac3414dfd75e18a9 PiperOrigin-RevId: 622898840
-rw-r--r--src/python/grpcio/grpc/aio/_metadata.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/src/python/grpcio/grpc/aio/_metadata.py b/src/python/grpcio/grpc/aio/_metadata.py
index 230cb0df78..6303c17529 100644
--- a/src/python/grpcio/grpc/aio/_metadata.py
+++ b/src/python/grpcio/grpc/aio/_metadata.py
@@ -14,13 +14,13 @@
"""Implementation of the metadata abstraction for gRPC Asyncio Python."""
from collections import OrderedDict
from collections import abc
-from typing import Any, Iterator, List, Tuple, Union
+from typing import Any, Iterator, List, Optional, Tuple, Union
MetadataKey = str
MetadataValue = Union[str, bytes]
-class Metadata(abc.Mapping):
+class Metadata(abc.Collection):
"""Metadata abstraction for the asynchronous calls and interceptors.
The metadata is a mapping from str -> List[str]
@@ -89,6 +89,23 @@ class Metadata(abc.Mapping):
for value in values:
yield (key, value)
+ def keys(self) -> abc.KeysView:
+ return abc.KeysView(self)
+
+ def values(self) -> abc.ValuesView:
+ return abc.ValuesView(self)
+
+ def items(self) -> abc.ItemsView:
+ return abc.ItemsView(self)
+
+ def get(
+ self, key: MetadataKey, default: MetadataValue = None
+ ) -> Optional[MetadataValue]:
+ try:
+ return self[key]
+ except KeyError:
+ return default
+
def get_all(self, key: MetadataKey) -> List[MetadataValue]:
"""For compatibility with other Metadata abstraction objects (like in Java),
this would return all items under the desired <key>.