Skip to content

Commit 966da64

Browse files
lesnik512claude
andcommitted
style: use http.HTTPStatus directly instead of inventing constants
In client.py: drop _CLIENT_ERROR_LOW/_SERVER_ERROR_LOW/_SERVER_ERROR_HIGH in favor of HTTPStatus.BAD_REQUEST/INTERNAL_SERVER_ERROR direct comparison. 600 is the only literal that stays (no stdlib equivalent for the synthetic 5xx upper bound); single noqa with justification, same pattern as the 0.1 transport. In tests/test_middleware.py: replace _STATUS_OK/_STATUS_UPGRADED/ _STATUS_SERVICE_UNAVAILABLE with HTTPStatus.OK / HTTPStatus.IM_USED / HTTPStatus.SERVICE_UNAVAILABLE. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent ab262b4 commit 966da64

2 files changed

Lines changed: 10 additions & 18 deletions

File tree

‎src/httpware/client.py‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828
f"{_FORWARDED_KWARG_NAMES}; configure the httpx2.AsyncClient you pass instead."
2929
)
3030

31-
_CLIENT_ERROR_LOW = HTTPStatus.BAD_REQUEST.value # 400
32-
_SERVER_ERROR_LOW = HTTPStatus.INTERNAL_SERVER_ERROR.value # 500
33-
_SERVER_ERROR_HIGH = 600 # synthetic upper bound; no standard HTTP status reaches this
34-
3531

3632
class AsyncClient:
3733
"""Async HTTP client: thin wrapper around httpx2 with typed decoding and middleware."""
@@ -107,10 +103,10 @@ async def _terminal(self, request: httpx2.Request) -> httpx2.Response:
107103
raise TransportError(str(exc)) from exc
108104
raise
109105
status = response.status_code
110-
if _CLIENT_ERROR_LOW <= status < _SERVER_ERROR_HIGH:
106+
if HTTPStatus.BAD_REQUEST <= status < 600: # noqa: PLR2004 — 600 is the synthetic upper bound for 5xx
111107
exc_class = STATUS_TO_EXCEPTION.get(
112108
status,
113-
ClientStatusError if status < _SERVER_ERROR_LOW else ServerStatusError,
109+
ClientStatusError if status < HTTPStatus.INTERNAL_SERVER_ERROR else ServerStatusError,
114110
)
115111
raise exc_class(response)
116112
return response

‎tests/test_middleware.py‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for the Middleware protocol, Next type, chain composition, and decorators."""
22

33
import asyncio
4+
from http import HTTPStatus
45

56
import httpx2
67
import pytest
@@ -19,12 +20,7 @@ def _make_request(url: str = "https://example.test/x") -> httpx2.Request:
1920
return httpx2.Request("GET", url)
2021

2122

22-
_STATUS_OK = 200
23-
_STATUS_UPGRADED = 299
24-
_STATUS_SERVICE_UNAVAILABLE = 503
25-
26-
27-
def _make_response(status: int = _STATUS_OK, *, request: httpx2.Request | None = None) -> httpx2.Response:
23+
def _make_response(status: int = HTTPStatus.OK, *, request: httpx2.Request | None = None) -> httpx2.Response:
2824
if request is None:
2925
request = _make_request()
3026
return httpx2.Response(status, request=request)
@@ -48,7 +44,7 @@ async def terminal(request: httpx2.Request) -> httpx2.Response:
4844
dispatch = compose((), terminal)
4945
request = _make_request()
5046
response = await dispatch(request)
51-
assert response.status_code == _STATUS_OK
47+
assert response.status_code == HTTPStatus.OK
5248
assert seen == [request]
5349

5450

@@ -93,21 +89,21 @@ async def terminal(request: httpx2.Request) -> httpx2.Response:
9389
async def test_after_response_decorator_transforms_response() -> None:
9490
@after_response
9591
async def upgrade_status(request: httpx2.Request, response: httpx2.Response) -> httpx2.Response:
96-
return httpx2.Response(299, request=request, headers=response.headers, content=response.content)
92+
return httpx2.Response(HTTPStatus.IM_USED, request=request, headers=response.headers, content=response.content)
9793

9894
async def terminal(request: httpx2.Request) -> httpx2.Response:
99-
return _make_response(200, request=request)
95+
return _make_response(HTTPStatus.OK, request=request)
10096

10197
dispatch = compose((upgrade_status,), terminal)
10298
response = await dispatch(_make_request())
103-
assert response.status_code == _STATUS_UPGRADED
99+
assert response.status_code == HTTPStatus.IM_USED
104100

105101

106102
async def test_on_error_decorator_can_translate_exception() -> None:
107103
@on_error
108104
async def swallow(request: httpx2.Request, exc: Exception) -> httpx2.Response | None:
109105
if isinstance(exc, RuntimeError) and str(exc) == "boom":
110-
return _make_response(503, request=request)
106+
return _make_response(HTTPStatus.SERVICE_UNAVAILABLE, request=request)
111107
return None
112108

113109
async def terminal(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
@@ -116,7 +112,7 @@ async def terminal(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
116112

117113
dispatch = compose((swallow,), terminal)
118114
response = await dispatch(_make_request())
119-
assert response.status_code == _STATUS_SERVICE_UNAVAILABLE
115+
assert response.status_code == HTTPStatus.SERVICE_UNAVAILABLE
120116

121117

122118
async def test_on_error_returns_none_reraises() -> None:

0 commit comments

Comments
 (0)