From b39e0385072f32191fd41330b5167318eb5e9b4f Mon Sep 17 00:00:00 2001 From: vahid-ahmadi Date: Sun, 22 Nov 2026 00:35:50 +0000 Subject: [PATCH] Publish only the system's person entity as is_person policyengine-core flags any root population with no members as is_person. policyengine-uk's new firm entity is one, so the model version published two person entities (person and firm), which breaks the "exactly one person entity" contract in the API catalog and misroutes firm inputs in the app. Derive is_person by comparing each entity's key with the key of the first is_person entity (core's own person_entity rule). Keys, not identity: a cloned core system copies its person entity. Tests: a fake-system regression test with a trailing firm entity, the country-model check now uses the person_entity key rule, and the UK pin accepts an optional trailing ("firm", "firms", False) row so the tests pass on both the pinned policyengine-uk 2.177.7 and the firm release. Co-Authored-By: Claude Opus 5.5 --- changelog.d/764.fixed.md | 1 + .../common/model_version.py | 14 +++++- tests/test_model_entities.py | 46 +++++++++++++++++-- 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 changelog.d/764.fixed.md diff --git a/changelog.d/764.fixed.md b/changelog.d/764.fixed.md new file mode 100644 index 000000000..c6216818d --- /dev/null +++ b/changelog.d/764.fixed.md @@ -0,0 +1 @@ +Publish only the country system's person entity with `is_person=True`, so a root population that core also flags as a person (the UK `firm` entity) is published as a non-person entity. diff --git a/src/policyengine/tax_benefit_models/common/model_version.py b/src/policyengine/tax_benefit_models/common/model_version.py index 31fcc7eac..c1f189542 100644 --- a/src/policyengine/tax_benefit_models/common/model_version.py +++ b/src/policyengine/tax_benefit_models/common/model_version.py @@ -205,12 +205,22 @@ def _dataset_class(self): # --- Shared loading helpers ------------------------------------------ def _populate_entities(self, system) -> None: - """Copy the entities and basic inputs the country package declares.""" + """Copy the entities and basic inputs the country package declares. + + Only the system's person entity is published with ``is_person=True``. + Core also flags root populations with no members (such as the UK + ``firm`` entity) as ``is_person``, and takes the first such entity as + the person entity. Compare keys rather than identity, because a cloned + core system copies its person entity. + """ + person_key = next( + (entity.key for entity in system.entities if entity.is_person), None + ) self.entities = [ Entity( key=core_entity.key, plural=core_entity.plural, - is_person=bool(core_entity.is_person), + is_person=core_entity.key == person_key, ) for core_entity in system.entities ] diff --git a/tests/test_model_entities.py b/tests/test_model_entities.py index 8f46456ca..dc27ec587 100644 --- a/tests/test_model_entities.py +++ b/tests/test_model_entities.py @@ -41,6 +41,31 @@ def test_populate_entities_records_key_plural_and_person_flag_in_order(): assert model.basic_inputs == ["region", "employment_income", "age"] +def test_populate_entities_publishes_only_the_person_entity_as_a_person(): + # Core flags a root population with no members, such as the UK firm + # entity, as is_person. Only the system's person entity (the first one + # core flags) is a person entity downstream. + model = make_empty_model_version() + system = SimpleNamespace( + entities=[ + _core_entity("person", "people", True), + _core_entity("benunit", "benunits", False), + _core_entity("household", "households", False), + _core_entity("firm", "firms", True), + ], + basic_inputs=[], + ) + + MicrosimulationModelVersion._populate_entities(model, system) + + assert model.entities == [ + Entity(key="person", plural="people", is_person=True), + Entity(key="benunit", plural="benunits", is_person=False), + Entity(key="household", plural="households", is_person=False), + Entity(key="firm", plural="firms", is_person=False), + ] + + def test_populate_entities_accepts_a_country_model_without_basic_inputs(): model = make_empty_model_version() system = SimpleNamespace( @@ -84,7 +109,7 @@ def test_country_models_publish_their_entities_and_basic_inputs(country_model): assert [ (entity.key, entity.plural, entity.is_person) for entity in model.entities ] == [ - (entity.key, entity.plural, bool(entity.is_person)) + (entity.key, entity.plural, entity.key == country_system.person_entity.key) for entity in country_system.entities ] assert [entity.key for entity in model.entities if entity.is_person] == ["person"] @@ -114,9 +139,24 @@ def test_country_models_publish_their_entities_and_basic_inputs(country_model): } +# Entities a country package may add after the pinned version. policyengine-uk +# adds a firm entity (a root population with no members) that core flags as +# is_person; it must be published as a non-person entity. Move the row into +# PINNED_ENTITIES once the pin includes it. +OPTIONAL_TRAILING_ENTITIES = { + "uk": [("firm", "firms", False)], + "us": [], +} + + def test_country_models_publish_their_pinned_entities(country_model): - assert [ + country_id = country_model.country_id + published = [ (entity.key, entity.plural, entity.is_person) for entity in country_model.model_version.entities - ] == PINNED_ENTITIES[country_model.country_id] + ] + assert published in ( + PINNED_ENTITIES[country_id], + PINNED_ENTITIES[country_id] + OPTIONAL_TRAILING_ENTITIES[country_id], + ) assert {"employment_income", "age"} <= set(country_model.model_version.basic_inputs)