Do a lazy import of the _main CLI module#947
Merged
Merged
Conversation
Importing the CLI code doubles the import time for httpx altogether. By importing lazily at runtime, the additional time only happens when running the httpx CLI, and not on every "import httpx".
The console script now points at a tiny `_cli.py` shim that lazily imports `_main` and preserves the friendly "install httpx[cli]" fallback. The `httpx2.main` attribute remains importable via `__getattr__` but emits a `DeprecationWarning` so existing callers have a migration path.
Drops the `_cli.py` shim and points the console script at `httpx2._main:main`. The friendly "install httpx[cli]" fallback is removed - users without the CLI extras will see a clear `ImportError`, which is enough signal that they need `pip install httpx2[cli]`.
`_main` is a CLI-only internal module; users shouldn't be steered toward calling it directly. Point them at the `httpx2` CLI entry point only.
Covers the two branches of `__getattr__` in `httpx2/__init__.py` that the existing CLI tests don't exercise (they import `main` from `httpx2._main` directly).
Bare `httpx2.does_not_exist` tripped ruff B018 (useless expression) and the previous `# type: ignore[attr-defined]` tripped mypy `unused-ignore` because `__getattr__` widens the module's attribute type. Assigning to `_` keeps both linters happy.
Drops the two coverage-only tests and the `pytest` import they required. The deprecation contract is documented by the warning text itself, and the function is small enough that `# pragma: no cover` is preferable to keeping dedicated tests for it.
Kludex
approved these changes
May 17, 2026
Member
|
I don't think the |
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.
Importing the CLI code doubles the import time for httpx altogether. By importing lazily at runtime, the additional time only happens when running the httpx CLI, and not on every "import httpx".
This speeds up imports by around 25%