diff --git a/extropy/population/sampler/core.py b/extropy/population/sampler/core.py index c0ade8c..4c2053b 100644 --- a/extropy/population/sampler/core.py +++ b/extropy/population/sampler/core.py @@ -310,6 +310,11 @@ def _sample_dependent_as_agent( agent["household_id"] = household_id agent["household_role"] = f"dependent_{dependent.relationship}" agent["relationship_to_primary"] = dependent.relationship + dep_name = str(getattr(dependent, "name", "")).strip() + if dep_name: + agent["first_name"] = dep_name + if parent.get("last_name"): + agent["last_name"] = parent["last_name"] # Copy household-scoped attributes from parent for attr in spec.attributes: diff --git a/tests/test_agent_focus.py b/tests/test_agent_focus.py index a4a661b..f0b2940 100644 --- a/tests/test_agent_focus.py +++ b/tests/test_agent_focus.py @@ -518,3 +518,28 @@ def test_different_focus_values(self): for focus in ["surgeons", "retired couples", "families", None]: spec = _make_household_spec(size=100, agent_focus=focus) assert spec.meta.agent_focus == focus + + +class TestPromotedDependentNames: + def test_promoted_dependents_preserve_household_names(self): + spec = _make_household_spec(size=240, agent_focus="families") + result = sample_population(spec, count=240, seed=42) + + promoted = [ + a + for a in result.agents + if a.get("household_role", "").startswith("dependent_") + ] + assert promoted, "Expected promoted dependents in families mode" + + households = {h["id"]: h for h in getattr(result, "_households", [])} + for dep in promoted: + first_name = dep.get("first_name") + assert first_name, "Promoted dependents must have first_name" + + hh = households.get(dep["household_id"]) + assert hh is not None + dependent_names = {d.get("name") for d in hh.get("dependent_data", [])} + assert first_name in dependent_names, ( + "Promoted dependent names should stay aligned with generated dependent records" + )