Bad typing for requests.cookies.RequestsCookieJar
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.1k
- Forks
- 2.1k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 82
Description
RequestsCookieJar is typed as a subtype of both http.CookieJar and MutableMapping[str, str]:
including a comment ignoring the type error. This is simply wrong; RequestsCookieJar is not a MutableMapping because the __iter__ fn differs.
This causes real problems:
import requests.cookies
import typing
cookiejar = requests.cookies.RequestsCookieJar()
cookiejar["a"] = "b"
print("outside func")
for cookie in cookiejar:
typing.reveal_type(cookie) # type is Cookie at typecheck and runtime
print(f"{type(cookie)=}")
def in_func(not_a_cookiejar:typing.Iterable[str]):
print("in_func")
for maybe_cookie in not_a_cookiejar:
typing.reveal_type(maybe_cookie) # type is str at typecheck
print(f"{type(maybe_cookie)=}") # type is Cookie at runtime
in_func(cookiejar)
pyright 1.1.407 says:
information: Type of "cookie" is "Cookie"
information: Type of "maybe_cookie" is "str"
mypy 1.19.1 says:
bad-types.py:9: note: Revealed type is "http.cookiejar.Cookie"
bad-types.py:15: note: Revealed type is "builtins.str"
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in stubs/requests/requests/cookies.pyi at the RequestsCookieJar declaration around line 38, then compare its iter behavior with the MutableMapping[str, str] annotation. Run the provided pyright and mypy reproduction to verify that iteration is typed and behaves as Cookie values. Done means the stub no longer gives callers an incorrect iterable element type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 72/100