|
1 | 1 | """Static-typing tests for AsyncClient and Client overloads. |
2 | 2 |
|
3 | | -These assert overload selection at runtime via isinstance checks. ty/mypy |
4 | | -catches the static-typing variant during `just lint`. |
| 3 | +Assertions come in two forms: |
| 4 | +- Runtime ``isinstance`` checks that verify the overload selected at call |
| 5 | + time (send / send_with_response with and without response_model). |
| 6 | +- Static ``typing.assert_type`` calls that let ty/mypy verify the inferred |
| 7 | + return type during ``just lint``. |
5 | 8 | """ |
6 | 9 |
|
| 10 | +import typing |
7 | 11 | from http import HTTPStatus |
8 | 12 |
|
9 | 13 | import httpx2 |
@@ -92,3 +96,32 @@ def test_sync_send_with_response_model_returns_typed() -> None: |
92 | 96 | client = Client(httpx2_client=httpx2.Client(transport=transport)) |
93 | 97 | result = client.send(httpx2.Request("GET", "https://example.test/x"), response_model=_User) |
94 | 98 | assert isinstance(result, _User) |
| 99 | + |
| 100 | + |
| 101 | +# --------------------------------------------------------------------------- |
| 102 | +# *_with_response static-type assertions — validates tuple[Response, T] destructuring |
| 103 | +# --------------------------------------------------------------------------- |
| 104 | + |
| 105 | + |
| 106 | +async def test_get_with_response_return_type() -> None: |
| 107 | + transport = httpx2.MockTransport( |
| 108 | + lambda req: httpx2.Response(HTTPStatus.OK, request=req, json={"id": 1, "name": "a"}) |
| 109 | + ) |
| 110 | + client = AsyncClient(httpx2_client=httpx2.AsyncClient(transport=transport)) |
| 111 | + response, user = await client.get_with_response("https://example.test/x", response_model=_User) |
| 112 | + typing.assert_type(response, httpx2.Response) |
| 113 | + typing.assert_type(user, _User) |
| 114 | + assert isinstance(response, httpx2.Response) |
| 115 | + assert isinstance(user, _User) |
| 116 | + |
| 117 | + |
| 118 | +def test_sync_get_with_response_return_type() -> None: |
| 119 | + transport = httpx2.MockTransport( |
| 120 | + lambda req: httpx2.Response(HTTPStatus.OK, request=req, json={"id": 1, "name": "a"}) |
| 121 | + ) |
| 122 | + client = Client(httpx2_client=httpx2.Client(transport=transport)) |
| 123 | + response, user = client.get_with_response("https://example.test/x", response_model=_User) |
| 124 | + typing.assert_type(response, httpx2.Response) |
| 125 | + typing.assert_type(user, _User) |
| 126 | + assert isinstance(response, httpx2.Response) |
| 127 | + assert isinstance(user, _User) |
0 commit comments