Skip to content

Commit c4e015d

Browse files
committed
Validate declared field values through their owning access semantics
1 parent ffcffaa commit c4e015d

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

‎src/python_introspect/validation.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ def overlay_non_none_dataclass(
7777

7878

7979
def validate_annotated_dataclass(instance: object) -> None:
80-
"""Validate every field from the instance's resolved dataclass annotations."""
80+
"""Validate declared field values through their owning access semantics.
81+
82+
A field descriptor may supply an effective value (for example inherited
83+
configuration data). Bypassing it validates private storage instead of the
84+
declared value. Literal null remains subject to the annotation.
85+
"""
8186

8287
owner_type = type(instance)
8388
if not is_dataclass(instance) or isinstance(instance, type):
@@ -88,8 +93,8 @@ def validate_annotated_dataclass(instance: object) -> None:
8893

8994
annotations = resolved_class_annotations(owner_type)
9095
for declared_field in fields(instance):
91-
annotation = annotations.get(declared_field.name, declared_field.type)
92-
value = object.__getattribute__(instance, declared_field.name)
96+
annotation = annotations[declared_field.name]
97+
value = getattr(instance, declared_field.name)
9398
validate_annotation_value(
9499
annotation,
95100
value,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""Annotation validation consumes the field owner's public value semantics."""
2+
from dataclasses import dataclass
3+
4+
import pytest
5+
6+
from python_introspect import AnnotatedDataclassValidationMixin, validate_annotated_dataclass
7+
8+
9+
DEFERRED = object()
10+
11+
12+
@dataclass
13+
class EffectiveValue(AnnotatedDataclassValidationMixin):
14+
value: int
15+
16+
def __getattribute__(self, name):
17+
value = object.__getattribute__(self, name)
18+
return 7 if name == "value" and value is DEFERRED else value
19+
20+
21+
def test_validation_reads_effective_values_without_rewriting_storage():
22+
instance = EffectiveValue(DEFERRED)
23+
assert instance.value == 7
24+
assert object.__getattribute__(instance, "value") is DEFERRED
25+
validate_annotated_dataclass(instance)
26+
27+
28+
@pytest.mark.parametrize("value", [None, "invalid", False])
29+
def test_literal_null_and_invalid_concrete_values_are_not_masked(value):
30+
with pytest.raises(TypeError, match="must be int"):
31+
EffectiveValue(value)
32+
33+
34+
def test_field_descriptor_failure_propagates_its_original_identity():
35+
failure = RuntimeError("field accessor failed")
36+
37+
@dataclass
38+
class FailedRead(AnnotatedDataclassValidationMixin):
39+
value: int
40+
41+
def __getattribute__(self, name):
42+
if name == "value":
43+
raise failure
44+
return object.__getattribute__(self, name)
45+
46+
with pytest.raises(RuntimeError) as raised:
47+
FailedRead(3)
48+
assert raised.value is failure

0 commit comments

Comments
 (0)