Skip to content

Switch HTTP client from httpx to httpx2; require Python ≥ 3.11 - #467

Open
ehinman wants to merge 9 commits into
mainfrom
update-httpx2
Open

ehinman wants to merge 9 commits into
mainfrom
update-httpx2

Conversation

@ehinman

@ehinman ehinman commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

Did not realize this was not on my fork...

Summary

This PR replaces httpx with httpx2 (https://pypi.org/project/httpx2/), Pydantic's maintained fork of httpx 0.28.1.
httpx hasn't had a stable release since 0.28.1, and httpx2 is maintained and gets security fixes. The public API is
the same, so the code change is mostly the rename. httpx2 requires Python 3.11, so this PR also raises the minimum
supported Python from 3.10 to 3.11. Python 3.10 reached end of life in October 2026.

Changes

  • Dependency: httpx is replaced by httpx2 in pyproject.toml and the pre-commit mypy hook. import httpx and httpx.X
    become httpx2 throughout dataretrieval/ and tests/.

  • Python 3.11 minimum:

    • requires-python is now >=3.11, and the classifiers and CI test matrix are updated to 3.11, 3.13, 3.14.
    • mypy's python_version and ruff's target-version are now 3.11.
    • The tomli backport and its version check are removed; the configuration code now uses tomllib from the standard
      library.
  • Test mocking:

    • pytest-httpx doesn't support httpx2 yet. Support is proposed in Add support for httpx2 Colin-b/pytest_httpx#239, which is still open.
    • This PR removes pytest-httpx and adds a small in-house httpx_mock fixture to tests/conftest.py. It copies the
      parts of pytest-httpx this suite uses: add_response, add_exception, get_requests and reset. It follows pytest-httpx's
      URL matching and response-reuse rules with strict mode off.
    • The fixture keeps the name httpx_mock, so no test bodies changed. Once Add waterdata.get_nearest_continuous helper #239 is released, the fixture can be
      deleted and pytest-httpx added back.
  • Docs:

    • The user guide, reference and architecture pages, AGENTS.md and CONTRIBUTING.md are updated.
    • Old NEWS entries and the ADRs are left as written, because they record what was true at the time.
    • A new NEWS entry describes the breaking changes.
  • Lint: lines that exceeded 88 characters are wrapped, and ruff's py311 rules replaced timezone.utc with
    datetime.UTC.

Breaking changes for users

  • Python 3.10 is no longer supported.
  • SSL certificates are checked against the operating system's trust store instead of certifi's bundled certificates.
    SSL_CERT_FILE, SSL_CERT_DIR and ssl_check=False still work. This should help on networks that inspect TLS traffic.
  • NetworkError.cause now holds an httpx2 exception, such as httpx2.ConnectError.
  • Log messages come from the httpx2 and httpcore2.* loggers.
  • Functions that return a raw response, such as streamstats with its default format, now return an httpx2.Response.

Testing

  • 1,759 offline tests pass, and branch coverage is 99.08% (the minimum is 98.9%).
  • ruff check and format, mypy (strict), lint-imports, xenon and complexipy all pass.
  • The tests were run in a clean environment with httpx2 2.16.2 and no httpx installed, which confirms nothing still
    depends on the old package.
  • The live API tests were run in my dev environment (pytest tests/ -m live). These are the only check of the new certificate handling against the real USGS services.

Follow-up

  • When pytest-httpx releases httpx2 support, delete the in-house fixture from tests/conftest.py and add pytest-httpx
    back to the test extra.

Co-authored by Claude Opus 5.5

@ehinman
ehinman requested a review from thodson-usgs November 16, 2026 22:40

@thodson-usgs thodson-usgs left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, but let's hold this until we're ready for the 2.0 release

ehinman and others added 6 commits November 23, 2026 08:09
pytest-httpx has no httpx2 support yet (Colin-b/pytest_httpx#239), so
this branch had replaced it with an in-house httpx_mock fixture. Keep
the released plugin instead: tests/conftest.py calls
httpx2.alias_httpx(), so the plugin's 'import httpx' resolves to httpx2,
then imports its fixture and pytest_configure hook. addopts disables the
plugin's autoload (-p no:pytest_httpx), which would import httpx before
the alias. The full suite passes unchanged against the plugin, and the
test files return to main's wording. Drop the alias and the -p flag once
pytest-httpx supports httpx2.

Also, after the Python 3.11 and httpx2 moves:
- Type BaseConfiguration.load with typing.Self and delete the _C TypeVar.
- Inline the lazy tomllib import and delete the one-caller _toml_parser().
- Drop the duplicate 3.11 classifier and the sys.version_info coverage
  exclusion, and the non_mocked_hosts fixture pytest-httpx no longer reads.
- Finish the rename: httpcore2.ConnectError in interruptions, httpx2 in
  the queryables regeneration snippet.
- ADR 0009 Compliance: the leaf test now allows no third-party package.
- Rewrap paragraphs left ragged by the tomli removal.
@thodson-usgs thodson-usgs added the v2 Breaking or major change: defer to the 2.0 release label Nov 23, 2026
NEWS.md: kept both entries (11/21 ratings bug fix above the 11/18 httpx2 entry).
ratings.py auto-merged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Breaking or major change: defer to the 2.0 release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants