From 0aabe86c806c618f1510b13c1c2524c70e0bf095 Mon Sep 17 00:00:00 2001 From: Abhinay Kumar <111532209+abhinyaay@users.noreply.github.com> Date: Tue, 15 Sep 2026 12:22:48 +0530 Subject: [PATCH 1/2] fix: preserve Omitted singleton across copy, deepcopy and pickle SingletonMeta enforces the singleton via __call__, but copy, deepcopy and pickle recreate the instance through __new__, bypassing the metaclass. This broke equality for any object with an Omitted-valued field after a copy or pickle round-trip. Add __copy__, __deepcopy__ and __reduce__ so Omitted always resolves back to the singleton. --- src/unihttp/omitted.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/unihttp/omitted.py b/src/unihttp/omitted.py index 1d4c748..0af8e7e 100644 --- a/src/unihttp/omitted.py +++ b/src/unihttp/omitted.py @@ -1,4 +1,4 @@ -from typing import Any, ClassVar, TypeAlias, TypeVar +from typing import Any, ClassVar, Self, TypeAlias, TypeVar class SingletonMeta(type): @@ -17,6 +17,15 @@ def __repr__(self) -> str: def __bool__(self) -> bool: return False + def __copy__(self) -> Self: + return self + + def __deepcopy__(self, memo: dict[int, Any]) -> Self: + return self + + def __reduce__(self) -> tuple[type[Self], tuple[()]]: + return (type(self), ()) + T = TypeVar("T") Omittable: TypeAlias = T | Omitted # noqa: UP040 From fa592588e3f75a9b3f2df5d408c31e93fade3125 Mon Sep 17 00:00:00 2001 From: Abhinay Kumar <111532209+abhinyaay@users.noreply.github.com> Date: Tue, 15 Sep 2026 12:25:21 +0530 Subject: [PATCH 2/2] test: cover Omitted identity across copy, deepcopy and pickle Regression tests for the singleton fix: copy/deepcopy/pickle identity and equality, dataclass equality round-trips, and dataclasses.asdict(). --- tests/test_features/test_omitted_identity.py | 63 ++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/test_features/test_omitted_identity.py diff --git a/tests/test_features/test_omitted_identity.py b/tests/test_features/test_omitted_identity.py new file mode 100644 index 0000000..a3fca5d --- /dev/null +++ b/tests/test_features/test_omitted_identity.py @@ -0,0 +1,63 @@ +import copy +import dataclasses +import pickle +from dataclasses import dataclass + +from unihttp.omitted import Omittable, Omitted + + +def test_singleton_identity(): + assert Omitted() is Omitted() + + +def test_copy_preserves_singleton(): + o = Omitted() + assert copy.copy(o) is o + assert copy.copy(o) == o + + +def test_deepcopy_preserves_singleton(): + o = Omitted() + assert copy.deepcopy(o) is o + assert copy.deepcopy(o) == o + + +def test_pickle_preserves_singleton(): + o = Omitted() + restored = pickle.loads(pickle.dumps(o)) + assert restored is o + assert restored == o + + +def test_deepcopy_memo_argument(): + o = Omitted() + memo: dict[int, object] = {} + assert copy.deepcopy(o, memo) is o + + +@dataclass +class _Model: + name: str + optional: Omittable[str] = Omitted() + + +def test_dataclass_equality_survives_deepcopy(): + a = _Model(name="a") + b = copy.deepcopy(a) + assert a == b + assert a.optional == b.optional + + +def test_dataclass_equality_survives_pickle(): + a = _Model(name="a") + b = pickle.loads(pickle.dumps(a)) + assert a == b + assert a.optional == b.optional + + +def test_asdict_deepcopy_keeps_singleton(): + # dataclasses.asdict() deep-copies field values; the Omitted sentinel + # must remain the singleton so downstream equality checks still hold. + a = _Model(name="a") + dumped = dataclasses.asdict(a) + assert dumped["optional"] is Omitted()