Repository navigation
fix: make generated clients type-check with httpx and mypy - #476
Merged
Merged
Conversation
Fixes #463 - HttpClient protocols now take request arguments as keyword-only and drop **kwargs, so httpx.Client, httpx.AsyncClient and requests.Session satisfy them structurally. Base clients only call post() with keywords. - Restore mypy-compatible `# type: ignore[...]` comments on the optional websockets and opentelemetry fallbacks (ty honours them as well). - Add a ty-checked regression test assigning httpx clients to every shipped HttpClient protocol.
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or 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.
Fixes #463
Problem
Since 0.19.0 a generated client fails type checking when users pass their own HTTP client, and also on its own optional-import fallbacks:
HttpClient.post()declaredjson/data/files/headersas positional-or-keyword and ended with**kwargs: Any.httpx.Client.post/httpx.AsyncClient.posttake everything afterurlas keyword-only and have no**kwargs, so mypy and pyright rejecthttp_client=httpx.AsyncClient(...). The same applied torequests.Session, which the docs list as a compatible client.websocketsandopentelemetryfallbacks (ClientConnection = Any,Context = Any, …) only carried# ty: ignore[...], which mypy doesn't understand (fix(types): align ty suppressions for GraphQL and optional imports #427 replaced the previous# type: ignore[...]).Changes
HttpClientprotocols take request arguments as keyword-only and no longer require**kwargs. Base clients only callpost()with keywords, so runtime behaviour is unchanged, and existing custom clients written against the old protocol still satisfy the new one.# type: ignore[misc,assignment,unused-ignore]on the fallback assignments. ty honours it too.docs/02-guides/03-using-generated-client.md.tests/client_generators/dependencies/test_http_client_protocol.py: annotated assignments ofhttpx.Client/httpx.AsyncClientto every shipped protocol, which ty checks duringhatch run lint. Verified that it fails against the old protocols.The remote-schema
HttpClientinschema.pyis out of scope.Verification
Generated async, sync, async+otel and sync+otel clients from a minimal schema and checked them with mypy 2.4.1 and pyright 1.1.414:
mypy --check-untyped-defs(as in #463)httpx.Client,httpx.AsyncClient,requests.Sessionpassed ashttp_clientThe remaining
mypy --strictand pyright findings in the async websockets block (untypedws_connectfallback,Data/Originused as types) were already present in 0.18.0 and are left for a follow-up.hatch run lintpasses, andhatch test -a: 1052 passed on Python 3.10–3.14.