From 155fdd0cd4ff7fdbdf6163e8e0d6880374e73f38 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 12 Mar 2017 19:32:46 -0700 Subject: [PATCH] Fixes to tempfile stubs (#981) Fixes #975 And a few more things I noticed while reading the tempfile docs. In 3.5, most functions in tempfile were changed to accept either str or bytes in their prefix and suffix arguments, and return bytes or str accordingly. This seemed like a case for AnyStr. We were missing tempdirb and tempprefixb, added in 3.5. TemporaryFile and others were declared as returning BinaryIO, but they actually return either binary or text IO depending on the mode passed in. I changed the return type to IO[Any], similar to builtins.open. --- stdlib/2/tempfile.pyi | 6 +-- stdlib/3/tempfile.pyi | 111 ++++++++++++++++++++++++++++-------------- 2 files changed, 78 insertions(+), 39 deletions(-) diff --git a/stdlib/2/tempfile.pyi b/stdlib/2/tempfile.pyi index d718bd703..15eda96f9 100644 --- a/stdlib/2/tempfile.pyi +++ b/stdlib/2/tempfile.pyi @@ -86,16 +86,16 @@ class TemporaryDirectory: @overload def mkstemp() -> Tuple[int, str]: ... @overload -def mkstemp(suffix: AnyStr = ..., prefix: AnyStr = ..., dir: AnyStr = ..., +def mkstemp(suffix: AnyStr = ..., prefix: AnyStr = ..., dir: Optional[AnyStr] = ..., text: bool = ...) -> Tuple[int, AnyStr]: ... @overload def mkdtemp() -> str: ... @overload -def mkdtemp(suffix: AnyStr = ..., prefix: AnyStr = ..., dir: AnyStr = ...) -> AnyStr: ... +def mkdtemp(suffix: AnyStr = ..., prefix: AnyStr = ..., dir: Optional[AnyStr] = ...) -> AnyStr: ... @overload def mktemp() -> str: ... @overload -def mktemp(suffix: AnyStr = ..., prefix: AnyStr = ..., dir: AnyStr = ...) -> AnyStr: ... +def mktemp(suffix: AnyStr = ..., prefix: AnyStr = ..., dir: Optional[AnyStr] = ...) -> AnyStr: ... def gettempdir() -> str: ... def gettempprefix() -> str: ... diff --git a/stdlib/3/tempfile.pyi b/stdlib/3/tempfile.pyi index ea5207ad0..ab8c38099 100644 --- a/stdlib/3/tempfile.pyi +++ b/stdlib/3/tempfile.pyi @@ -3,49 +3,88 @@ # based on http://docs.python.org/3.3/library/tempfile.html +import sys from types import TracebackType -from typing import BinaryIO, Optional, Tuple, Type +from typing import Any, AnyStr, Generic, IO, Optional, Tuple, Type # global variables -tempdir = ... # type: str +tempdir = ... # type: Optional[str] template = ... # type: str -# TODO text files -# function stubs -def TemporaryFile( - mode: str = ..., buffering: int = ..., encoding: str = ..., - newline: str = ..., suffix: str = ..., prefix: str = ..., - dir: str = ... -) -> BinaryIO: - ... -def NamedTemporaryFile( - mode: str = ..., buffering: int = ..., encoding: str = ..., - newline: str = ..., suffix: str = ..., prefix: str = ..., - dir: str = ..., delete: bool =... -) -> BinaryIO: - ... -def SpooledTemporaryFile( - max_size: int = ..., mode: str = ..., buffering: int = ..., - encoding: str = ..., newline: str = ..., suffix: str = ..., - prefix: str = ..., dir: str = ... -) -> BinaryIO: - ... +if sys.version_info >= (3, 5): + def TemporaryFile( + mode: str = ..., buffering: int = ..., encoding: str = ..., + newline: str = ..., suffix: Optional[AnyStr]= ..., prefix: Optional[AnyStr] = ..., + dir: Optional[AnyStr] = ... + ) -> IO[Any]: + ... + def NamedTemporaryFile( + mode: str = ..., buffering: int = ..., encoding: str = ..., + newline: str = ..., suffix: Optional[AnyStr] = ..., prefix: Optional[AnyStr] = ..., + dir: Optional[AnyStr] = ..., delete: bool =... + ) -> IO[Any]: + ... + def SpooledTemporaryFile( + max_size: int = ..., mode: str = ..., buffering: int = ..., + encoding: str = ..., newline: str = ..., suffix: Optional[AnyStr] = ..., + prefix: Optional[AnyStr] = ..., dir: Optional[AnyStr] = ... + ) -> IO[Any]: + ... -class TemporaryDirectory: - name = ... # type: str - def __init__(self, suffix: str = ..., prefix: str = ..., - dir: str = ...) -> None: ... - def cleanup(self) -> None: ... - def __enter__(self) -> str: ... - def __exit__(self, exc_type: Optional[Type[BaseException]], - exc_val: Optional[Exception], - exc_tb: Optional[TracebackType]) -> bool: ... + class TemporaryDirectory(Generic[AnyStr]): + name = ... # type: str + def __init__(self, suffix: Optional[AnyStr] = ..., prefix: Optional[AnyStr] = ..., + dir: Optional[AnyStr] = ...) -> None: ... + def cleanup(self) -> None: ... + def __enter__(self) -> AnyStr: ... + def __exit__(self, exc_type: Optional[Type[BaseException]], + exc_val: Optional[BaseException], + exc_tb: Optional[TracebackType]) -> bool: ... + + def mkstemp(suffix: Optional[AnyStr] = ..., prefix: Optional[AnyStr] = ..., dir: Optional[AnyStr] = ..., + text: bool = ...) -> Tuple[int, AnyStr]: ... + def mkdtemp(suffix: Optional[AnyStr] = ..., prefix: Optional[AnyStr] = ..., + dir: Optional[str] = ...) -> AnyStr: ... + def mktemp(suffix: Optional[AnyStr] = ..., prefix: Optional[AnyStr] = ..., dir: Optional[AnyStr] = ...) -> AnyStr: ... + + def gettempdirb() -> bytes: ... + def gettempprefixb() -> bytes: ... +else: + def TemporaryFile( + mode: str = ..., buffering: int = ..., encoding: str = ..., + newline: str = ..., suffix: str = ..., prefix: str = ..., + dir: Optional[str] = ... + ) -> IO[Any]: + ... + def NamedTemporaryFile( + mode: str = ..., buffering: int = ..., encoding: str = ..., + newline: str = ..., suffix: str = ..., prefix: str = ..., + dir: Optional[str] = ..., delete: bool =... + ) -> IO[Any]: + ... + def SpooledTemporaryFile( + max_size: int = ..., mode: str = ..., buffering: int = ..., + encoding: str = ..., newline: str = ..., suffix: str = ..., + prefix: str = ..., dir: Optional[str] = ... + ) -> IO[Any]: + ... + + class TemporaryDirectory: + name = ... # type: str + def __init__(self, suffix: str = ..., prefix: str = ..., + dir: Optional[str] = ...) -> None: ... + def cleanup(self) -> None: ... + def __enter__(self) -> str: ... + def __exit__(self, exc_type: Optional[Type[BaseException]], + exc_val: Optional[BaseException], + exc_tb: Optional[TracebackType]) -> bool: ... + + def mkstemp(suffix: str = ..., prefix: str = ..., dir: Optional[str] = ..., + text: bool = ...) -> Tuple[int, str]: ... + def mkdtemp(suffix: str = ..., prefix: str = ..., + dir: Optional[str] = ...) -> str: ... + def mktemp(suffix: str = ..., prefix: str = ..., dir: Optional[str] = ...) -> str: ... -def mkstemp(suffix: str = ..., prefix: str = ..., dir: str = ..., - text: bool = ...) -> Tuple[int, str]: ... -def mkdtemp(suffix: str = ..., prefix: str = ..., - dir: str = ...) -> str: ... -def mktemp(suffix: str = ..., prefix: str = ..., dir: str = ...) -> str: ... def gettempdir() -> str: ... def gettempprefix() -> str: ...