Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(annots): singleton-ness should be inerited #37

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading