python / python/typeshed

What is the correct way to subclass asyncio.StreamReader?

Đang mở
#12,423 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

topic: asyncio
Ngôn ngữ chính
Python
Star
5.1k
Fork
2.1k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
82

Mô tả

#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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Kiểm tra stub của asyncio.StreamReader.readuntil và triển khai stdlib của Python 3.12, bắt đầu với chữ ký đã được thay đổi bởi #11755. Tái hiện lỗi override bằng StreamReaderWrapper được cung cấp và mypy, sau đó thiết lập một annotation công khai cho phép trường hợp sử dụng subclassing được tài liệu hóa mà không làm lộ một kiểu nội bộ không phù hợp.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
developer-experience
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.