Skip to content

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

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

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.

@mmcky

mmcky commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Thanks @earfman, and sorry to be closing this one — it is a thorough piece of work and the diagnosis went well beyond what the issue asked for. Two findings in particular were yours rather than ours: that overwrite=True would replace a good local file with the error page and still report success, and that the two existing tests had been passing against a saved error page, so the bug was quietly passing its own test. Both are worth recording, and they are the reason this closure is not a judgement on the code.

We have decided to retire the module instead of repairing it. quantecon/util/notebooks.py will be deprecated in 0.12.0 and removed in v1.0. requests is a hard dependency of QuantEcon.py solely to serve fetch_nb_dependencies, and the function has no remaining constituency — no usage across any of the lecture repositories, and the handful of callers we can find are dormant notebooks descending from two 2017-era demos. Its default remote was last updated in 2023. Any data still needed will move to QuantEcon/data-lectures, which is the canonical home for datasets referenced by stable URLs.

Given that, merging a fix to a function scheduled for removal would spend review on code we are deleting, so we are closing this and #904 together, and closing #870 as well so that nobody else picks it up and repeats the work. That last part is on us — #870 sat open as a high-priority bug and drew two independent fixes on the same morning.

Thank you for the care you put into this, and please do consider another issue on the tracker if you would like — this one simply stopped being worth fixing.

@earfman

earfman commented Sep 11, 2026

Copy link
Copy Markdown
Author

Thanks @mmcky, that makes sense. Retiring it beats patching something nobody calls. For transparency, this one was AI-assisted (Assisted-by: claude opus5), and I'll put that line on anything I send from here on. I'll look through the tracker for something else.

mmcky added a commit that referenced this pull request Oct 8, 2026
…#953)

* DEPR: deprecate fetch_nb_dependencies and demote requests

Deprecates `quantecon.util.notebooks` in 0.12.0 for removal in v1.0, and
moves `requests` out of the mandatory dependency set into a `notebooks`
extra that is removed with the module.

`requests` served exactly one call site in the package. The function has
no remaining constituency: no usage across lecture-python.myst,
lecture-python-advanced.myst, lecture-python-intro or
lecture-python-programming.myst, and the callers that exist are dormant
notebooks descending from two 2017-era demos. Its default remote,
QuantEcon/QuantEcon.notebooks, was last pushed 2023-07-15. Data still
needed moves to QuantEcon/data-lectures.

Calling the function emits a DeprecationWarning attributed to the
caller's frame. Where `requests` is absent the lazy import raises an
ImportError naming the extra. The stale 2016 TODO block, which no longer
described the code, is replaced by the deprecation directive.

Adds `r.raise_for_status()` as harm reduction for the module's final
release: #870 reported that a missing remote path saved GitHub's 404 HTML
page under the requested filename and reported success. Verified against
the reported reproducer, which now raises HTTPError and writes no file.
The other three defects in #870 are left unfixed, the function being
scheduled for removal.

Part of #880. Supersedes #870, #904 and #905, all closed.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* TEST: skip the notebooks tests without requests; set the deprecation version to 0.13.0

Addresses two Copilot comments: the module is skipped with importorskip
where requests (now the notebooks extra) is not installed, and the
teardown no longer fails when raise_for_status left no file behind. The
deprecation version becomes 0.13.0, the next release, matching #979.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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

2 participants