Skip to content

Update JsonType containers to read-based collections#7436

Merged
nateprewitt merged 2 commits into
mainfrom
JsonType
May 13, 2026
Merged

Update JsonType containers to read-based collections#7436
nateprewitt merged 2 commits into
mainfrom
JsonType

Conversation

@nateprewitt
Copy link
Copy Markdown
Member

@nateprewitt nateprewitt commented May 12, 2026

The current definition for JsonType is constrained to dict for map-like objects but that introduces issues when input is explicitly typed.

The type checker is able to infer typing correctly with:

payload = {"test": "test"}
r = requests.post(url, json=payload)

but not

payload: dict[str, str] = {"test": "test"}
r = requests.post(url, json=payload)

This is due to the varied Value typing on JsonType and the mutability of dict. Changing it to Mapping which only exposes read interfacea allows us to check covariant value types. This works because the json input is never touched. It's taken from input and passed directly to json.dumps resulting in bytes. There's no access point in the pipeline to modify it later.

We're going to start with this expansion of the type to see if it works broadly. The goal is to avoid Any for JsonType if at all possible.

Comment thread src/requests/_types.py Outdated
@nateprewitt nateprewitt marked this pull request as ready for review May 12, 2026 18:01
@nateprewitt nateprewitt changed the title Move JsonType from dict to Mapping Update JsonType containers to read-based collections May 12, 2026
@nateprewitt nateprewitt merged commit 5691f59 into main May 13, 2026
61 checks passed
@nateprewitt nateprewitt deleted the JsonType branch May 13, 2026 14:43
@nateprewitt nateprewitt mentioned this pull request May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inline annotations in 2.34.0 cause mypy error for Request json payloads

3 participants