python / python/typeshed

`urllib.request.Request.data` type erroring when using `decode`

Aperta
#11,470 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

When getting the data out of a urllib.request.Request object, mypy is erroring saying that decode is not supported even tho it is a bytes object (and does not error when at runtime).

$ cat t.py
from urllib.request import Request

req = Request("http://localhost:8080", data=b"req body")

if req.data:
    print(dir(req.data))
    print(type(req.data))
    print(req.data.decode())

$ mypy t.py
t.py:8: error: Item "Buffer" of "Buffer | SupportsRead[bytes] | Iterable[bytes]" has no attribute "decode"  [union-attr]
t.py:8: error: Item "SupportsRead[bytes]" of "Buffer | SupportsRead[bytes] | Iterable[bytes]" has no attribute "decode"  [union-attr]
t.py:8: error: Item "Iterable[bytes]" of "Buffer | SupportsRead[bytes] | Iterable[bytes]" has no attribute "decode"  [union-attr]
Found 3 errors in 1 file (checked 1 source file)

$ python t.py
[... 'decode', ...]
<class 'bytes'>
req body

$ python --version --version
Python 3.12.0 (main, Oct  2 2023, 20:56:14) [Clang 16.0.3 ]

$ mypy --version
mypy 1.8.0 (compiled: yes)

I believe PR #10225 had made this an error as it changed bytes to Buffer.

- ReadOnlyBuffer: TypeAlias = bytes
- ReadableBuffer: TypeAlias = ReadOnlyBuffer | WriteableBuffer  # stable
+ ReadOnlyBuffer: TypeAlias = Buffer  # stable
+ ReadableBuffer: TypeAlias = Buffer  # stable

Before: https://github.com/python/typeshed/blame/56aeeb677f27c6771e683314621521aff5c97cd1/stdlib/_typeshed/__init__.pyi#L230-L241
After: https://github.com/python/typeshed/blame/1e7e174b4fc4da6a66622168f8012c7ee0499392/stdlib/_typeshed/__init__.pyi#L263-L267


Deferred until #11422.

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 con l’annotazione dei dati di urllib.request.Request e gli alias di Buffer in stdlib/_typeshed/init.pyi, confrontando le modifiche precedenti e successive collegate nell’issue. Riproduci l’esempio mypy fornito e consulta PR #10225 e l’issue differita #11422 per il contesto. Il lavoro è completo quando l’esempio accetta decode senza perdere i tipi di dati accettati previsti.

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

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.