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