psf / psf/requests

mypy warns about invalid types for json argument

Open
#7,443 4 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
54.3k
Forks
10.4k
Avg merge
16h 43m
Merged PRs (30d)
3

Description

I have warnings about invalid types for the json argument, that didn't occur when using types-requests.

The changes in v2.34.1 changed the errors, but did not fix them.

typecheck.py:6: error: Argument "json" to "post" has incompatible type "dict[str, Collection[str]]"; expected "JsonType"  [arg-type]
typecheck.py:9: error: Argument "json" to "post" has incompatible type "dict[str, object]"; expected "JsonType"  [arg-type]

Reproduction Steps

import requests

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

    k = {"foo": d, "bool": True}
    requests.post("https://example.com", json=k)

It seems ok if a literal is used without the intermediate variable:

    requests.post("https://example.com", json={"foo": d, "bar": "hi"})
    requests.post("https://example.com", json={"foo": d, "bool": True})

System Information

{
  "chardet": {
    "version": null
  },
  "charset_normalizer": {
    "version": "3.4.7"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "3.15"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.13.5"
  },
  "platform": {
    "release": "6.12.74+deb13+1-amd64",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.34.1"
  },
  "system_ssl": {
    "version": "30500050"
  },
  "urllib3": {
    "version": "2.7.0"
  },
  "using_charset_normalizer": true,
  "using_pyopenssl": false
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running mypy against the provided typecheck.py reproduction, focusing on the json argument to requests.post and the difference between intermediate dictionaries and literals. Trace the typing used for that entry point; done means both intermediate-variable examples type-check without invalid-type warnings while the existing literal cases continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.