Skip to content

Inconsistent type hints lead to bad type checks #1078

Closed

Description

@AstraLuma

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

AnyIO version

4.12.1

Python version

3.14.2

What happened?

import typing as T
import anyio


async def hash_stream(algo: str, stream: anyio.abc.ByteReceiveStream, **opts) -> bytes:
    ...


async def _do_hash(file: anyio.AsyncFile[bytes], algo: str, meta: object):
    # Don't context-manage the stream, it's the caller's job to close everything
    T.reveal_type(file)
    T.reveal_type(file.wrapped)
    stream = anyio.streams.file.FileReadStream(file.wrapped)
    digest = await hash_stream(algo, stream)
    setattr(meta, f"hash_{algo}", digest)

mypy output:

src/swiss_army_upload/junk_drawer/rsync.py:63: note: Revealed type is "anyio._core._fileio.AsyncFile[builtins.bytes]"
src/swiss_army_upload/junk_drawer/rsync.py:64: note: Revealed type is "typing.IO[builtins.bytes]"
src/swiss_army_upload/junk_drawer/rsync.py:65: error: Argument 1 to "FileReadStream" has incompatible type "IO[bytes]"; expected "BinaryIO"  [arg-type]

This happens because BinaryIO is a subclass of IO[bytes], not an equivalent alias. (https://docs.python.org/3/library/typing.html#abcs-and-protocols-for-working-with-i-o) So while you can use a BinaryIO as an IO[bytes], you cannot use an IO[bytes] as a BinaryIO (in the eyes of mypy).

How can we reproduce the bug?

Type check the above code

Activity

  1. added 2 commits that reference this issue on Mar 22, 2026
    31d9ef1
    f8c375e
  2. added a commit that references this issue on Mar 22, 2026
    602f660
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions