Bad typing for requests.cookies.RequestsCookieJar
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
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.
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"
- Dominant language
- Python
- Stars
- 5.1k
- Forks
- 2.1k
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 73
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.
More from python/typeshed
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
project: infrastructure
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
stubs: improvement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100