python / python/typeshed

What is the correct way to subclass asyncio.StreamReader?

Aperta
#12,423 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

#11755 changed the signature of asyncio.StreamReader.readuntil to make the separator argument take an internal type _ReaduntilBuffer that just has ... for its definition. This is confusing because inside the python 3.12 stdlib code this argument is already typed as bytes, which is the obvious choice. When I try to write my own subclass that takes bytes for the argument mypy complains I'm violating Liskov. Since this class is specifically designed as a base class that users would want to subclass themselves, it seems like there should be a way to do this that doesn't use internal types, no?

import asyncio
import typing

class StreamReaderWrapper(asyncio.StreamReader):
    def __init__(self):
        self._reader: asyncio.StreamReader | None = None

    @typing.override
    async def read(self, n: int = -1) -> bytes:
        assert self._reader is not None
        return await self._reader.read(n)

    @typing.override
    async def readline(self) -> bytes:
        assert self._reader is not None
        return await self._reader.readline()

    @typing.override
    async def readexactly(self, n: int) -> bytes:
        assert self._reader is not None
        return await self._reader.readexactly(n)

    @typing.override
    async def readuntil(self, separator: bytes = b'\n') -> bytes:
        assert self._reader is not None
        return await self._reader.readuntil(separator)
foo.py:4: error: Argument 1 of "readuntil" is incompatible with supertype "StreamReader"; supertype defines the argument type as "_ReaduntilBuffer"  [override]
foo.py:4: note: This violates the Liskov substitution principle
foo.py:4: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides

Note that using bytes | bytearray | memoryview as the type instead (taken from #11755) doesn't work either, same error

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

Esamina lo stub di asyncio.StreamReader.readuntil e l’implementazione della stdlib di Python 3.12, iniziando dalla firma modificata da #11755. Riproduci l’errore di override con lo StreamReaderWrapper fornito e mypy, quindi definisci un’annotazione pubblica che consenta il caso d’uso documentato del subclassing senza esporre un tipo interno inadatto.

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

Valutazione

Stack tecnologico
python
Ambito
developer-experience
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 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.