[Python] How do I add a type hint for a WriteableBuffer parameter?

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to add a parameter type to the readinto() method declared in a custom class that derives from RawIOBase, like this:

    from io import RawIOBase

    class Reader(RawIOBase):
    def readinto(self, buf: bytearray) -> int:
    pass # actual implementation omitted


    But pyright complains:

    io.py:6:9 - error: Method "readinto" overrides class "_RawIOBase" in an incompatible manner
    Parameter 2 type mismatch: base parameter is type "WriteableBuffer", override parameter is type "bytearray"
    "Buffer" is not assignable to "bytearray" (reportIncompatibleMethodOverride)
    1 error, 0 warnings, 0 informations



    How do I fix this? Note: I know I can remove the type hint entirely. I want to assign it the correct type instead.

    I'm using Python 3.13.3 and pyright 1.1.400.

    Continue reading...

Compartilhe esta Página