[Aio AioRpcError] Allow pickle AioRpcError (#33891)

Fix: #33643.

This change adds `__reduce__` to `AioRpcError` to allow pickle.

### Testing

Added bazel unit test, without this change, test will fail with error:
```
TypeError: AioRpcError.__init__() missing 3 required positional arguments: 'code', 'initial_metadata', and 'trailing_metadata'
```
revert-33442-printExp
Xuan Wang 2 years ago committed by GitHub
parent 9e5c3652d7
commit 96140caba6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      src/python/grpcio/grpc/aio/_call.py
  2. 23
      src/python/grpcio_tests/tests_aio/unit/aio_rpc_error_test.py

@ -153,6 +153,18 @@ class AioRpcError(grpc.RpcError):
def __str__(self) -> str:
return self._repr()
def __reduce__(self):
return (
type(self),
(
self._code,
self._initial_metadata,
self._trailing_metadata,
self._details,
self._debug_error_string,
),
)
def _create_rpc_error(
initial_metadata: Metadata, status: cygrpc.AioRpcStatus

@ -14,6 +14,7 @@
"""Tests AioRpcError class."""
import logging
import pickle
import unittest
import grpc
@ -52,6 +53,28 @@ class TestAioRpcError(unittest.TestCase):
aio_rpc_error.debug_error_string(), _TEST_DEBUG_ERROR_STRING
)
def test_pickle(self):
aio_rpc_error = AioRpcError(
grpc.StatusCode.CANCELLED,
initial_metadata=_TEST_INITIAL_METADATA,
trailing_metadata=_TEST_TRAILING_METADATA,
details="details",
debug_error_string=_TEST_DEBUG_ERROR_STRING,
)
dump_error = pickle.dumps(aio_rpc_error)
loaded_error = pickle.loads(dump_error)
self.assertEqual(loaded_error.code(), grpc.StatusCode.CANCELLED)
self.assertEqual(loaded_error.details(), "details")
self.assertEqual(
loaded_error.initial_metadata(), _TEST_INITIAL_METADATA
)
self.assertEqual(
loaded_error.trailing_metadata(), _TEST_TRAILING_METADATA
)
self.assertEqual(
loaded_error.debug_error_string(), _TEST_DEBUG_ERROR_STRING
)
if __name__ == "__main__":
logging.basicConfig()

Loading…
Cancel
Save