Skip to content

Commit 774e677

Browse files
committed
Reconstruct nested dataclasses through their actual nominal owner (#16)
1 parent dbc3c64 commit 774e677

2 files changed

Lines changed: 44 additions & 14 deletions

File tree

‎src/objectstate/object_state.py‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2600,20 +2600,11 @@ def _extract_all_parameters_flat(
26002600
# First try from type annotation, then fall back to checking actual value
26012601
nested_type = self._get_nested_dataclass_type(info.param_type)
26022602

2603-
# A registered lazy runtime value owns its raw reconstruction type.
2604-
# The enclosing annotation commonly names the concrete base config,
2605-
# but reconstructing unresolved ``None`` sentinels through that base
2606-
# would run concrete validation before inheritance resolution.
2607-
if current_value is not None:
2608-
value_type = type(current_value)
2609-
if is_dataclass(value_type):
2610-
from objectstate.lazy_factory import get_base_type_for_lazy
2611-
2612-
if (
2613-
nested_type is None
2614-
or get_base_type_for_lazy(value_type) is not None
2615-
):
2616-
nested_type = value_type
2603+
# The actual dataclass owns both its flattened fields and their
2604+
# reconstruction, including concrete nominal subtypes and lazy
2605+
# overlays. An annotation supplies the owner only without a value.
2606+
if current_value is not None and is_dataclass(type(current_value)):
2607+
nested_type = type(current_value)
26172608

26182609
if nested_type is not None and current_value is not None:
26192610
# Store the nested config type reference at this path

‎tests/test_object_state_update_instance.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,45 @@ class ConfigWithNestedTopology:
7070
nested: NestedTopology = field(default_factory=NestedTopology)
7171

7272

73+
@dataclass(frozen=True)
74+
class SpatialConfig:
75+
width: int = 12
76+
77+
78+
@dataclass(frozen=True)
79+
class VolumeConfig(SpatialConfig):
80+
depth: int = 60
81+
82+
83+
@dataclass
84+
class ConfigWithSpatialDomain:
85+
spatial: SpatialConfig = field(default_factory=SpatialConfig)
86+
87+
88+
def test_nested_nominal_subtype_survives_raw_live_and_saved_reconstruction():
89+
state = ObjectState(ConfigWithSpatialDomain(VolumeConfig()), scope_id="volume")
90+
assert state.to_object().spatial == VolumeConfig()
91+
assert state.to_saved_resolved_object().spatial == VolumeConfig()
92+
assert state.to_resolved_object().spatial == VolumeConfig()
93+
94+
state.update_parameter("spatial.depth", 30)
95+
assert state.to_object().spatial == VolumeConfig(depth=30)
96+
assert state.to_resolved_object().spatial == VolumeConfig(depth=30)
97+
assert state.to_saved_resolved_object().spatial == VolumeConfig(depth=60)
98+
99+
100+
def test_nested_nominal_owner_changes_rebuild_parameter_topology():
101+
state = ObjectState(ConfigWithSpatialDomain(), scope_id="spatial")
102+
state.update_object_instance(ConfigWithSpatialDomain(VolumeConfig(width=24)))
103+
assert state.to_saved_resolved_object().spatial == VolumeConfig(width=24)
104+
assert "spatial.depth" in state.parameters
105+
106+
state.update_object_instance(ConfigWithSpatialDomain(SpatialConfig(width=18)))
107+
assert state.to_object().spatial == SpatialConfig(width=18)
108+
assert state.to_saved_resolved_object().spatial == SpatialConfig(width=18)
109+
assert "spatial.depth" not in state.parameters
110+
111+
73112
class DelegatedHost:
74113
__objectstate_delegate__ = "config"
75114

0 commit comments

Comments
 (0)