From c13c4f1e82b4fb5cfe8fa6cc2dfff147f3b4124b Mon Sep 17 00:00:00 2001 From: vahid-ahmadi Date: Sun, 22 Nov 2026 00:38:19 +0000 Subject: [PATCH] Describe only the person entity as is_person in v1 metadata policyengine-core flags any root population with no members as is_person. policyengine-uk's new firm entity is one, so /uk/metadata would describe firm with is_person true and the app would offer firm inputs as person variables. build_entities now sets is_person only for the entity whose key matches the first is_person entity (core's person_entity rule); keys rather than identity because a cloned system copies its person entity. Co-Authored-By: Claude Opus 5.5 --- changelog.d/firm-entity-is-person.fixed.md | 1 + policyengine_api/country.py | 15 +++++++++--- tests/unit/test_country.py | 28 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 changelog.d/firm-entity-is-person.fixed.md diff --git a/changelog.d/firm-entity-is-person.fixed.md b/changelog.d/firm-entity-is-person.fixed.md new file mode 100644 index 000000000..97d44ae6d --- /dev/null +++ b/changelog.d/firm-entity-is-person.fixed.md @@ -0,0 +1 @@ +Describe only a country's person entity as a person in `/{country}/metadata`, so the UK `firm` entity, which policyengine-core also flags `is_person`, is described as a non-person entity. diff --git a/policyengine_api/country.py b/policyengine_api/country.py index 06308e9d2..b426db888 100644 --- a/policyengine_api/country.py +++ b/policyengine_api/country.py @@ -403,14 +403,23 @@ def build_parameters(self) -> dict: return parameter_data def build_entities(self) -> dict: - """Describe every entity by key, with its roles (empty when it has none).""" + """Describe every entity by key, with its roles (empty when it has none). + + Only the system's person entity is described with ``is_person`` true. + policyengine-core also flags a root population with no members (such + as the UK ``firm`` entity) as ``is_person``, and takes the first such + entity as the person entity. Keys are compared because a cloned system + copies its person entity. + """ + entities = self.tax_benefit_system.entities + person_key = next((entity.key for entity in entities if entity.is_person), None) data = {} - for entity in self.tax_benefit_system.entities: + for entity in entities: entity_data = { "plural": entity.plural, "label": entity.label, "doc": entity.doc, - "is_person": entity.is_person, + "is_person": entity.key == person_key, "key": entity.key, } if hasattr(entity, "roles"): diff --git a/tests/unit/test_country.py b/tests/unit/test_country.py index 1c14cbbd7..807afab02 100644 --- a/tests/unit/test_country.py +++ b/tests/unit/test_country.py @@ -66,6 +66,34 @@ def test__json_safe_serializes_dates(self): } +class TestCountryEntities: + def test__build_entities_describes_only_the_person_entity_as_a_person(self): + # policyengine-core flags a root population with no members, such as + # the UK firm entity, as is_person. Only the first one is the person. + def core_entity(key, plural, *, is_person): + return SimpleNamespace( + key=key, plural=plural, label=key, doc="", is_person=is_person + ) + + country = PolicyEngineCountry.__new__(PolicyEngineCountry) + country.tax_benefit_system = SimpleNamespace( + entities=[ + core_entity("person", "people", is_person=True), + core_entity("household", "households", is_person=False), + core_entity("firm", "firms", is_person=True), + ] + ) + + entities = country.build_entities() + + assert {key: entity["is_person"] for key, entity in entities.items()} == { + "person": True, + "household": False, + "firm": False, + } + assert entities["firm"]["roles"] == {} + + class TestHouseholdCalculation: def test__calculate_does_not_enable_or_read_simulation_tracing(self, monkeypatch): class SimulationWithoutTracing: