Allow overriding the network timeout with HATCH_NETWORK_TIMEOUT - #2355
Open
puneetgani wants to merge 1 commit into
Open
Allow overriding the network timeout with HATCH_NETWORK_TIMEOUT#2355puneetgani wants to merge 1 commit into
puneetgani wants to merge 1 commit into
Conversation
The default timeout applied to Hatch's own network requests was hardcoded to 10 seconds, which is too short on slow or heavily proxied connections. The only workaround was patching the constant. Add `get_timeout`, which returns `DEFAULT_TIMEOUT` unless the `HATCH_NETWORK_TIMEOUT` environment variable is set to a positive number, and use it for downloads and the package index client. An environment variable is used rather than a `config.toml` option because neither call site has access to the application config: `PythonManager` is constructed without it and the default template only receives a cache directory. Closes pypa#2158 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.
Closes #2158
What
DEFAULT_TIMEOUTinhatch/utils/network.py(added in #1531) is hardcoded to 10 seconds and applies to every network request Hatch makes itself. On slow or heavily proxied connections that is too short, and the only workaround available today is patching the constant, which is what the issue reporter resorted to.This adds a
get_timeout()helper that returnsDEFAULT_TIMEOUTunless theHATCH_NETWORK_TIMEOUTenvironment variable is set, and uses it at both call sites:download_file— Python distributions and SPDX license textsPackageIndex.client— publishing to a package indexThe default behaviour is unchanged.
$ HATCH_NETWORK_TIMEOUT=60 hatch python install 3.12Why an environment variable rather than
config.tomlNeither call site has access to the application config:
PythonManageris constructed without it, andDefaultTemplateonly receives a cache directory. Threading config into both would be a much larger change, whereas an environment variable works from anywhere and matches the existingHATCH_*convention (HATCH_CACHE_DIR,HATCH_DATA_DIR,HATCH_PYTHON_SOURCE_*). I'm happy to rework this as aconfig.tomloption instead if you'd prefer that direction.This also covers the publisher-timeout half of #761, which @jamesdow21 noted on the issue is the same underlying problem.
Validation
The value must parse as a finite, positive number; anything else raises a
ValueErrornaming the variable. An explicitly passedtimeout=keyword still wins over the environment variable, sincedownload_fileusessetdefault.Notes
This does not change the timeout of the installers Hatch shells out to (
pip,uv) — those have their own settings, which the docs now point to. The issue title mentionspip install, butDEFAULT_TIMEOUTnever reached pip; the body and the reporter's workaround are about the constant itself, which is what this changes.Changes
src/hatch/utils/network.py—get_timeout(), used bydownload_filesrc/hatch/index/core.py— useget_timeout()for the index clientsrc/hatch/config/constants.py—AppEnvVars.NETWORK_TIMEOUTdocs/config/hatch.md— new "Network" sectiondocs/history/hatch.md— changelog entrytests/utils/test_network.py— new file, 16 tests covering the default, overrides, validation failures, and that the value reachesstreaming_responseruff check,ruff format --check, andmypypass on the changed files.tests/utils,tests/index,tests/python,tests/config, andtests/publishpass locally (260 passed, 30 skipped).AI disclosure
Per the AI contributions policy in
CONTRIBUTING.md: this change was written with Claude Code (Opus 5). I reviewed the design decision, the diff, and the tests, and verified the behaviour end to end against a livehatch python installrun. I can explain and defend every line of it.