My modest goal: Correctly annotate a function argument as an object supporting read() which returns bytes.
- If I use
typing.BinaryIO, I cannot accept aserial.Serialobject (which derives fromio.RawIOBase). - If I use
io.RawIOBase, I cannot accept aBinaryIOobject (e.g.sys.stdin.buffer)
import io
import serial
from typing import BinaryIO, Optional
import sys
def foo_rawiobase(infile: io.RawIOBase) -> Optional[bytes]:
return infile.read(42)
def foo_binaryio(infile: BinaryIO) -> bytes:
return infile.read(42)
def foo_serial(dev: serial.Serial) -> None:
# error: Argument 1 to "foo_binaryio" has incompatible type "Serial"; expected "BinaryIO" [arg-type]
foo_binaryio(dev)
# ok
foo_rawiobase(dev)
def foo_file(f: BinaryIO) -> None:
# ok
foo_binaryio(f)
# error: Argument 1 to "foo_rawiobase" has incompatible type "BinaryIO"; expected "RawIOBase" [arg-type]
foo_rawiobase(f)
def foo_stdin() -> None:
foo_file(sys.stdin.buffer)
typeshed definitions:
How do I properly handle both cases?