Skip to content

fix(datasets): needs from one source are kept with one fetch - #239

Merged
billlyzhaoyh merged 3 commits into
mainfrom
fix/download-once-per-source
Dec 4, 2026
Merged

billlyzhaoyh merged 3 commits into
mainfrom
fix/download-once-per-source

Conversation

@billlyzhaoyh

Copy link
Copy Markdown
Contributor

Closes #238.

The problem

download_datasets() kept each need with download_need_figures(need), which forgets the need figures held in memory before it fetches. Several needs share one source: homelessness, low income, vaccinations and crime each have two. So each of those sources was fetched twice in one run. ONS limits repeated requests, and during #237's review about 10 requests in a couple of minutes were refused. The workspace build will keep everything with one download_datasets() call in the 0.4.0 pin bump.

The fix

download_datasets() now forgets the need figures once, before it fetches anything. It keeps each need through a new keep_fetched_need_figures(need), which uses what is already held, so needs from one file share one fetch.

  • download_need_figures() on its own still forgets first and fetches fresh figures, as before.
  • Only the need figures are forgotten, as before. Other figures held in memory, such as the deprivation index and map outlines, are left alone.
  • Census needs from Nomis are still one query each, because each need asks Nomis for different figures. That is unchanged.

Checks run

  • A regression test, test_keeping_both_needs_fetches_ukhsa_once. It failed on main, where the three requests were made twice, and passes with the fix.
  • ruff check, ruff format --check, ty check src, ast-grep as CI runs it, the full pytest (1,294 passed), mkdocs build --strict.
  • Live: keeping ons-police-recorded-crime and ukhsa-childhood-vaccinations into an empty folder made 6 requests, none repeated (12 before).
  • /code-review (no bugs, four nits) and /simplify. From those, the new function gives a clear error for an unknown need and has a full docstring, and clearing was narrowed from every cache to the need figures.

Not done: the census unemployment needs could share one Nomis query split by sex. That would be a separate change.

AI tools: Claude Code wrote the change and ran the checks above.

download_datasets() kept each need with download_need_figures(), which
forgets the need figures held in memory before fetching. So a source
with two needs, such as ONS's crime tables or UKHSA's vaccinations, was
fetched twice in one run, and ONS rate-limits repeated requests.

download_datasets() now forgets the need figures once, before it
fetches, and keeps each need through what was fetched in the run.
Calling download_need_figures() on its own still fetches afresh.

Signed-off-by: billlyzhaoyh <billlyzhaoyh@users.noreply.github.com>
Github-Issue: #238
The Codex review of #239 found three ways the fix changed what main did:

- download_datasets() forgot the need figures held in memory even when no
  need was kept, such as when keeping the postcode table alone. It now
  forgets them only when a need is among the datasets kept.
- A need's download in Dataset.downloads, run on its own, kept whatever
  was in memory and noted it as fetched today. It fetches afresh again, as
  on main. Only a run of download_datasets() shares what was fetched.
- download_need_figures() forgot the figures before it refused an unknown
  need. It checks the need first, as on main.

Each has a test that failed before the change.

Github-Issue: #238
From the /simplify review of #239:

- The two new vaccination tests served UKHSA a second time to see what
  was asked. The gov_uk fixture now hands out the fake request it
  already makes, as the homelessness and fuel poverty tests do.
- _NeedDownload's docstring repeated how a run shares a fetch, which
  _run_with_others already says.

No behaviour changes.

Github-Issue: #238
@billlyzhaoyh
billlyzhaoyh marked this pull request as ready for review December 4, 2026 18:24
@billlyzhaoyh
billlyzhaoyh requested a review from lucharo as a code owner December 4, 2026 18:24
@billlyzhaoyh
billlyzhaoyh merged commit c0af608 into main Dec 4, 2026
7 checks passed
@billlyzhaoyh
billlyzhaoyh deleted the fix/download-once-per-source branch December 4, 2026 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keeping a source's figures fetches it once per need

1 participant