From bca61b70bc72787c81b191f850d5b000187958fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kriszti=C3=A1n=20Sz=C5=B1cs?= Date: Wed, 11 Sep 2024 18:18:25 +0200 Subject: [PATCH] fix(annots): singleton-ness should be inerited --- koerce/annots.py | 3 ++- koerce/tests/test_annots.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/koerce/annots.py b/koerce/annots.py index c7f907b..c33993e 100644 --- a/koerce/annots.py +++ b/koerce/annots.py @@ -723,7 +723,7 @@ def __new__( initable=None, hashable=None, immutable=None, - singleton=False, + singleton=None, allow_coercion=True, **kwargs, ): @@ -747,6 +747,7 @@ def __new__( is_initable |= spec.initable is_hashable |= spec.hashable is_immutable |= spec.immutable + is_singleton |= spec.singleton signatures.append(spec.signature) attributes.update(spec.attributes) diff --git a/koerce/tests/test_annots.py b/koerce/tests/test_annots.py index 6d4d8ec..4c3d9ba 100644 --- a/koerce/tests/test_annots.py +++ b/koerce/tests/test_annots.py @@ -2209,6 +2209,10 @@ class DataType(Annotable, singleton=True): nullable: bool = True +class PrimitiveType(DataType): + pass + + def test_singleton_basics(): one = OneAndOnly() only = OneAndOnly() @@ -2219,6 +2223,15 @@ def test_singleton_basics(): assert OneAndOnly.__instances__[key] is one +def test_singleton_child_is_singleton(): + prim1 = PrimitiveType() + prim2 = PrimitiveType() + assert prim1 is prim2 + assert len(PrimitiveType.__instances__) == 1 + assert PrimitiveType.__spec__.singleton is True + assert PrimitiveType.__instances__ is DataType.__instances__ + + def test_singleton_lifetime() -> None: one = OneAndOnly() assert len(OneAndOnly.__instances__) == 1