Skip to content

Commit 73b739b

Browse files
committed
fix(links): use dynamic person entity key instead of hardcoded 'person'
ImplicitOne2ManyLink hardcoded target_entity_key='person', which crashes when the person entity has a different key (e.g. 'individu' in openfisca-france). This fix adds a person_entity_key parameter to ImplicitOne2ManyLink and passes the actual person entity key from Simulation._resolve_links(). Fixes the France API crash: KeyError: "Link 'individus': target entity 'person' not found in populations ['individu', 'famille', 'foyer_fiscal', 'menage']"
1 parent 1a87a41 commit 73b739b

5 files changed

Lines changed: 7 additions & 7 deletions

File tree

openfisca_core/links/implicit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ def _project_implicit(self, result: numpy.ndarray) -> numpy.ndarray:
3838
class ImplicitOne2ManyLink(One2ManyLink):
3939
"""A group → person link using GroupPopulation's internal arrays."""
4040

41-
def __init__(self, name: str, group_entity_key: str):
41+
def __init__(self, name: str, group_entity_key: str, person_entity_key: str):
4242
super().__init__(
4343
name=name,
4444
link_field="", # Not used
45-
target_entity_key="person", # The target of the O2M is persons
45+
target_entity_key=person_entity_key,
4646
)
4747
self._group_entity_key = group_entity_key
4848

openfisca_core/links/tests/test_edge_cases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ class is_female(variables.Variable):
294294
)
295295

296296
# Create and bind implicit link
297-
link = ImplicitOne2ManyLink("persons", "household")
297+
link = ImplicitOne2ManyLink("persons", "household", "person")
298298
pop = sim.populations["household"]
299299
link.attach(pop)
300300
link.resolve(sim.populations)

openfisca_core/links/tests/test_implicit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_implicit_many2one(sim):
6262

6363

6464
def test_implicit_one2many(sim):
65-
link = ImplicitOne2ManyLink("persons", "household")
65+
link = ImplicitOne2ManyLink("persons", "household", "person")
6666
link.attach(sim.populations["household"])
6767
link.resolve(sim.populations)
6868

openfisca_core/simulations/simulation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def _resolve_links(self) -> None:
115115
# group -> persons (One2Many)
116116
o2m_name = person_entity.plural
117117
if not population.entity.get_link(o2m_name):
118-
o2m = ImplicitOne2ManyLink(o2m_name, group_key)
118+
o2m = ImplicitOne2ManyLink(o2m_name, group_key, person_entity.key)
119119
population.entity.add_link(o2m)
120120

121121
from copy import copy

tests/core/test_link_accessors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class rent(variables.Variable):
5353

5454

5555
def test_nth_accessor(simple_sim):
56-
link = ImplicitOne2ManyLink("persons", "household")
56+
link = ImplicitOne2ManyLink("persons", "household", "person")
5757
link.attach(simple_sim.populations["household"])
5858
link.resolve(simple_sim.populations)
5959

@@ -67,7 +67,7 @@ def test_nth_accessor(simple_sim):
6767

6868

6969
def test_one2many_get_by_role(simple_sim):
70-
link = ImplicitOne2ManyLink("persons", "household")
70+
link = ImplicitOne2ManyLink("persons", "household", "person")
7171
link.attach(simple_sim.populations["household"])
7272
link.resolve(simple_sim.populations)
7373

0 commit comments

Comments
 (0)