SPMForecast cannot be pickled or deep-copied (mappingproxy fields)
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
- Domain
- developer-experience
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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from PolicyEngine/spm-calculator
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
PolicyEngine/spm-calculator#67 ·
Maintainers usually reply within 2 days
-
Difficulty 4/5 1-2 days Newbie friendliness 62/100
PolicyEngine/spm-calculator#53 ·
Maintainers usually reply within 2 days
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
PolicyEngine/spm-calculator#48 · 8 comments ·
Maintainers usually reply within 2 days
-
Difficulty 4/5 3-5 days Newbie friendliness 58/100
PolicyEngine/spm-calculator#47 ·
Maintainers usually reply within 2 days
-
Difficulty 5/5 Over a week Newbie friendliness 45/100
PolicyEngine/spm-calculator#35 ·
Maintainers usually reply within 2 days
All issues in PolicyEngine/spm-calculator
Similar issues
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
meetecho/juturna_nodes#43 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
TigreGotico/text2spoken#726 ·
Maintainers usually reply within 1 day
-
bug dependencies
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
nolte/claude-shared#822 ·
Maintainers usually reply within 1 day
-
`RequestHandler.build_request_kwargs` drops empty request bodies such as `{}` or `""`Possibly taken @abhirajadhikary06 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
typesense/typesense-python#170 ·
Maintainers usually reply within 1 day
-
claude-subscription-directsdk: tool calls with raw control characters in string values are droppedOpen
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
NousResearch/hermes-official-plugins#538 ·
Maintainers usually reply within 3 days