Skip to content

Fix JsonType to accept Mapping[str, Any] instead of recursive Mapping[str, "JsonType"]#7449

Open
driphtyio wants to merge 1 commit into
psf:mainfrom
driphtyio:main
Open

Fix JsonType to accept Mapping[str, Any] instead of recursive Mapping[str, "JsonType"]#7449
driphtyio wants to merge 1 commit into
psf:mainfrom
driphtyio:main

Conversation

@driphtyio
Copy link
Copy Markdown

Fixes #7443

Problem

The recursive JsonType definition used Mapping[str, "JsonType"], which causes mypy to reject valid dict subtypes like dict[str, object] or dict[str, Collection[str]] due to mypy's invariance handling of recursive Mapping types.

Reproduction

def fn(d: dict[str, str]) -> None:
    j = {"foo": d, "bar": "hi"}
    requests.post("https://example.com", json=j)  # mypy error

Fix

Replaced the recursive Mapping[str, "JsonType"] with Mapping[str, Any]. This matches how Python's json.dumps actually accepts any serializable value. Any is the standard pattern used by CPython's json module stubs for the same purpose.

Testing

  • 407 relevant tests pass (all pre-existing failures are network-dependent)
  • No new mypy errors introduced
  • pyright and ty continue to work correctly

The recursive JsonType definition used Mapping[str, "JsonType"], which
caused mypy to reject valid dict subtypes like dict[str, object] or
dict[str, Collection[str]] due to mypy's invariance handling of
recursive Mapping types.

The fix replaces the recursive Mapping value type with Any, matching
how Python's json.dumps actually accepts any serializable value.

Fixes psf#7443
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.

mypy warns about invalid types for json argument

1 participant