Skip to content

Make RequestsCookieJar.popitem() work#7513

Open
vineethsaivs wants to merge 1 commit into
psf:mainfrom
vineethsaivs:fix/cookiejar-popitem
Open

Make RequestsCookieJar.popitem() work#7513
vineethsaivs wants to merge 1 commit into
psf:mainfrom
vineethsaivs:fix/cookiejar-popitem

Conversation

@vineethsaivs

Copy link
Copy Markdown

Closes #6190

RequestsCookieJar subclasses both CookieJar and MutableMapping[str, str | None]. Its __iter__ is inherited from CookieJar and yields Cookie objects rather than names. The popitem() it inherited from MutableMapping does roughly:

key = next(iter(self))   # a Cookie object, not a name
value = self[key]        # __getitem__ looks up by name -> KeyError

so popitem() always raised KeyError, even on a non-empty jar:

>>> jar = requests.cookies.RequestsCookieJar()
>>> jar.set("a", "1"); jar.set("b", "2")
>>> jar.popitem()
KeyError: "name=Cookie(version=0, name='a', ...), domain=None, path=None"

This overrides popitem() to remove and return a (name, value) pair using the jar's existing dict-like view (iteritems()), and to raise KeyError only when the jar is empty, matching the documented behavior and the MutableMapping[str, str | None] contract.

Added a regression test (test_cookie_popitem) that pops both entries, checks each returned pair is a real (name, value) from the jar, and confirms a KeyError on the empty jar. It fails on main and passes with this change.

RequestsCookieJar subclasses both CookieJar and MutableMapping. Its __iter__
comes from CookieJar and yields Cookie objects rather than names, so the
popitem() inherited from MutableMapping did `self[next(iter(self))]`, i.e.
looked a cookie up by a Cookie object, and always raised KeyError even when the
jar was non-empty.

Override popitem() to remove and return a (name, value) pair using the jar's
dict-like view, and raise KeyError only when the jar is empty, matching the
documented behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

requests.cookies.RequestsCookieJar: popitem() does not work

1 participant