Skip to content

Storage keys need a structured (branch, period) scheme: str(period) is lossy and separators collide with branch names #526

Open

Description

@MaxGhenis

#523 / #525 fix the crash where get_known_periods/get_known_branch_periods split storage keys on every colon, but the underlying key scheme has deeper, pre-existing structural problems (surfaced in cross-family review of #525):

  1. str(period) is lossy for day-anchored periods. Keys embed f"{branch_name}:{period}", and Period.__str__ drops the day for e.g. month:2027-11-15:2 / year:2027-11-15:2, so distinct valid periods can collide in the cache and the readers reconstruct date-shifted periods.
  2. Branch names have no enforced grammar. Simulation.get_branch accepts arbitrary names; a branch name containing : breaks split(":", 1) parsing in the readers and in delete.
  3. OnDiskStorage has the same defect class with _ as the separator ({branch}_{period} + unconstrained split("_")): a branch name like my_reform mis-parses, and complex-period colons produce awkward filenames.

The clean fix is structured keys — (branch_name, Period) tuples in memory and an escaped/encoded filename scheme on disk — rather than string round-trips. That is a storage-format refactor deserving its own design + review, hence this issue rather than a rider on #525.

Activity

  1. MaxGhenis commented on Oct 5, 2026

    @MaxGhenis
    ContributorAuthor

    On Windows this is a hard failure, not only an awkward file name. OnDiskStorage.put saves to {branch}_{period}.npy, and Windows does not allow : in a file name. So any period whose string form has a colon (year:2012:2, month:2012-01:3, day:2012-01-01:3, or a year that starts mid-year such as year:2012-03) cannot be stored on disk there:

    OSError: [Errno 22] Invalid argument: 'C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\openfisca_mskse962\\uprated_any_unit\\default_year:2012:2.npy'
    

    That is from #582's Windows jobs (run 37336787831), whose tie tests store year:2012:2 and year:2014:3 on disk. The REPLACEMENTS_DIR path has the same problem: tempfile.mkstemp(prefix=f"{filename}.") also puts the period string in the file name.

    #582 now keeps multi-unit periods in memory on Windows in those tests (can_store_on_disk in tests/fixtures/uprating_order.py), and points here. The encoded on-disk file-name scheme proposed above would fix it. Its round-trip test should run on the Windows CI jobs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions