Skip to content

Commit b58b8f3

Browse files
tarcisioehauntsaninjaJelleZijlstra
authored
Make BytesIO inherit from BufferedIOBase. (#4082)
Co-authored-by: Shantanu <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent b2b397c commit b58b8f3

File tree

6 files changed

+15
-40
lines changed

6 files changed

+15
-40
lines changed

stdlib/3/gzip.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class GzipFile(_compression.BaseStream):
8282
def writable(self) -> bool: ...
8383
def seekable(self) -> bool: ...
8484
def seek(self, offset: int, whence: int = ...) -> int: ...
85-
def readline(self, size: int = ...) -> bytes: ...
85+
def readline(self, size: Optional[int] = ...) -> bytes: ...
8686

8787
class _GzipReader(_compression.DecompressReader):
8888
def __init__(self, fp: IO[bytes]) -> None: ...

stdlib/3/io.pyi

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class IOBase:
6464
def truncate(self, __size: Optional[int] = ...) -> int: ...
6565
def writable(self) -> bool: ...
6666
def writelines(self, __lines: Iterable[Union[bytes, bytearray]]) -> None: ...
67-
def readline(self, __size: int = ...) -> bytes: ...
67+
def readline(self, __size: Optional[int] = ...) -> bytes: ...
6868
def __del__(self) -> None: ...
6969
@property
7070
def closed(self) -> bool: ...
@@ -85,7 +85,6 @@ class BufferedIOBase(IOBase):
8585
def read(self, __size: Optional[int] = ...) -> bytes: ...
8686
def read1(self, __size: int = ...) -> bytes: ...
8787

88-
8988
class FileIO(RawIOBase):
9089
mode: str
9190
name: Union[int, str]
@@ -97,52 +96,27 @@ class FileIO(RawIOBase):
9796
opener: Optional[Callable[[Union[int, str], str], int]] = ...
9897
) -> None: ...
9998

100-
# TODO should extend from BufferedIOBase
101-
class BytesIO(BinaryIO):
99+
class BytesIO(BufferedIOBase, BinaryIO):
102100
def __init__(self, initial_bytes: bytes = ...) -> None: ...
103101
# BytesIO does not contain a "name" field. This workaround is necessary
104102
# to allow BytesIO sub-classes to add this field, as it is defined
105103
# as a read-only property on IO[].
106104
name: Any
105+
def __enter__(self: _T) -> _T: ...
107106
def getvalue(self) -> bytes: ...
108107
def getbuffer(self) -> memoryview: ...
109-
# copied from IOBase
110-
def __iter__(self) -> Iterator[bytes]: ...
111-
def __next__(self) -> bytes: ...
112-
def __enter__(self) -> BytesIO: ...
113-
def __exit__(self, t: Optional[Type[BaseException]] = ..., value: Optional[BaseException] = ...,
114-
traceback: Optional[TracebackType] = ...) -> Optional[bool]: ...
115-
def close(self) -> None: ...
116-
def fileno(self) -> int: ...
117-
def flush(self) -> None: ...
118-
def isatty(self) -> bool: ...
119-
def readable(self) -> bool: ...
120-
def readlines(self, __size: int = ...) -> List[bytes]: ...
121-
def seek(self, __pos: int, __whence: int = ...) -> int: ...
122-
def seekable(self) -> bool: ...
123-
def tell(self) -> int: ...
124-
def truncate(self, __size: Optional[int] = ...) -> int: ...
125-
def writable(self) -> bool: ...
126-
# TODO should be the next line instead
127-
# def writelines(self, lines: List[Union[bytes, bytearray]]) -> None: ...
128-
def writelines(self, __lines: Any) -> None: ...
129-
def readline(self, __size: Optional[int] = ...) -> bytes: ...
130-
def __del__(self) -> None: ...
131-
closed: bool
132-
# copied from BufferedIOBase
133-
def detach(self) -> RawIOBase: ...
134-
def readinto(self, __buffer: _bytearray_like) -> int: ...
135-
def write(self, __b: Union[bytes, bytearray]) -> int: ...
136-
def readinto1(self, __buffer: _bytearray_like) -> int: ...
137-
def read(self, __size: Optional[int] = ...) -> bytes: ...
138108
if sys.version_info >= (3, 7):
139109
def read1(self, __size: Optional[int] = ...) -> bytes: ...
140110
else:
141-
def read1(self, __size: Optional[int]) -> bytes: ...
111+
def read1(self, __size: Optional[int]) -> bytes: ... # type: ignore
142112

143113
class BufferedReader(BufferedIOBase):
144114
def __init__(self, raw: RawIOBase, buffer_size: int = ...) -> None: ...
145115
def peek(self, __size: int = ...) -> bytes: ...
116+
if sys.version_info >= (3, 7):
117+
def read1(self, __size: int = ...) -> bytes: ...
118+
else:
119+
def read1(self, __size: int) -> bytes: ... # type: ignore
146120

147121
class BufferedWriter(BufferedIOBase):
148122
def __init__(self, raw: RawIOBase, buffer_size: int = ...) -> None: ...
@@ -153,6 +127,10 @@ class BufferedRandom(BufferedReader, BufferedWriter):
153127
def __init__(self, raw: RawIOBase, buffer_size: int = ...) -> None: ...
154128
def seek(self, __target: int, __whence: int = ...) -> int: ...
155129
def tell(self) -> int: ...
130+
if sys.version_info >= (3, 7):
131+
def read1(self, __size: int = ...) -> bytes: ...
132+
else:
133+
def read1(self, __size: int) -> bytes: ... # type: ignore
156134

157135
class BufferedRWPair(BufferedIOBase):
158136
def __init__(self, reader: RawIOBase, writer: RawIOBase,

stdlib/3/lzma.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class LZMAFile(io.BufferedIOBase, IO[bytes]):
9292
def peek(self, size: int = ...) -> bytes: ...
9393
def read(self, size: Optional[int] = ...) -> bytes: ...
9494
def read1(self, size: int = ...) -> bytes: ...
95-
def readline(self, size: int = ...) -> bytes: ...
95+
def readline(self, size: Optional[int] = ...) -> bytes: ...
9696
def write(self, data: bytes) -> int: ...
9797
def seek(self, offset: int, whence: int = ...) -> int: ...
9898
def tell(self) -> int: ...

tests/stubtest_whitelists/py35.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ ctypes.CDLL.__init__
2525
fractions.Fraction.__new__ # private _normalize param was made keyword-only in Python 3.6
2626
importlib.metadata
2727
importlib.resources
28-
io.BufferedRandom.read1
29-
io.BufferedReader.read1
3028
io.StringIO.readline
3129
ipaddress._BaseNetwork.__init__
3230
json.dump

tests/stubtest_whitelists/py36.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ email.message.MIMEPart.as_string
2323
enum.Enum._generate_next_value_
2424
importlib.metadata
2525
importlib.resources
26-
io.BufferedRandom.read1
27-
io.BufferedReader.read1
2826
io.StringIO.readline
2927
ipaddress._BaseNetwork.__init__
3028
json.loads

tests/stubtest_whitelists/py3_common.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ io.BufferedReader.truncate
256256
io.BufferedWriter.seek
257257
io.BufferedWriter.truncate
258258
io.BytesIO.readlines
259+
io.BytesIO.seek # Parameter name for a positional-only param differs from its name in the inherited method
259260
io.FileIO.seek
260261
io.StringIO.seek
261262
io.StringIO.truncate

0 commit comments

Comments
 (0)