Skip to content

Commit

Permalink
test(annots): cover inheritance of AnnotableSpec flags
Browse files Browse the repository at this point in the history
  • Loading branch information
kszucs committed Sep 10, 2024
1 parent be2cc9b commit 1867b40
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
7 changes: 3 additions & 4 deletions koerce/annots.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,6 @@ def __new__(metacls, clsname, bases, dct, **kwargs):
return cls


# TODO(kszucs): cover immutable inheritance additivity with tests
class AnnotableMeta(AbstractMeta):
def __new__(
metacls,
Expand Down Expand Up @@ -701,15 +700,15 @@ def __new__(
# create the base classes for the new class
traits: list[type] = []
if is_immutable and immutable is False:
raise ValueError(
raise TypeError(
"One of the base classes is immutable so the child class cannot be mutable"
)
if is_hashable and hashable is False:
raise ValueError(
raise TypeError(
"One of the base classes is hashable so this child class must be hashable"
)
if is_hashable and not is_immutable:
raise ValueError("Only immutable classes can be hashable")
raise TypeError("Only immutable classes can be hashable")
if hashable:
traits.append(Hashable)
if immutable:
Expand Down
44 changes: 44 additions & 0 deletions koerce/tests/test_annots.py
Original file line number Diff line number Diff line change
Expand Up @@ -2100,6 +2100,50 @@ def test_annotable_spec_flags():
assert Annotable.__spec__.hashable is False


def test_annotable_spec_flag_inheritance():
class A(Annotable):
pass

class B(Annotable, immutable=True):
pass

assert A.__spec__.initable is False
assert A.__spec__.immutable is False
assert A.__spec__.hashable is False
assert B.__spec__.initable is False
assert B.__spec__.immutable is True
assert B.__spec__.hashable is False

class C(A, B):
pass

assert C.__spec__.initable is False
assert C.__spec__.immutable is True
assert C.__spec__.hashable is False

class D(B, A, hashable=True):
pass

assert D.__spec__.initable is False
assert D.__spec__.immutable is True
assert D.__spec__.hashable is True

with pytest.raises(TypeError):

class E(A, B, immutable=False):
pass

with pytest.raises(TypeError):

class F(D, hashable=False):
pass

with pytest.raises(TypeError):

class G(D, immutable=False):
pass


def test_user_model():
class User(Annotable):
id: int
Expand Down

0 comments on commit 1867b40

Please sign in to comment.