Let a caller supply the storage backend to annual_cli.main() - #4584
Merged
Conversation
…ain() StorageBase is already an abstraction — annual_weather.py and annual_tariff.py only ever call self.storage.load()/save(), and AnnualPredictor takes whatever storage it is given — but annual_cli.main() hard-coded StorageLocalFiles. The only way to run the annual tool against a different backend was therefore to fork the CLI, or to reimplement main() and with it the --machine stdout/stderr contract, which is the fiddly part: the stdout redirect that stops a stray print() from corrupting the one-JSON-object stdout a parent process parses. main() now takes storage_factory, called as storage_factory(work_dir, log) — exactly how StorageLocalFiles is constructed, so the default is that class itself and the command line behaves as it always has. The motivating case is embedding the annual tool in a long-lived service. There a per-process work dir means every process re-downloads the same immutable ERA5 and Octopus data and none of it can be shared, which also multiplies requests against the rate APIs. A shared backend (Redis, S3, a database) fixes both, and now needs a factory rather than a fork. Tests cover both paths: the default still constructs StorageLocalFiles, and a supplied factory reaches AnnualPredictor and receives --work-dir and the run's log callable. The new assertions were verified to fail without the change (TypeError: unexpected keyword argument 'storage_factory'). The annual_cli, annual_cli_machine, annual_cli_machine_end_to_end, annual_job and storage suites all pass, and black 23.11.0 and ruff 0.11.4 (the pinned pre-commit versions) report no changes. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
annual_cli.main()gains an optionalstorage_factory, called asstorage_factory(work_dir, log)— exactly howStorageLocalFilesis constructed today, so the default is that class itself and the command line behaves as it always has.Why
StorageBaseis already an abstraction:annual_weather.pyandannual_tariff.pyonly ever callself.storage.load()/save(), andAnnualPredictoruses whatever storage it is handed. But this entry point hard-coded the one implementation, so running the annual tool against a different backend meant forking the CLI — or reimplementingmain(), and with it the--machinestdout/stderr contract. That last part is the fiddly bit worth not duplicating: theredirect_stdoutguard that stops a strayprint()from anythingpredictor.run()pulls in from corrupting the single JSON object a parent process parses off stdout.The motivating case is embedding the annual tool in a long-lived service. There, a per-process work dir means every process re-downloads the same immutable ERA5 and Octopus data, none of it can be shared between them, and the duplicated requests land on the rate APIs. Pointing several processes at one shared backend (Redis, S3, a database) fixes both — and with this, that needs a factory rather than a fork.
Scope
Two lines of behaviour change, plus a docstring. No change to the CLI, its arguments, or its output. Nothing else in the file moves.
Testing
Added to
tests/test_annual_cli.py, in the existing style:StorageLocalFiles— so a plain command-line run is unchangedAnnualPredictor, and the factory receives--work-dirand the run'slogcallableI checked the new assertions genuinely fail without the change (
TypeError: main() got an unexpected keyword argument 'storage_factory') rather than passing vacuously.Suites run and passing:
annual_cli,annual_cli_machine,annual_cli_machine_end_to_end,annual_job,storage.black23.11.0 andruff0.11.4 — the versions pinned in.pre-commit-config.yaml— both report no changes. (The commit itself was made with--no-verifyonly because thepre-commitbinary on this machine has a broken install; the hooks themselves were run by hand.)