Skip to content

Commit

Permalink
fix(annots): singleton-ness should be inerited
Browse files Browse the repository at this point in the history
  • Loading branch information
kszucs committed Sep 11, 2024
1 parent 37ef82e commit bca61b7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion koerce/annots.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ def __new__(
initable=None,
hashable=None,
immutable=None,
singleton=False,
singleton=None,
allow_coercion=True,
**kwargs,
):
Expand All @@ -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)

Expand Down
13 changes: 13 additions & 0 deletions koerce/tests/test_annots.py
Original file line number Diff line number Diff line change
Expand Up @@ -2209,6 +2209,10 @@ class DataType(Annotable, singleton=True):
nullable: bool = True


class PrimitiveType(DataType):
pass


def test_singleton_basics():
one = OneAndOnly()
only = OneAndOnly()
Expand All @@ -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
Expand Down

0 comments on commit bca61b7

Please sign in to comment.