Skip to content

Commit 3b6f02e

Browse files
Migrate airflow-core to httpx2 (#74244)
* Migrate airflow-core from `httpx` to `httpx2` * Ban httpx imports in airflow-core with ruff instead of ast-grep Ruff's banned-api rule already covers this, so a separate ast-grep hook only adds tooling. It also keeps airflow-core consistent with how the Task SDK enforces the same rule.
1 parent c510e4d commit 3b6f02e

9 files changed

Lines changed: 24 additions & 25 deletions

File tree

‎airflow-core/pyproject.toml‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ dependencies = [
102102
# and reports them as a 400. Below it, an unauthenticated form body to POST /auth/token is
103103
# parsed with no field-count or field-size bound at all.
104104
"starlette>=1.3.1",
105-
"httpx>=0.25.0",
106-
# Only for InProcessExecutionAPI's transports, which feed httpx2-based Task SDK clients.
107-
# The rest of airflow-core is still on httpx; tracked at https://github.com/apache/airflow/issues/70522
108105
"httpx2>=2.0.0",
109106
'importlib_metadata>=6.5;python_version<"3.12"',
110107
'importlib_metadata>=7.0;python_version>="3.12"',
@@ -348,3 +345,9 @@ shared_distributions = [
348345
"apache-airflow-shared-plugins-manager",
349346
"apache-airflow-shared-providers-discovery",
350347
]
348+
349+
[tool.ruff]
350+
extend = "../pyproject.toml"
351+
352+
[tool.ruff.lint.flake8-tidy-imports.extend-banned-api]
353+
"httpx".msg = "Use httpx2; httpx objects do not interoperate with the httpx2-based Task SDK client. See https://github.com/apache/airflow/issues/70522"

‎airflow-core/src/airflow/api/client/local_client.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from __future__ import annotations
2121

22-
import httpx
22+
import httpx2
2323

2424
from airflow.api.common import delete_dag, trigger_dag
2525
from airflow.exceptions import AirflowBadRequest, PoolNotFound
@@ -30,8 +30,8 @@
3030
class Client:
3131
"""Local API client implementation."""
3232

33-
def __init__(self, auth=None, session: httpx.Client | None = None):
34-
self._session: httpx.Client = session or httpx.Client()
33+
def __init__(self, auth=None, session: httpx2.Client | None = None):
34+
self._session: httpx2.Client = session or httpx2.Client()
3535
if auth:
3636
self._session.auth = auth
3737

‎airflow-core/src/airflow/api_fastapi/auth/tokens.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from typing import TYPE_CHECKING, Any, Literal, overload
2727

2828
import attrs
29-
import httpx
29+
import httpx2
3030
import jwt
3131
import structlog
3232
from asgiref.sync import async_to_sync
@@ -107,7 +107,7 @@ class JWKS:
107107
fetched_at: float = 0
108108
last_fetch_attempt_at: float = 0
109109

110-
client: httpx.AsyncClient = attrs.field(factory=httpx.AsyncClient)
110+
client: httpx2.AsyncClient = attrs.field(factory=httpx2.AsyncClient)
111111

112112
_jwks: jwt.PyJWKSet | None = None
113113
refresh_jwks: bool = True

‎airflow-core/src/airflow/api_fastapi/execution_api/in_process.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ def app(self):
118118

119119
return self._app
120120

121-
# httpx2, not httpx: the only consumers are Task SDK clients, which subclass httpx2.Client.
122121
@cached_property
123122
def transport(self) -> httpx2.WSGITransport:
124123
import httpx2

‎airflow-core/src/airflow/example_dags/example_dag_decorator.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# [START dag_decorator_usage]
2323
from typing import TYPE_CHECKING, Any
2424

25-
import httpx
25+
import httpx2
2626
import pendulum
2727

2828
from airflow.providers.standard.operators.bash import BashOperator
@@ -42,7 +42,7 @@ def __init__(self, *, url: str, **kwargs):
4242
self.url = url
4343

4444
def execute(self, context: Context):
45-
return httpx.get(self.url).json()
45+
return httpx2.get(self.url).json()
4646

4747

4848
@dag(

‎airflow-core/tests/unit/api_fastapi/auth/test_tokens.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from unittest.mock import patch
2525

2626
import anyio
27-
import httpx
27+
import httpx2
2828
import jwt
2929
import pytest
3030
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey
@@ -63,9 +63,9 @@ async def test_fetch_jwks_success(self, private_key):
6363
jwk_content = json.dumps({"keys": [key_to_jwk_dict(private_key, "kid")]})
6464

6565
async def mock_transport(request):
66-
return httpx.Response(status_code=200, content=jwk_content)
66+
return httpx2.Response(status_code=200, content=jwk_content)
6767

68-
client = httpx.AsyncClient(transport=httpx.MockTransport(mock_transport))
68+
client = httpx2.AsyncClient(transport=httpx2.MockTransport(mock_transport))
6969
jwks = JWKS(url="https://example.com/jwks.json", client=client)
7070

7171
# Test fetching JWKS
@@ -79,9 +79,9 @@ async def test_refresh_remote_jwks(
7979
jwk_content = json.dumps({"keys": [key_to_jwk_dict(ed25519_private_key, "kid")]})
8080

8181
async def mock_transport(request):
82-
return httpx.Response(status_code=200, content=jwk_content)
82+
return httpx2.Response(status_code=200, content=jwk_content)
8383

84-
client = httpx.AsyncClient(transport=httpx.MockTransport(mock_transport))
84+
client = httpx2.AsyncClient(transport=httpx2.MockTransport(mock_transport))
8585
current = 1_000_000.0
8686

8787
def mock_monotonic():

‎airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_dag_sources.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import pendulum
2424
import pytest
25-
from httpx import Response
25+
from httpx2 import Response
2626
from sqlalchemy import select
2727

2828
from airflow.api_fastapi.core_api.routes.public.dag_sources import REDACTED_SOURCE

‎airflow-core/tests/unit/api_fastapi/execution_api/versions/head/test_hitl.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import pytest
2323
import time_machine
24-
from httpx import Client
2524
from sqlalchemy import select
2625
from uuid6 import uuid7
2726

@@ -187,7 +186,7 @@ def test_upsert_hitl_detail_with_empty_option(
187186

188187
@time_machine.travel(datetime(2025, 7, 3, 0, 0, 0), tick=False)
189188
@pytest.mark.usefixtures("sample_hitl_detail")
190-
def test_update_hitl_detail(client: Client, sample_ti: TaskInstance) -> None:
189+
def test_update_hitl_detail(client: TestClient, sample_ti: TaskInstance) -> None:
191190
response = client.patch(
192191
f"/execution/hitlDetails/{sample_ti.id}",
193192
json={
@@ -206,7 +205,7 @@ def test_update_hitl_detail(client: Client, sample_ti: TaskInstance) -> None:
206205
}
207206

208207

209-
def test_update_hitl_detail_without_option(client: Client, sample_ti: TaskInstance) -> None:
208+
def test_update_hitl_detail_without_option(client: TestClient, sample_ti: TaskInstance) -> None:
210209
response = client.patch(
211210
f"/execution/hitlDetails/{sample_ti.id}",
212211
json={
@@ -218,7 +217,7 @@ def test_update_hitl_detail_without_option(client: Client, sample_ti: TaskInstan
218217
assert response.status_code == 422
219218

220219

221-
def test_update_hitl_detail_without_ti(client: Client) -> None:
220+
def test_update_hitl_detail_without_ti(client: TestClient) -> None:
222221
ti_id = str(uuid7())
223222
response = client.patch(
224223
f"/execution/hitlDetails/{ti_id}",
@@ -238,13 +237,13 @@ def test_update_hitl_detail_without_ti(client: Client) -> None:
238237

239238

240239
@pytest.mark.usefixtures("sample_hitl_detail")
241-
def test_get_hitl_detail(client: Client, sample_ti: TaskInstance) -> None:
240+
def test_get_hitl_detail(client: TestClient, sample_ti: TaskInstance) -> None:
242241
response = client.get(f"/execution/hitlDetails/{sample_ti.id}")
243242
assert response.status_code == 200
244243
assert response.json() == expected_empty_hitl_detail_response_part
245244

246245

247-
def test_get_hitl_detail_without_ti(client: Client) -> None:
246+
def test_get_hitl_detail_without_ti(client: TestClient) -> None:
248247
response = client.get(f"/execution/hitlDetails/{uuid7()}")
249248
assert response.status_code == 404
250249
assert response.json() == {

‎uv.lock‎

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)