feat: make type checkers reject unknown client options - #150
Merged
Merged
Conversation
The option TypedDicts behind **httpx2_options are now closed (PEP 728) via typing_extensions, so ty reports a misspelled or refused option such as verfy= or cert= at type-check time; pyright and mypy already did.
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.
Problem
Since 0.18.0,
AsyncClient/Clienttake their httpx2 options as**httpx2_options: Unpack[_AsyncClientOptions]. pyright and mypy reject an unknown key there, such asverfy=or the refusedcert=, but ty does not. httpware, jwks-client and semvertag all type-check with ty, so in these repos those mistakes only surface at runtime, when the constructor's key check raisesTypeError.ty's behaviour is intentional. python/typing#2272 lets checkers read
**kwargs: Unpack[TD]as also accepting extra keys, and a ty maintainer confirmed on astral-sh/ty#154 that it won't change. The suggested fix is a closed TypedDict (PEP 728). That's built intotypingfrom Python 3.15 and available today throughtyping_extensions.Change
_AsyncClientOptionsand_ClientOptionsare declared withclosed=True. The shared_ClientOptionsBasestays open, because PEP 728 doesn't let a subclass of a closed TypedDict add keys. All three are nowtyping_extensions.TypedDict.typing-extensions>=4.14.1. Before this, httpx2 was the only one, and httpx2 pulls intyping_extensionsonly below 3.13.Why the floor is 4.14.1
typing_extensions4.12.2 through 4.13.2 return an empty__optional_keys__for a closed TypedDict. The constructor's key check reads that attribute, so it would reject every option,verifyincluded. 4.14.0 is correct on 3.11, 3.12, 3.13, 3.14 and 3.14t.typing-extensions>=4.14.1. With 4.14.0, the floors job on 3.14 can't resolve.Tests
test_type_checkers_reject_unsupported_{async,sync}_optionsmake literal calls such asAsyncClient(verfy=True)andAsyncClient(cert=...), each with# ty: ignore[unknown-argument]. Before the change, ty reports four unused-ignore warnings andty checkexits 1. After it, the suppressions are used. At runtime, each call still has to raiseTypeError.typing-extensions==4.13.2fails 78 of the option tests.uv pip compile --resolution lowest-direct) on 3.11, 3.12, 3.13, 3.14 and 3.14t, and all 957 tests pass on each.