Skip to content

widen Path.write_bytes typing #1135

Closed

Description

@vfazio

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

Python version

3.12

What happened?

With mypy v2, we got flagged for passing a bytesarray into Path.write_bytes

It's currently typed as:

async def write_bytes(self, data: bytes) -> int:

pathlib.Path types this as:

def write_bytes(self, data: ReadableBuffer) -> int: ...

I guess all it needs to be is a buffer type per https://peps.python.org/pep-0688/

I haven't checked for other areas that may be accidentally constraining types now that mypy doesn't promote these types by default.

https://mypy.readthedocs.io/en/latest/changelog.html#enable-strict-bytes-by-default
https://mypy.readthedocs.io/en/latest/command_line.html#cmdoption-mypy-no-strict-bytes

How can we reproduce the bug?

import anyio

async def fn() -> None:
    x = anyio.Path("test.bytes")
    view = memoryview(b"")
    await x.write_bytes(view)

anyio.run(fn)

Activity

  1. added
    bug
    on May 7, 2026
  2. added a commit that references this issue on May 7, 2026
    b979108
  3. vfazio commented on May 7, 2026

    @vfazio
    Author

    This is fun, all I have to do now is create an issue and let the Ai bots spin up PRs to fix them

  4. agronholm commented on May 7, 2026

    @agronholm
    Owner

    Well, I don't care who writes the fixes so long as they play by the rules and respond to reviews.

  5. vfazio commented on May 7, 2026

    @vfazio
    Author

    I'll try to take some time this weekend to see if there are other places that need to be widened unless that analysis was already done in the PR.

  6. vfazio commented on May 7, 2026

    @vfazio
    Author

    A quick search shows that ByteSendStream and its subclasses are probably affected due to send as well as maybe UDPSocket.sendto. Things may be a bit hairier with generics like UnreliableObjectStream[bytes] or the generics wrapping the packet types.

    I think Path.write_bytes may have been the easy one :-/

    It looks like the underlying trio and asyncio stubs already use some form of union of bytes | bytearray | memoryview or some variant of Buffer in these function calls.

    Not sure how painful it would be to define a typevar that unions these byte like objects or if switching to Buffer is the right move (i'm still pretty type dumb).

    Also not sure if this is worth chasing and ripping the bandaid off or doing piecemeal as issues come up. Users do have the option of disabling the mypy check or pinning to a lower mypy version but they may be loathe to do that since it could be masking issues .

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

    bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions