Skip to content

Commit 602f660

Browse files
committed
Widened type annotations to accept IO[bytes] in file streams
Fixes #1078. Closes #1085. Closes #1095. Closes #1096.
1 parent b5dcd45 commit 602f660

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

docs/versionhistory.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
2020
- Fixed the pytest plugin not running tests that had the ``anyio`` marker added
2121
programmatically via ``pytest_collection_modifyitems``
2222
(`#422 <https://github.com/agronholm/anyio/issues/422>`_; PR by @chbndrhnns)
23+
- Widened the type annotations of file I/O streams to accept ``IO[bytes]``
24+
instead of just ``BinaryIO``
25+
(`#1078 <https://github.com/agronholm/anyio/issues/1078>`_)
2326

2427
**4.12.1**
2528

src/anyio/streams/file.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from io import SEEK_SET, UnsupportedOperation
1111
from os import PathLike
1212
from pathlib import Path
13-
from typing import Any, BinaryIO, cast
13+
from typing import IO, Any
1414

1515
from .. import (
1616
BrokenResourceError,
@@ -25,15 +25,15 @@
2525

2626
class FileStreamAttribute(TypedAttributeSet):
2727
#: the open file descriptor
28-
file: BinaryIO = typed_attribute()
28+
file: IO[bytes] = typed_attribute()
2929
#: the path of the file on the file system, if available (file must be a real file)
3030
path: Path = typed_attribute()
3131
#: the file number, if available (file must be a real file or a TTY)
3232
fileno: int = typed_attribute()
3333

3434

3535
class _BaseFileStream:
36-
def __init__(self, file: BinaryIO):
36+
def __init__(self, file: IO[bytes]):
3737
self._file = file
3838

3939
async def aclose(self) -> None:
@@ -76,7 +76,7 @@ async def from_path(cls, path: str | PathLike[str]) -> FileReadStream:
7676
7777
"""
7878
file = await to_thread.run_sync(Path(path).open, "rb")
79-
return cls(cast(BinaryIO, file))
79+
return cls(file)
8080

8181
async def receive(self, max_bytes: int = 65536) -> bytes:
8282
try:
@@ -143,7 +143,7 @@ async def from_path(
143143
"""
144144
mode = "ab" if append else "wb"
145145
file = await to_thread.run_sync(Path(path).open, mode)
146-
return cls(cast(BinaryIO, file))
146+
return cls(file)
147147

148148
async def send(self, item: bytes) -> None:
149149
try:

0 commit comments

Comments
 (0)