Make _TemporaryFileWrapper generic on AnyStr, use it (#5456)

* Make _TemporaryFileWrapper generic on AnyStr, use it

Previously it was not used on NamedTemoraryFile and thus the file
field was missing from the types for the return value of
NamedTemoraryFile.
This commit is contained in:
Jade
2021-05-17 09:04:40 -07:00
committed by GitHub
parent a0abacdeaf
commit 56ebf92463

View File

@@ -28,7 +28,7 @@ if sys.version_info >= (3, 8):
delete: bool = ...,
*,
errors: Optional[str] = ...,
) -> IO[str]: ...
) -> _TemporaryFileWrapper[str]: ...
@overload
def NamedTemporaryFile(
mode: Literal["rb", "wb", "ab", "xb", "r+b", "w+b", "a+b", "x+b"] = ...,
@@ -41,7 +41,7 @@ if sys.version_info >= (3, 8):
delete: bool = ...,
*,
errors: Optional[str] = ...,
) -> IO[bytes]: ...
) -> _TemporaryFileWrapper[bytes]: ...
@overload
def NamedTemporaryFile(
mode: str = ...,
@@ -54,7 +54,7 @@ if sys.version_info >= (3, 8):
delete: bool = ...,
*,
errors: Optional[str] = ...,
) -> IO[Any]: ...
) -> _TemporaryFileWrapper[Any]: ...
else:
@overload
@@ -67,7 +67,7 @@ else:
prefix: Optional[AnyStr] = ...,
dir: Optional[_DirT[AnyStr]] = ...,
delete: bool = ...,
) -> IO[str]: ...
) -> _TemporaryFileWrapper[str]: ...
@overload
def NamedTemporaryFile(
mode: Literal["rb", "wb", "ab", "xb", "r+b", "w+b", "a+b", "x+b"] = ...,
@@ -78,7 +78,7 @@ else:
prefix: Optional[AnyStr] = ...,
dir: Optional[_DirT[AnyStr]] = ...,
delete: bool = ...,
) -> IO[bytes]: ...
) -> _TemporaryFileWrapper[bytes]: ...
@overload
def NamedTemporaryFile(
mode: str = ...,
@@ -89,7 +89,7 @@ else:
prefix: Optional[AnyStr] = ...,
dir: Optional[_DirT[AnyStr]] = ...,
delete: bool = ...,
) -> IO[Any]: ...
) -> _TemporaryFileWrapper[Any]: ...
if sys.platform == "win32":
TemporaryFile = NamedTemporaryFile
@@ -163,12 +163,12 @@ else:
dir: Optional[_DirT[AnyStr]] = ...,
) -> IO[Any]: ...
class _TemporaryFileWrapper(IO[str]):
file: IO[str]
class _TemporaryFileWrapper(Generic[AnyStr], IO[AnyStr]):
file: IO[AnyStr]
name: Any
delete: bool
def __init__(self, file: IO[str], name: Any, delete: bool = ...) -> None: ...
def __enter__(self) -> _TemporaryFileWrapper: ...
def __init__(self, file: IO[AnyStr], name: Any, delete: bool = ...) -> None: ...
def __enter__(self) -> _TemporaryFileWrapper[AnyStr]: ...
def __exit__(
self, exc: Optional[Type[BaseException]], value: Optional[BaseException], tb: Optional[TracebackType]
) -> Optional[bool]: ...
@@ -178,23 +178,30 @@ class _TemporaryFileWrapper(IO[str]):
# These methods don't exist directly on this object, but
# are delegated to the underlying IO object through __getattr__.
# We need to add them here so that this class is concrete.
def __iter__(self) -> Iterator[str]: ...
def __next__(self) -> str: ...
def __iter__(self) -> Iterator[AnyStr]: ...
# FIXME: __next__ doesn't actually exist on this class and should be removed:
# see also https://github.com/python/typeshed/pull/5456#discussion_r633068648
# >>> import tempfile
# >>> ntf=tempfile.NamedTemporaryFile()
# >>> next(ntf)
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: '_TemporaryFileWrapper' object is not an iterator
def __next__(self) -> AnyStr: ...
def fileno(self) -> int: ...
def flush(self) -> None: ...
def isatty(self) -> bool: ...
def next(self) -> str: ...
def read(self, n: int = ...) -> str: ...
def read(self, n: int = ...) -> AnyStr: ...
def readable(self) -> bool: ...
def readline(self, limit: int = ...) -> str: ...
def readlines(self, hint: int = ...) -> List[str]: ...
def readline(self, limit: int = ...) -> AnyStr: ...
def readlines(self, hint: int = ...) -> list[AnyStr]: ...
def seek(self, offset: int, whence: int = ...) -> int: ...
def seekable(self) -> bool: ...
def tell(self) -> int: ...
def truncate(self, size: Optional[int] = ...) -> int: ...
def writable(self) -> bool: ...
def write(self, s: str) -> int: ...
def writelines(self, lines: Iterable[str]) -> None: ...
def write(self, s: AnyStr) -> int: ...
def writelines(self, lines: Iterable[AnyStr]) -> None: ...
# It does not actually derive from IO[AnyStr], but it does implement the
# protocol.