From c65b5ea2167c7bb77595459325b91a89d6d9e46b Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Thu, 22 Oct 2026 02:11:23 +0400 Subject: [PATCH 1/2] Open the simulation runtime's spans with the typed span scope #879 already opens the annual run's eight request, dataset, policy and simulation-build spans in simulation_runtime.py with scopes.span, and annotates _build_simulation as returning the StageReportingSimulation that _report_calculation_start needs. This commit opens the ninth span, credential setup in setup_gcp_credentials, the same way, and documents why _build_simulation returns a StageReportingSimulation in both branches (ArtifactBaselineSimulation subclasses it). Neither change alters what a run does, and the executor's baseline stays at 68 entries. Co-Authored-By: Claude Opus 5.5 --- .../policyengine_simulation_executor/simulation_runtime.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_runtime.py b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_runtime.py index 257151403..7001ba7e2 100644 --- a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_runtime.py +++ b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_runtime.py @@ -157,7 +157,7 @@ def setup_gcp_credentials( with contextlib.ExitStack() as credentials_file_stack: try: credential_span = ( - runtime.span(ANNUAL_IMPACT_STAGES.name(Stage.CREDENTIAL_SETUP)) + scopes.span(runtime, ANNUAL_IMPACT_STAGES.name(Stage.CREDENTIAL_SETUP)) if runtime is not None else contextlib.nullcontext() ) @@ -690,6 +690,11 @@ def _build_simulation( policy: dict[str, Any] | None, region: RegionResolution, ) -> StageReportingSimulation: + """Build a simulation that reports when its calculation starts. + + A run with a deterministic baseline id gets an ArtifactBaselineSimulation, + which is a StageReportingSimulation that can load a precomputed baseline. + """ from policyengine_simulation_executor.baseline_artifacts import ( ArtifactBaselineSimulation, BaselineIdentityRequest, From 10fa9fae6d830588edf34aed171d2ef32b246453 Mon Sep 17 00:00:00 2001 From: Anthony Volk <14987227+anth-volk@users.noreply.github.com> Date: Mon, 26 Oct 2026 15:45:37 +0400 Subject: [PATCH 2/2] Type the simulation runtime's compiled-policy reads The last 2 Pyright errors in simulation_runtime.py, without changing what a run does: - _compile_interval_policy: compile_reform returns None only for an empty policy, which the function has already returned, so a None result now returns the policy unchanged instead of failing on .items(). - _build_simulation reassigned its `policy: dict | None` parameter to the compiled policy, which may be a Policy; the result now has its own name, runtime_policy, used where the reassigned name was. The UK weight-matrix fallback, whose region-type and entity-data reads this change also typed before, has since been removed from main, so those two fixes are no longer part of it. The executor's baseline drops from 68 entries to 66, and docs/generated/rules.md is rendered again. Co-Authored-By: Claude Opus 5.5 --- docs/generated/rules.md | 2 +- .../simulation_runtime.py | 11 +++++++---- rules/baselines/pyright-executor.txt | 2 -- rules/registry.toml | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/generated/rules.md b/docs/generated/rules.md index 719c83ca6..4268bb317 100644 --- a/docs/generated/rules.md +++ b/docs/generated/rules.md @@ -67,7 +67,7 @@ Every rule that governs `policyengine-sim-api` is listed here with how it is enf - `sim.ci.multiline-run-is-script`: 13 remaining in `rules/baselines/ci-multiline-run.txt` - `sim.microdata.weight-stripping-confined`: 0 remaining in `rules/baselines/sim-microdata-weight-stripping.txt` - `sim.quality.pyright-apis-integ`: 4 remaining in `rules/baselines/pyright-apis-integ.txt` -- `sim.quality.pyright-executor`: 68 remaining in `rules/baselines/pyright-executor.txt` +- `sim.quality.pyright-executor`: 66 remaining in `rules/baselines/pyright-executor.txt` - `sim.quality.pyright-gateway`: 0 remaining in `rules/baselines/pyright-gateway.txt` ### Exceptions and review dates diff --git a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_runtime.py b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_runtime.py index 7001ba7e2..470eb271a 100644 --- a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_runtime.py +++ b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_runtime.py @@ -314,6 +314,9 @@ def _compile_interval_policy( ) compiled = compile_reform(policy, year=year, model_version=country_module.model) + if compiled is None: + # compile_reform returns None only for an empty policy, returned above. + return policy parameter_values = [] for path, periods in compiled.items(): dated_values = [ @@ -712,7 +715,7 @@ def _build_simulation( spm_kwargs: dict[str, Any] = {"spm": selection} if selection is not None else {} country = params.get("country", "us") country_module = _country_module(country) - policy = _compile_interval_policy( + runtime_policy = _compile_interval_policy( policy, country_module=country_module, year=_parse_year(params) ) scoping_strategy = region.scoping_strategy @@ -721,7 +724,7 @@ def _build_simulation( params, country=country, dataset_is_default=dataset_selection.is_default, - policy=policy, + policy=runtime_policy, region_code=region.code, scoping_strategy=scoping_strategy, year=_parse_year(params), @@ -736,14 +739,14 @@ def _build_simulation( id=simulation_id, dataset=dataset, tax_benefit_model_version=country_module.model, - policy=policy, + policy=runtime_policy, scoping_strategy=scoping_strategy, ) return StageReportingSimulation( **spm_kwargs, dataset=dataset, tax_benefit_model_version=country_module.model, - policy=policy, + policy=runtime_policy, scoping_strategy=scoping_strategy, ) diff --git a/rules/baselines/pyright-executor.txt b/rules/baselines/pyright-executor.txt index 50db66e6b..91f8199eb 100644 --- a/rules/baselines/pyright-executor.txt +++ b/rules/baselines/pyright-executor.txt @@ -58,8 +58,6 @@ projects/policyengine-simulation-executor/src/policyengine_simulation_executor/s projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_output_common.py :: reportArgumentType :: Argument of type "Unknown | Any | Series" cannot be assigned to parameter "x" of type "ConvertibleToFloat" in function "__new__" projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_output_common.py :: reportAttributeAccessIssue :: Cannot access attribute "model_dump" for class "object" projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_output_common.py :: reportOptionalMemberAccess :: "data" is not a known attribute of "None" -projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_runtime.py :: reportAssignmentType :: Type "dict[str, Any] | Policy | None" is not assignable to declared type "dict[str, Any] | None" -projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_runtime.py :: reportOptionalMemberAccess :: "items" is not a known attribute of "None" projects/policyengine-simulation-executor/src/policyengine_simulation_executor/spm.py :: reportArgumentType :: Argument of type "object" cannot be assigned to parameter "defaults" of type "SPMSelection" in function "__init__" projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_parity.py :: reportArgumentType :: Argument of type "Series | Unknown | Self@DataFrame" cannot be assigned to parameter "actual_values" of type "Series" in function "_compare_column" projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_parity.py :: reportArgumentType :: Argument of type "Series | Unknown | Self@DataFrame" cannot be assigned to parameter "expected_values" of type "Series" in function "_compare_column" diff --git a/rules/registry.toml b/rules/registry.toml index 4e2f60794..19720c3be 100644 --- a/rules/registry.toml +++ b/rules/registry.toml @@ -1143,7 +1143,7 @@ job = "pyright" invocation = "python -m scripts.rules pyright" [rule.baseline] file = "rules/baselines/pyright-executor.txt" -count = 68 +count = 66 [rule.proof] bad_fixture = "rules/fixtures/sim.quality.pyright-executor/bad" good_fixture = "rules/fixtures/sim.quality.pyright-executor/good"