Skip to content

Commit 2518631

Browse files
committed
Compose dataclass validation through cooperative post-init
1 parent c4e015d commit 2518631

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎src/python_introspect/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from .choices import AnnotationChoices, declared_annotation_choices
4444
from .field_label import FieldLabel, declared_field_label
4545
from .validation import (
46+
DataclassPostInit,
4647
AnnotatedDataclassValidationMixin,
4748
AnnotationValidationError,
4849
overlay_non_none_dataclass,
@@ -121,6 +122,7 @@
121122
"is_enableable",
122123
"mark_enableable",
123124
# Runtime annotation validation
125+
"DataclassPostInit",
124126
"AnnotatedDataclassValidationMixin",
125127
"AnnotationValidationError",
126128
"overlay_non_none_dataclass",

‎src/python_introspect/validation.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,18 @@ class AnnotationValidationError(ValueError):
2929
DataclassT = TypeVar("DataclassT")
3030

3131

32-
class AnnotatedDataclassValidationMixin:
32+
class DataclassPostInit:
33+
"""Terminal owner for cooperative dataclass post-init capabilities.
34+
35+
Every participating mixin inherits this endpoint. C3 puts it after the
36+
participating capabilities, so none needs a missing-method fallback.
37+
"""
38+
39+
def __post_init__(self) -> None:
40+
pass
41+
42+
43+
class AnnotatedDataclassValidationMixin(DataclassPostInit):
3344
"""Validate a dataclass instance from its resolved annotations after construction.
3445
3546
Dataclass declarations inherit this mixin when their annotations are the
@@ -39,6 +50,7 @@ class AnnotatedDataclassValidationMixin:
3950
"""
4051

4152
def __post_init__(self) -> None:
53+
super().__post_init__()
4254
validate_annotated_dataclass(self)
4355

4456

‎tests/test_validation_field_access.py‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
import pytest
55

6-
from python_introspect import AnnotatedDataclassValidationMixin, validate_annotated_dataclass
6+
from python_introspect import (
7+
AnnotatedDataclassValidationMixin, DataclassPostInit, validate_annotated_dataclass,
8+
)
79

810

911
DEFERRED = object()
@@ -46,3 +48,24 @@ def __getattribute__(self, name):
4648
with pytest.raises(RuntimeError) as raised:
4749
FailedRead(3)
4850
assert raised.value is failure
51+
52+
53+
def test_validator_cooperates_with_the_next_post_init_capability():
54+
events = []
55+
56+
class PrepareValue(DataclassPostInit):
57+
def __post_init__(self):
58+
events.append("prepare")
59+
self.value = int(self.value)
60+
super().__post_init__()
61+
62+
@dataclass
63+
class PreparedValue(AnnotatedDataclassValidationMixin, PrepareValue):
64+
value: int
65+
label: str = "ok"
66+
67+
assert PreparedValue("7").value == 7
68+
assert events == ["prepare"]
69+
with pytest.raises(TypeError, match="label must be str"):
70+
PreparedValue("7", label=None)
71+
assert events == ["prepare", "prepare"]

0 commit comments

Comments
 (0)