Bad typing for requests.cookies.RequestsCookieJar
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 5.1k
- Forks
- 2.1k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 82
Beschreibung
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"
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne in stubs/requests/requests/cookies.pyi bei der Deklaration von RequestsCookieJar um Zeile 38 und vergleiche dann dessen iter-Verhalten mit der Annotation MutableMapping[str, str]. Führe die bereitgestellte pyright- und mypy-Reproduktion aus, um zu überprüfen, dass die Iteration typisiert ist und sich wie Cookie-Werte verhält. Fertig ist die Aufgabe, wenn der Stub den Aufrufern keinen falschen Typ für die iterierbaren Elemente mehr liefert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- tooling
- Issue-Typ
- Bug
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Aktivitätsstatus
- Ruhig
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 72/100