microsoft / microsoft/yardl

Python type hint for Reader/Writer should probably be IO[bytes]

Open
#241 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
35
Forks
14
Avg merge
5d 17h
Merged PRs (30d)
1

Description

Currently, the Python BinaryProtocolReader accepts a binary input stream or a string filename:

class BinaryProtocolReader(ABC):
    def __init__(
        self,
        stream: Union[BufferedReader, BytesIO, BinaryIO, str],
        expected_schema: Optional[str],
    ) -> None:
    ....

However, Pyright 1.1.405 (2025-09-03) complains when the stream is of type IO[bytes], e.g.:

# Run mrd_phantom as subprocess and capture stdout
proc = subprocess.Popen(["mrd_phantom"], stdout=subprocess.PIPE)

if proc.stdout is None:
    raise RuntimeError("Failed to capture stdout")

with mrd.BinaryMrdReader(proc.stdout) as reader:    # <-- Pyright error
    ...

Pyright complains that

Argument of type "IO[bytes] | None" cannot be assigned to parameter "stream" of type "BufferedReader[_BufferedReaderStream] | BytesIO | BinaryIO | str" in function "__init__"
  Type "IO[bytes] | None" is not assignable to type "BufferedReader[_BufferedReaderStream] | BytesIO | BinaryIO | str"
    Type "IO[bytes]" is not assignable to type "BufferedReader[_BufferedReaderStream] | BytesIO | BinaryIO | str"
      "IO[bytes]" is not assignable to "BufferedReader[_BufferedReaderStream]"
      "IO[bytes]" is not assignable to "BytesIO"
      "IO[bytes]" is not assignable to "BinaryIO"
      "IO[bytes]" is not assignable to "str"Pylance[reportArgumentType](https://github.com/microsoft/pylance-release/blob/main/docs/diagnostics/reportArgumentType.md)

As I understand it, IO[bytes] is the generic ABC for all binary IO streams (source):

class typing.IO
class typing.TextIO
class typing.BinaryIO
Generic type IO[AnyStr] and its subclasses TextIO(IO[str]) and BinaryIO(IO[bytes]) represent the types of I/O streams such as returned by [open()](https://docs.python.org/3.8/library/functions.html#open).

This change appears to be recommended by type checkers like Ruff: https://github.com/astral-sh/ruff/issues/15532

Similar changes may be needed for BinaryProtocolWriter, NDJsonProtocolReader, and NDJsonProtocolReader and they can also be tested using subprocess.PIPE.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the definitions of BinaryProtocolReader, BinaryProtocolWriter, and NDJsonProtocolReader mentioned in the issue, then inspect how their stream parameters are annotated. Reproduce the subprocess.PIPE example with Pyright and update the relevant annotations so accepted binary streams type-check; verify the reader and writer cases, including the similar NDJSON case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.