SPMForecast cannot be pickled or deep-copied (mappingproxy fields)

Open
#49 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python

Research direction

Start in spm_calculator/rolling_forecast.py, focusing on SPMForecast.from_dict and the immutable forecast fields described in the issue. Run the provided pickle and deepcopy reproduction, then add regression coverage showing that serialization round-trips the forecast and deepcopy preserves the immutable object; confirm the forecast’s to_dict() output remains unchanged.

Written by the indexing model from the issue text.

Description

Summary

SPMForecast stores its fields as types.MappingProxyType objects, which pickle and copy.deepcopy cannot handle (TypeError: cannot pickle 'mappingproxy' object). policyengine-us keeps an SPMForecast on every tax-benefit system (system.spm_forecast_provider.forecast, via policyengine_us.spm.CountyRequiringSPMProvider). So no policyengine-us tax-benefit system, and no simulation that holds one, can be deep-copied or pickled.

Where

spm_calculator/rolling_forecast.py on main (7030e1b28e): SPMForecast.from_dict wraps _entries, _calculation_entries, _areas and _assignments (and nested maps) in MappingProxyType.

Reproduction

policyengine-us 2.21.0 (main at fbe24ad1b3) with spm-calculator 1.0.0.post1:

import copy, pickle
from policyengine_us import Simulation

sim = Simulation(situation={
    "people": {"a": {"age": {"2026": 35}, "employment_income": {"2026": 30000}}},
    "households": {"h": {"members": ["a"], "state_name": {"2026": "CA"}}},
})
provider = sim.tax_benefit_system.spm_forecast_provider
copy.deepcopy(provider)   # TypeError: cannot pickle 'mappingproxy' object
pickle.dumps(provider)    # TypeError: cannot pickle 'mappingproxy' object

The four paths in a calculated household simulation all end in that forecast, e.g. sim.branches[...].tax_benefit_system.spm_forecast_provider.forecast._entries.

Why it matters

policyengine-us expects systems to pickle: test_shared_policy_class_has_an_import_path (policyengine_us/tests/core/test_spm_simulation_isolation.py) publishes the shared-policy class so "anything that pickles a system (a worker pool, joblib, a notebook checkpoint)" can find it. Until now a RecursionError in policyengine-core hid this, because no simulation could be copied at all; PolicyEngine/policyengine-core#567 fixes that. With the core fix and the systems left shared between original and copy, a policyengine-us household simulation deep-copies and round-trips through pickle and recalculates the same values. Only the forecast blocks a full copy.

Possible fix

SPMForecast is documented as immutable ("Immutable projected inputs; accessors always return detached copies") and keeps its canonical JSON in _json, which to_dict() returns. So:

def __reduce__(self):
    return type(self).from_dict, (self.to_dict(),)

def __copy__(self):
    return self

def __deepcopy__(self, memo):
    return self

from_dict revalidates the document, and as_of is only a validation check, so a rebuilt forecast should equal the original. A regression test could assert pickle.loads(pickle.dumps(f)).to_dict() == f.to_dict() and copy.deepcopy(f) is f.

Dominant language
Python
Stars
0
Forks
1
Avg merge
2d 15h
Merged PRs (30d)
13

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PolicyEngine/spm-calculator

All issues in PolicyEngine/spm-calculator

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.