Skip to content

Commit 8f5a835

Browse files
tempfile.SpooledTemporaryFile: inherit from IOBase on 3.11 (#7802)
python/cpython#29560 Co-authored-by: Alex Waygood <[email protected]>
1 parent eab82c8 commit 8f5a835

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

stdlib/tempfile.pyi

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import io
12
import os
23
import sys
3-
from _typeshed import Self
4+
from _typeshed import Self, WriteableBuffer
45
from collections.abc import Iterable, Iterator
56
from types import TracebackType
67
from typing import IO, Any, AnyStr, Generic, overload
@@ -217,9 +218,14 @@ class _TemporaryFileWrapper(Generic[AnyStr], IO[AnyStr]):
217218
def write(self, s: AnyStr) -> int: ...
218219
def writelines(self, lines: Iterable[AnyStr]) -> None: ...
219220

220-
# It does not actually derive from IO[AnyStr], but it does implement the
221-
# protocol.
222-
class SpooledTemporaryFile(IO[AnyStr]):
221+
if sys.version_info >= (3, 11):
222+
_SpooledTemporaryFileBase = io.IOBase
223+
else:
224+
_SpooledTemporaryFileBase = object
225+
226+
# It does not actually derive from IO[AnyStr], but it does mostly behave
227+
# like one.
228+
class SpooledTemporaryFile(IO[AnyStr], _SpooledTemporaryFileBase):
223229
@property
224230
def encoding(self) -> str: ... # undocumented
225231
@property
@@ -318,20 +324,28 @@ class SpooledTemporaryFile(IO[AnyStr]):
318324
def fileno(self) -> int: ...
319325
def flush(self) -> None: ...
320326
def isatty(self) -> bool: ...
321-
def read(self, n: int = ...) -> AnyStr: ...
322-
def readline(self, limit: int = ...) -> AnyStr: ...
323-
def readlines(self, hint: int = ...) -> list[AnyStr]: ...
327+
if sys.version_info >= (3, 11):
328+
# These three work only if the SpooledTemporaryFile is opened in binary mode,
329+
# because the underlying object in text mode does not have these methods.
330+
def read1(self, __size: int = ...) -> AnyStr: ...
331+
def readinto(self, b: WriteableBuffer) -> int: ...
332+
def readinto1(self, b: WriteableBuffer) -> int: ...
333+
def detach(self) -> io.RawIOBase: ...
334+
335+
def read(self, __n: int = ...) -> AnyStr: ...
336+
def readline(self, __limit: int | None = ...) -> AnyStr: ... # type: ignore[override]
337+
def readlines(self, __hint: int = ...) -> list[AnyStr]: ... # type: ignore[override]
324338
def seek(self, offset: int, whence: int = ...) -> int: ...
325339
def tell(self) -> int: ...
326340
def truncate(self, size: int | None = ...) -> None: ... # type: ignore[override]
327341
def write(self, s: AnyStr) -> int: ...
328-
def writelines(self, iterable: Iterable[AnyStr]) -> None: ...
329-
def __iter__(self) -> Iterator[AnyStr]: ...
330-
# Other than the following methods, which do not exist on SpooledTemporaryFile
342+
def writelines(self, iterable: Iterable[AnyStr]) -> None: ... # type: ignore[override]
343+
def __iter__(self) -> Iterator[AnyStr]: ... # type: ignore[override]
344+
# These exist at runtime only on 3.11+.
331345
def readable(self) -> bool: ...
332346
def seekable(self) -> bool: ...
333347
def writable(self) -> bool: ...
334-
def __next__(self) -> AnyStr: ...
348+
def __next__(self) -> AnyStr: ... # type: ignore[override]
335349
if sys.version_info >= (3, 9):
336350
def __class_getitem__(cls, item: Any) -> GenericAlias: ...
337351

tests/stubtest_allowlists/py311.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,6 @@ subprocess.getstatusoutput
149149
symtable.SymbolTable.has_exec
150150
sys.UnraisableHookArgs # Not exported from sys
151151
sys.exception
152-
tempfile.SpooledTemporaryFile.detach
153-
tempfile.SpooledTemporaryFile.read1
154-
tempfile.SpooledTemporaryFile.readinto
155-
tempfile.SpooledTemporaryFile.readinto1
156152
tkinter._VersionInfoType.__doc__
157153
traceback.StackSummary.format_frame_summary
158154
traceback.TracebackException.__init__

0 commit comments

Comments
 (0)