python / python/typeshed

Bad typing for requests.cookies.RequestsCookieJar

Aperta Adatta ai principianti
#15,457 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stubs: false positive topic: collections
Lingua principale
Python
Stelle
5.1k
Fork
2.1k
Merge medio
1g 19h
PR unite (30g)
82

Descrizione

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"

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in stubs/requests/requests/cookies.pyi, alla dichiarazione di RequestsCookieJar intorno alla riga 38, quindi confronta il suo comportamento di iter con l’annotazione MutableMapping[str, str]. Esegui la riproduzione fornita con pyright e mypy per verificare che l’iterazione sia tipizzata e si comporti come valori Cookie. Il lavoro è completato quando lo stub non fornisce più ai chiamanti un tipo errato per l’elemento iterabile.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Tranquilla
Chiarezza
Specificata chiaramente
Idoneità per principianti
72/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.