From 2c470f86d12cc48d0c292cd4a573c7772fd293e7 Mon Sep 17 00:00:00 2001 From: phi Date: Fri, 13 Sep 2024 12:24:23 +0900 Subject: [PATCH] tests: serde test --- pyproject.toml | 6 +++++- src/timeout_executor/serde.py | 3 +-- tests/test_serde.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 tests/test_serde.py diff --git a/pyproject.toml b/pyproject.toml index 7b46dd4..67de46b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,7 +33,11 @@ Repository = "https://github.com/phi-friday/timeout-executor" [project.optional-dependencies] uvloop = ["uvloop; platform_system != 'Windows'"] -test = ["pytest>=8.0.2", "trio>=0.24.0"] +test = [ + "httpx>=0.27.2", + "pytest>=8.0.2", + "trio>=0.24.0", +] [tool.uv] managed = true diff --git a/src/timeout_executor/serde.py b/src/timeout_executor/serde.py index ce1999a..dac591b 100644 --- a/src/timeout_executor/serde.py +++ b/src/timeout_executor/serde.py @@ -1,6 +1,5 @@ from __future__ import annotations -import pickle import sys from collections import deque from collections.abc import Mapping @@ -78,7 +77,7 @@ def serialize_error(error: BaseException) -> SerializedError: reduce_mapping[key] = serialize_error(value) continue - with suppress(pickle.PicklingError): + with suppress(Exception): reduce_mapping[key] = cloudpickle.dumps(value) # TODO: ... __reduce_ex__ args[3:] diff --git a/tests/test_serde.py b/tests/test_serde.py new file mode 100644 index 0000000..ea84da5 --- /dev/null +++ b/tests/test_serde.py @@ -0,0 +1,28 @@ +# pyright: reportUnknownParameterType=false +# pyright: reportMissingParameterType=false +# pyright: reportImplicitOverride=false +from __future__ import annotations + +from typing import Any + +import httpx + +from timeout_executor import serde + + +def test_httpx_reduce_ex(): + def f() -> Any: + try: + httpx.get("http:://invalid/") + except Exception as e: # noqa: BLE001 + return e + return None + + error = f() + assert isinstance(error, httpx.UnsupportedProtocol) + + ser_error = serde.serialize_error(error) + de_error = serde.deserialize_error(ser_error) + + assert isinstance(de_error, httpx.UnsupportedProtocol) + assert hasattr(de_error, "request")