From acf0564a60538d59ac8e5cba61881f283205695b Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:35:01 +0400 Subject: [PATCH] Update parameter-read tests for children built on first read policyengine-core#570 makes ParameterNodeAtInstant build each child the first time it is read. Three tests relied on every child being built with the node, so they fail on a Core release containing that change. The read recorder now also patches __getattr__, which returns a child on its first read. The recorder test covers first and repeated attribute reads and fancy indexing served from the cached vectorial node, and the control parameter is built during the recording. The uprating cache test reads a node so that lower caches fill. Co-Authored-By: Claude Opus 5.5 --- .../lazy-parameter-node-tests.changed.md | 1 + .../test_labour_supply_parameters_are_read.py | 58 ++++++++++++++----- .../tests/test_uprating_inputs_reach_data.py | 4 +- 3 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 changelog.d/lazy-parameter-node-tests.changed.md diff --git a/changelog.d/lazy-parameter-node-tests.changed.md b/changelog.d/lazy-parameter-node-tests.changed.md new file mode 100644 index 0000000000..013421ed1e --- /dev/null +++ b/changelog.d/lazy-parameter-node-tests.changed.md @@ -0,0 +1 @@ +Updated the labour supply parameter-read test and the uprating cache test so they pass when policyengine-core builds parameter-node children on first read. diff --git a/policyengine_uk/tests/code_health/test_labour_supply_parameters_are_read.py b/policyengine_uk/tests/code_health/test_labour_supply_parameters_are_read.py index f3e5fa6180..5b49d80f1a 100644 --- a/policyengine_uk/tests/code_health/test_labour_supply_parameters_are_read.py +++ b/policyengine_uk/tests/code_health/test_labour_supply_parameters_are_read.py @@ -25,12 +25,18 @@ attribute or item of a node at an instant that holds a value (not a node), a direct call of a parameter or scale (``parameter(year)``), and fancy indexing of a node at an instant (``node[array]``), which reads every leaf under that -node. Building a node at an instant reads nothing: it resolves every child, -whether or not anything uses them. - -A control parameter that nothing reads is added to the tree of both runs. It -must stay unread, which shows the recorder does not count a parameter just -because it is in the tree. +node. A read straight from a node's ``_children`` is not recorded. + +Building a node at an instant does not count its children as read. A node may +build a child when it is first read: ``__getattr__`` then returns the child, +and ``__getattribute__`` returns it after that, so both are recorded. Building +a scale reads its brackets, which records the paths of the brackets but not the +path of the scale. + +A control parameter that nothing reads is added to the tree of both runs, and +every child of its node is built during the recording. It must stay unread, +which shows the recorder does not count a parameter just because it is in the +tree or has been built. """ import contextlib @@ -79,7 +85,7 @@ def leaf_paths(node: ParameterNode, path: str) -> set: def _leaves_at_instant(node: ParameterNodeAtInstant, path: str): - for key, child in object.__getattribute__(node, "_children").items(): + for key, child in node._children.items(): if isinstance(child, ParameterNodeAtInstant): yield from _leaves_at_instant(child, f"{path}.{key}") else: @@ -88,20 +94,26 @@ def _leaves_at_instant(node: ParameterNodeAtInstant, path: str): @contextlib.contextmanager def record_parameter_reads(): - """Record the path of every parameter read in the block.""" + """Record the paths of the kinds of parameter read listed above.""" reads = set() getattribute = ParameterNodeAtInstant.__getattribute__ + getattr_fallback = ParameterNodeAtInstant.__getattr__ getitem = ParameterNodeAtInstant.__getitem__ get_at_instant = AtInstantLike.get_at_instant + def record_attribute_read(self, key, value): + if not key.startswith("_") and not isinstance(value, ParameterNodeAtInstant): + reads.add(f"{getattribute(self, '_name')}.{key}") + def recording_getattribute(self, key): value = getattribute(self, key) - if ( - not key.startswith("_") - and not isinstance(value, ParameterNodeAtInstant) - and key in getattribute(self, "_children") - ): - reads.add(f"{getattribute(self, '_name')}.{key}") + if key in getattribute(self, "__dict__"): + record_attribute_read(self, key, value) + return value + + def recording_getattr(self, key): + value = getattr_fallback(self, key) + record_attribute_read(self, key, value) return value def recording_getitem(self, key): @@ -123,6 +135,7 @@ def recording_get_at_instant(self, instant): patch.setattr( ParameterNodeAtInstant, "__getattribute__", recording_getattribute ) + patch.setattr(ParameterNodeAtInstant, "__getattr__", recording_getattr) patch.setattr(ParameterNodeAtInstant, "__getitem__", recording_getitem) patch.setattr(AtInstantLike, "get_at_instant", recording_get_at_instant) yield reads @@ -175,6 +188,11 @@ def labour_supply_run(): with record_parameter_reads() as reads: response = formula_sim.calculate("employment_income_behavioral_response", YEAR) dynamics = dynamics_sim.apply_dynamics(YEAR) + for simulation in simulations: + # Build the control, which a node may otherwise leave unbuilt. + simulation.tax_benefit_system.parameters.get_child(CONTROL_NODE)( + f"{YEAR}-01-01" + )._children # Both paths ran in full: the formula measured a response, which it does # only after its early returns (its measurement branches are not kept), @@ -209,6 +227,7 @@ def test_recorder_does_not_count_a_parameter_nothing_reads(labour_supply_run): def test_recorder_counts_each_kind_of_read(): + getattr_fallback = ParameterNodeAtInstant.__getattr__ getitem = ParameterNodeAtInstant.__getitem__ get_at_instant = AtInstantLike.get_at_instant leaf = {"values": {"2000-01-01": 1}} @@ -216,22 +235,32 @@ def test_recorder_counts_each_kind_of_read(): "root", data={ "by_attribute": leaf, + "by_attribute_again": leaf, "by_item": leaf, "by_call": leaf, "unread": leaf, "vector": {"a": leaf, "b": leaf}, }, ) + # Read before recording starts, so the reads below use what these leave in + # the cached node: ``by_attribute_again`` is already an attribute, which + # does not reach ``__getattr__``, and ``vector`` already has its vectorial + # node, so fancy indexing does not read each child by name. + before_recording = root("2020-01-01") + before_recording.by_attribute_again + before_recording.vector[np.array(["a", "a"])] with record_parameter_reads() as reads: at_instant = root("2020-01-01") assert reads == set(), "Building a node at an instant is not a read." at_instant.by_attribute + at_instant.by_attribute_again at_instant["by_item"] root.by_call("2020-01-01") at_instant.vector[np.array(["a", "a"])] assert reads == { "root.by_attribute", + "root.by_attribute_again", "root.by_item", "root.by_call", "root.vector.a", @@ -239,5 +268,6 @@ def test_recorder_counts_each_kind_of_read(): } # The recorder leaves the classes as it found them. assert "__getattribute__" not in ParameterNodeAtInstant.__dict__ + assert ParameterNodeAtInstant.__getattr__ is getattr_fallback assert ParameterNodeAtInstant.__getitem__ is getitem assert AtInstantLike.get_at_instant is get_at_instant diff --git a/policyengine_uk/tests/test_uprating_inputs_reach_data.py b/policyengine_uk/tests/test_uprating_inputs_reach_data.py index 4956f94aad..8693501405 100644 --- a/policyengine_uk/tests/test_uprating_inputs_reach_data.py +++ b/policyengine_uk/tests/test_uprating_inputs_reach_data.py @@ -434,8 +434,8 @@ def test_a_failing_probe_leaves_the_system_and_its_caches_alone(): system = CountryTaxBenefitSystem() parameters = system.parameters - # Fill some node caches. - parameters("2027-01-01") + # Fill some node caches: reading a node caches it and the nodes above it. + parameters("2027-01-01").gov.hmrc.income_tax filled = [ n for n in parameters.get_descendants() if getattr(n, "_at_instant_cache", None) ]