Skip to content

Commit a4fc7d3

Browse files
committed
fix: use BinaryIO instead of IO[bytes] for typing
1 parent 958a69f commit a4fc7d3

File tree

7 files changed

+24
-16
lines changed

7 files changed

+24
-16
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/), and this project
66
adheres to [Semantic Versioning](https://semver.org/).
77

8+
## [Unreleased]
9+
10+
[unreleased]: https://github.com/rogdham/bigxml/compare/v0.3.1...HEAD
11+
12+
### :bug: Fixes
13+
14+
- Typing: use `BinaryIO` instead of `IO[bytes]`
15+
816
## [0.3.1] - 2021-12-26
917

1018
[0.3.1]: https://github.com/rogdham/python-xz/releases/tag/v0.3.1

src/xz/file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from io import SEEK_CUR, SEEK_END
22
import os
3-
from typing import IO, List, Optional
3+
from typing import BinaryIO, List, Optional, cast
44
import warnings
55

66
from xz.common import DEFAULT_CHECK, XZError
@@ -63,7 +63,7 @@ def __init__(
6363
# get fileobj
6464
if isinstance(filename, (str, bytes, os.PathLike)):
6565
# pylint: disable=consider-using-with, unspecified-encoding
66-
self.fileobj: IO[bytes] = open(filename, self.mode + "b")
66+
self.fileobj = cast(BinaryIO, open(filename, self.mode + "b"))
6767
self._close_fileobj = True
6868
elif hasattr(filename, "read"): # weak check but better than nothing
6969
self.fileobj = filename

src/xz/io.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
IOBase,
77
UnsupportedOperation,
88
)
9-
from typing import IO, Generic, Optional, TypeVar, Union, cast
9+
from typing import BinaryIO, Generic, Optional, TypeVar, Union, cast
1010

1111
from xz.utils import FloorDict
1212

1313
#
1414
# Typing note
1515
#
1616
# The consensus seems to favour IO instead of IOBase for typing.
17-
# However we cannot subclass IO[bytes] in IOAbstract as it conflicts with IOBase.
17+
# However we cannot subclass BinaryIO in IOAbstract as it conflicts with IOBase.
1818
#
19-
# As a result, some casting or unions between IOAbstract and IO[bytes] may be required internally.
19+
# As a result, some casting or unions between the two types may be required internally.
2020
#
2121

2222

@@ -41,7 +41,7 @@ def _check_not_closed(self) -> None:
4141

4242
def fileno(self) -> int:
4343
try:
44-
return cast(IO[bytes], self.fileobj).fileno() # type: ignore[attr-defined]
44+
return cast(BinaryIO, self.fileobj).fileno() # type: ignore[attr-defined]
4545
except AttributeError:
4646
raise UnsupportedOperation("fileno") # pylint: disable=raise-missing-from
4747

@@ -245,7 +245,7 @@ def _read(self, size: int) -> bytes:
245245
class IOProxy(IOAbstract):
246246
def __init__(
247247
self,
248-
fileobj: Union[IO[bytes], IOAbstract], # see typing note on top of this file
248+
fileobj: Union[BinaryIO, IOBase], # see typing note on top of this file
249249
start: int,
250250
end: int,
251251
) -> None:

src/xz/open.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from functools import wraps
22
from io import TextIOWrapper
3-
from typing import IO, List, Optional, Union, cast, overload
3+
from typing import BinaryIO, List, Optional, Union, cast, overload
44

55
from xz.file import XZFile
66
from xz.typing import (
@@ -34,7 +34,7 @@ def __init__(
3434
filters=filters,
3535
)
3636
super().__init__(
37-
cast(IO[bytes], self.xz_file),
37+
cast(BinaryIO, self.xz_file),
3838
encoding,
3939
errors,
4040
newline,

src/xz/stream.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from io import SEEK_CUR
2-
from typing import IO, List
2+
from typing import BinaryIO, List
33

44
from xz.block import XZBlock
55
from xz.common import (
@@ -44,7 +44,7 @@ def _fileobj_blocks_end_pos(self) -> int:
4444
)
4545

4646
@classmethod
47-
def parse(cls, fileobj: IO[bytes]) -> "XZStream":
47+
def parse(cls, fileobj: BinaryIO) -> "XZStream":
4848
"""Parse one XZ stream from a fileobj.
4949
5050
fileobj position should be right at the end of the stream when calling

src/xz/typing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from os import PathLike
22
import sys
3-
from typing import IO, Any, Mapping, Optional, Sequence, Union
3+
from typing import Any, BinaryIO, Mapping, Optional, Sequence, Union
44

55
if sys.version_info >= (3, 9): # pragma: no cover
6-
_LZMAFilenameType = Union[str, bytes, PathLike[str], PathLike[bytes], IO[bytes]]
6+
_LZMAFilenameType = Union[str, bytes, PathLike[str], PathLike[bytes], BinaryIO]
77
from typing import Literal
88
else: # pragma: no cover
9-
_LZMAFilenameType = Union[str, bytes, PathLike, IO[bytes]]
9+
_LZMAFilenameType = Union[str, bytes, PathLike, BinaryIO]
1010

1111
# ducktype Literal (NB we cannot use __class_getitem__ on Python 3.6)
1212
# we could require typing-extensions package but that's hardly an improvement

tests/unit/test_ioabstract.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from io import DEFAULT_BUFFER_SIZE, UnsupportedOperation
22
from pathlib import Path
3-
from typing import IO
3+
from typing import BinaryIO
44
from unittest.mock import Mock, call
55

66
import pytest
@@ -27,7 +27,7 @@ def test_fileno(tmp_path: Path) -> None:
2727
file_path.write_bytes(b"abcd")
2828

2929
class Impl(IOAbstract):
30-
def __init__(self, fileobj: IO[bytes]) -> None:
30+
def __init__(self, fileobj: BinaryIO) -> None:
3131
super().__init__(10)
3232
self.fileobj = fileobj
3333

0 commit comments

Comments
 (0)