Skip to content

Bad typing for requests.cookies.RequestsCookieJar #15457

@shelvacu

Description

@shelvacu

RequestsCookieJar is typed as a subtype of both http.CookieJar and MutableMapping[str, str]:

class RequestsCookieJar(CookieJar, MutableMapping[str, str]): # type: ignore[misc] # conflicting __iter__ in the base classes

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions