Skip to content

FIX: do not write failed downloads to disk in fetch_nb_dependencies - #905

Open
earfman wants to merge 2 commits into
QuantEcon:mainfrom
earfman:fix-fetch-nb-dependencies-error-handling
Open

FIX: do not write failed downloads to disk in fetch_nb_dependencies#905
earfman wants to merge 2 commits into
QuantEcon:mainfrom
earfman:fix-fetch-nb-dependencies-error-handling

Conversation

@earfman

@earfman earfman commented Jul 25, 2026

Copy link
Copy Markdown

Fixes #870

Addresses all six acceptance criteria from the issue.

Behaviour change

A failed request no longer produces a file. Previously the response body was written unconditionally and True appended, so a missing remote path saved GitHub's HTML error page under the requested filename and reported it as fetched — the failure surfaced later as a parse error somewhere unrelated.

Reproduced against a local server returning 404 (no network dependency):

before:  status [True]   absent.csv written (404 HTML body on disk)
after:   status [False]  no file, UserWarning emitted

While verifying I found one more consequence worth flagging: with overwrite=True, the old code replaced a good local file with the error page and still reported success. A file containing real data became <html>...404...</html>. That is silent data loss, and it is also fixed here.

Changes

  • raise_for_status() inside a try; on requests.RequestException the code warns, appends False, and skips the write
  • module-level TIMEOUT = (10, 30) (connect, read), overridable via a new timeout= keyword argument
  • the file handle no longer shadows the loop variable (as f rather than as fl)
  • isinstance(files, (list, tuple)) replaces type(files) == list, so a tuple no longer falls through to the dict branch and raises AttributeError
  • the stale 2016 TODO block is removed from the module docstring (wget is long gone), and the return value is now documented

I looked at the update-fetch-utility branch mentioned in the issue — it is docstring reformatting and import hoisting only, none of the four defects are addressed there, so nothing to salvage.

Tests

New TestFetchFailures class, offline (a local http.server on an ephemeral port, tmp_path + monkeypatch.chdir, no network, no stray files): missing remote file returns False and writes nothing, a 200 still writes correct bytes, tuple input is accepted, and the timeout is forwarded — including an assertion that the default is a real bound rather than None.

The two existing tests are unchanged and still pass. Worth knowing: they fetch a live URL, so in a sandbox without access to github.com they were previously passing on a saved error page (the bug passing its own test). They pass normally against the real URL, which is what CI sees.

Notes for review

  • Callers who relied on requests exceptions propagating now get False in the status list instead. The new Returns section documents this; happy to re-raise instead if you would rather keep the exception contract.
  • raise_for_status() only inspects the HTTP status. A host that returns 200 with an HTML page for a wrong path would still be written verbatim; a content-type check would be a separate change.
  • The (10, 30) default is a per-read-gap timeout, not a total budget: a slow-but-steady large download still succeeds. I checked that a 120 KB transfer trickling over ~58s completes fine.

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.

FIX: fetch_nb_dependencies writes HTTP error pages to disk and reports success

1 participant