-
Notifications
You must be signed in to change notification settings - Fork 18
Add opt-in httpx import alias helper #968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jonathan343
wants to merge
3
commits into
pydantic:main
Choose a base branch
from
jonathan343:httpx-alias
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import sys | ||
| from importlib import metadata | ||
|
|
||
| __all__ = ["enable_httpx_alias"] | ||
|
|
||
|
|
||
| def _httpx_distribution_installed() -> bool: | ||
| try: | ||
| metadata.distribution("httpx") | ||
| except metadata.PackageNotFoundError: | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| def _alias_loaded_httpx2_modules() -> None: | ||
| for name, module in list(sys.modules.items()): | ||
| if name == "httpx2" or name.startswith("httpx2."): | ||
| alias_name = f"httpx{name.removeprefix('httpx2')}" | ||
| existing_module = sys.modules.get(alias_name) | ||
| if existing_module is not None and existing_module is not module: | ||
| raise RuntimeError( | ||
| "Cannot alias httpx2 as httpx because another httpx module is already loaded. " | ||
| "Call httpx2.enable_httpx_alias() before anything imports httpx." | ||
| ) | ||
| sys.modules[alias_name] = module | ||
|
|
||
|
|
||
| def enable_httpx_alias() -> None: | ||
| """ | ||
| Alias `httpx2` as `httpx` for compatibility with existing code. | ||
|
|
||
| This must be called before anything imports `httpx`. It will fail if the | ||
| real `httpx` package is installed or if another `httpx` module has already | ||
| been imported in the current process. | ||
| """ | ||
| httpx2_module = sys.modules["httpx2"] | ||
|
|
||
| existing_httpx_module = sys.modules.get("httpx") | ||
| if existing_httpx_module is httpx2_module: | ||
| _alias_loaded_httpx2_modules() | ||
| return | ||
|
|
||
| loaded_httpx_modules = [name for name in sys.modules if name == "httpx" or name.startswith("httpx.")] | ||
| if loaded_httpx_modules: | ||
| raise RuntimeError( | ||
| "Cannot alias httpx2 as httpx because another httpx module is already loaded. " | ||
| "Call httpx2.enable_httpx_alias() before anything imports httpx." | ||
| ) | ||
|
|
||
| if _httpx_distribution_installed(): | ||
| raise RuntimeError( | ||
| "Cannot alias httpx2 as httpx because the httpx distribution is installed. " | ||
| "Uninstall httpx or use import httpx2 directly." | ||
| ) | ||
|
|
||
| sys.modules["httpx"] = httpx2_module | ||
| _alias_loaded_httpx2_modules() | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import sys | ||
| import types | ||
| import typing | ||
| from importlib import metadata | ||
|
|
||
| import pytest | ||
|
|
||
| import httpx2 | ||
| from httpx2 import _alias | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def restore_httpx_modules() -> typing.Iterator[None]: | ||
| original_modules = {name: module for name, module in sys.modules.items() if _is_httpx_module(name)} | ||
| _remove_httpx_modules() | ||
|
|
||
| yield | ||
|
|
||
| _remove_httpx_modules() | ||
| sys.modules.update(original_modules) | ||
|
|
||
|
|
||
| def _remove_httpx_modules() -> None: | ||
| for name in [name for name in sys.modules if _is_httpx_module(name)]: | ||
| del sys.modules[name] | ||
|
|
||
|
|
||
| def _is_httpx_module(name: str) -> bool: | ||
| return name == "httpx" or name.startswith("httpx.") | ||
|
|
||
|
|
||
| def _httpx_distribution_missing() -> bool: | ||
| return False | ||
|
|
||
|
|
||
| def _httpx_distribution_installed() -> bool: | ||
| return True | ||
|
|
||
|
|
||
| def test_httpx_distribution_installed_returns_false_when_missing(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| def distribution(name: str) -> object: | ||
| assert name == "httpx" | ||
| raise metadata.PackageNotFoundError | ||
|
|
||
| monkeypatch.setattr(metadata, "distribution", distribution) | ||
|
|
||
| assert not _alias._httpx_distribution_installed() | ||
|
|
||
|
|
||
| def test_httpx_distribution_installed_returns_true_when_found(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| def distribution(name: str) -> object: | ||
| assert name == "httpx" | ||
| return object() | ||
|
|
||
| monkeypatch.setattr(metadata, "distribution", distribution) | ||
|
|
||
| assert _alias._httpx_distribution_installed() | ||
|
|
||
|
|
||
| def test_enable_httpx_alias(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| monkeypatch.setattr(_alias, "_httpx_distribution_installed", _httpx_distribution_missing) | ||
|
|
||
| httpx2.enable_httpx_alias() | ||
|
|
||
| import httpx | ||
|
|
||
| assert httpx is httpx2 | ||
| assert httpx.Client is httpx2.Client | ||
|
|
||
|
|
||
| def test_enable_httpx_alias_is_idempotent(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| monkeypatch.setattr(_alias, "_httpx_distribution_installed", _httpx_distribution_missing) | ||
|
|
||
| httpx2.enable_httpx_alias() | ||
| httpx2.enable_httpx_alias() | ||
|
|
||
| assert sys.modules["httpx"] is httpx2 | ||
|
|
||
|
|
||
| def test_enable_httpx_alias_aliases_loaded_submodules(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| monkeypatch.setattr(_alias, "_httpx_distribution_installed", _httpx_distribution_missing) | ||
|
|
||
| httpx2.enable_httpx_alias() | ||
|
|
||
| import httpx._client as httpx_client | ||
|
|
||
| assert httpx_client is sys.modules["httpx2._client"] | ||
|
|
||
|
|
||
| def test_enable_httpx_alias_fails_when_httpx_distribution_is_installed(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| monkeypatch.setattr(_alias, "_httpx_distribution_installed", _httpx_distribution_installed) | ||
|
|
||
| with pytest.raises(RuntimeError, match="httpx distribution is installed"): | ||
| httpx2.enable_httpx_alias() | ||
|
|
||
|
|
||
| def test_enable_httpx_alias_fails_when_httpx_is_loaded() -> None: | ||
| sys.modules["httpx"] = types.ModuleType("httpx") | ||
|
|
||
| with pytest.raises(RuntimeError, match="another httpx module is already loaded"): | ||
| httpx2.enable_httpx_alias() | ||
|
|
||
|
|
||
| def test_enable_httpx_alias_fails_when_httpx_submodule_is_loaded() -> None: | ||
| sys.modules["httpx._client"] = types.ModuleType("httpx._client") | ||
|
|
||
| with pytest.raises(RuntimeError, match="another httpx module is already loaded"): | ||
| httpx2.enable_httpx_alias() | ||
|
|
||
|
|
||
| def test_enable_httpx_alias_fails_when_alias_submodule_is_replaced(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| monkeypatch.setattr(_alias, "_httpx_distribution_installed", _httpx_distribution_missing) | ||
|
|
||
| httpx2.enable_httpx_alias() | ||
| sys.modules["httpx._client"] = types.ModuleType("httpx._client") | ||
|
|
||
| with pytest.raises(RuntimeError, match="another httpx module is already loaded"): | ||
| httpx2.enable_httpx_alias() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this check needed? Assuming this gets run only once,
existing_httpx_modulewill beNone. If it's run twice, it prevents the next check from running.