python / python/typeshed

Bad typing for requests.cookies.RequestsCookieJar

Open Beginner friendly
#15,457 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stubs: false positive topic: collections
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]:

https://github.com/python/typeshed/blob/6ee36bacecd24fca98d080a6965821e7e2d9e590/stubs/requests/requests/cookies.pyi#L38

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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.