From 71506a82afed3890e48345bcb1ae32030424fc25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ila=C3=AF=20Deutel?= Date: Sat, 12 Oct 2019 22:46:34 -0700 Subject: [PATCH 1/9] Overload on mode for open() and Path.open() --- stdlib/2and3/builtins.pyi | 72 +++++++++++++++++++++++++++++++++++---- stdlib/3/pathlib.pyi | 14 ++++++-- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/stdlib/2and3/builtins.pyi b/stdlib/2and3/builtins.pyi index 0a39dae9f752..985c1b208bda 100644 --- a/stdlib/2and3/builtins.pyi +++ b/stdlib/2and3/builtins.pyi @@ -5,7 +5,7 @@ from typing import ( TypeVar, Iterator, Iterable, NoReturn, overload, Container, Sequence, MutableSequence, Mapping, MutableMapping, Tuple, List, Any, Dict, Callable, Generic, Set, AbstractSet, FrozenSet, MutableSet, Sized, Reversible, SupportsInt, SupportsFloat, SupportsAbs, - SupportsComplex, IO, BinaryIO, Union, + SupportsComplex, IO, BinaryIO, TextIO, Union, ItemsView, KeysView, ValuesView, ByteString, Optional, AnyStr, Type, Text, Protocol, ) @@ -1333,16 +1333,74 @@ def next(__i: Iterator[_T]) -> _T: ... def next(__i: Iterator[_T], default: _VT) -> Union[_T, _VT]: ... def oct(__i: Union[int, _SupportsIndex]) -> str: ... +if sys.version_info >= (3, 3): + # Changed in version 3.3: The 'x' mode was added. + _OPEN_TEXT_MODE = Literal[ + 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', + 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', + 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', + 'x', 'x+', '+x', 'xt', 'tx', 'xt+', 'x+t', '+xt', 'tx+', 't+x', '+tx', + 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', + ] + _OPEN_BINARY_MODE = Literal[ + 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', + 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', + 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', + 'xb', 'bx', 'xb+', 'x+b', '+xb', 'bx+', 'b+x', '+bx', + 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', + ] +else: + _OPEN_TEXT_MODE = Literal[ + 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', + 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', + 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', + 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', + ] + _OPEN_BINARY_MODE = Literal[ + 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', + 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', + 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', + 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', + ] + if sys.version_info >= (3, 6): - def open(file: Union[str, bytes, int, _PathLike[Any]], mode: str = ..., buffering: int = ..., encoding: Optional[str] = ..., - errors: Optional[str] = ..., newline: Optional[str] = ..., closefd: bool = ..., - opener: Optional[Callable[[str, int], int]] = ...) -> IO[Any]: ... + # Changed in version 3.6: Support added to accept objects implementing os.PathLike. + _OPEN_FILE = Union[str, bytes, int, _PathLike[Any]] elif sys.version_info >= (3,): - def open(file: Union[str, bytes, int], mode: str = ..., buffering: int = ..., encoding: Optional[str] = ..., + _OPEN_FILE = Union[str, bytes, int] +else: + _OPEN_FILE = Union[unicode, int] + +if sys.version_info >= (3, 3): + # Changed in version 3.3: The opener parameter was added. + @overload + def open(file: _OPEN_FILE, mode: _OPEN_TEXT_MODE = ..., buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., newline: Optional[str] = ..., closefd: bool = ..., - opener: Optional[Callable[[str, int], int]] = ...) -> IO[Any]: ... + opener: Optional[Callable[[str, int], int]] = ...) -> TextIO: ... + @overload + def open(file: _OPEN_FILE, mode: _OPEN_BINARY_MODE, buffering: int = ..., encoding: None = ..., errors: None = ..., + newline: None = ..., closefd: bool = ..., opener: Optional[Callable[[str, int], int]] = ...) -> BinaryIO: ... + @overload + def open(file: _OPEN_FILE, mode: str, buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., + newline: Optional[str] = ..., closefd: bool = ..., opener: Optional[Callable[[str, int], int]] = ...) \ + -> IO[Any]: ... +elif sys.version_info >= (3,): + @overload + def open(file: _OPEN_FILE, mode: _OPEN_TEXT_MODE = ..., buffering: int = ..., encoding: Optional[str] = ..., + errors: Optional[str] = ..., newline: Optional[str] = ..., closefd: bool = ...) -> TextIO: ... + @overload + def open(file: _OPEN_FILE, mode: _OPEN_BINARY_MODE, buffering: int = ..., encoding: None = ..., errors: None = ..., + newline: None = ..., closefd: bool = ...) -> BinaryIO: ... + @overload + def open(file: _OPEN_FILE, mode: str, buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., + newline: Optional[str] = ..., closefd: bool = ...) -> IO[Any]: ... else: - def open(name: Union[unicode, int], mode: unicode = ..., buffering: int = ...) -> BinaryIO: ... + @overload + def open(file: _OPEN_FILE, mode: _OPEN_TEXT_MODE = ..., buffering: int = ...) -> TextIO: ... + @overload + def open(file: _OPEN_FILE, mode: _OPEN_BINARY_MODE, buffering: int = ...) -> BinaryIO: ... + @overload + def open(file: _OPEN_FILE, mode: unicode, buffering: int = ...) -> IO[Any]: ... def ord(__c: Union[Text, bytes]) -> int: ... if sys.version_info >= (3,): diff --git a/stdlib/3/pathlib.pyi b/stdlib/3/pathlib.pyi index fb925e60121b..ddd02441afef 100644 --- a/stdlib/3/pathlib.pyi +++ b/stdlib/3/pathlib.pyi @@ -1,4 +1,6 @@ -from typing import Any, Generator, IO, Optional, Sequence, Tuple, Type, TypeVar, Union, List +from builtins import _OPEN_BINARY_MODE, _OPEN_TEXT_MODE +from typing import (Any, BinaryIO, Generator, IO, List, Optional, Sequence, + TextIO, Tuple, Type, TypeVar, Union, overload) from types import TracebackType import os import sys @@ -91,8 +93,14 @@ class Path(PurePath): else: def mkdir(self, mode: int = ..., parents: bool = ..., exist_ok: bool = ...) -> None: ... - def open(self, mode: str = ..., buffering: int = ..., - encoding: Optional[str] = ..., errors: Optional[str] = ..., + @overload + def open(self, mode: _OPEN_TEXT_MODE = ..., buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., + newline: Optional[str] = ...) -> TextIO: ... + @overload + def open(self, mode: _OPEN_BINARY_MODE, buffering: int = ..., encoding: None = ..., errors: None = ..., + newline: None = ...) -> BinaryIO: ... + @overload + def open(self, mode: str = ..., buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., newline: Optional[str] = ...) -> IO[Any]: ... def owner(self) -> str: ... def rename(self, target: Union[str, PurePath]) -> None: ... From 6bec337d6d04c77255a92712a58f04a72bcf3887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ila=C3=AF=20Deutel?= Date: Tue, 15 Oct 2019 22:43:21 -0700 Subject: [PATCH 2/9] Update Python2 stubs --- stdlib/2/__builtin__.pyi | 72 ++++++++++++++++++++++++++++++++++---- third_party/2/pathlib2.pyi | 14 ++++++-- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/stdlib/2/__builtin__.pyi b/stdlib/2/__builtin__.pyi index 0a39dae9f752..985c1b208bda 100644 --- a/stdlib/2/__builtin__.pyi +++ b/stdlib/2/__builtin__.pyi @@ -5,7 +5,7 @@ from typing import ( TypeVar, Iterator, Iterable, NoReturn, overload, Container, Sequence, MutableSequence, Mapping, MutableMapping, Tuple, List, Any, Dict, Callable, Generic, Set, AbstractSet, FrozenSet, MutableSet, Sized, Reversible, SupportsInt, SupportsFloat, SupportsAbs, - SupportsComplex, IO, BinaryIO, Union, + SupportsComplex, IO, BinaryIO, TextIO, Union, ItemsView, KeysView, ValuesView, ByteString, Optional, AnyStr, Type, Text, Protocol, ) @@ -1333,16 +1333,74 @@ def next(__i: Iterator[_T]) -> _T: ... def next(__i: Iterator[_T], default: _VT) -> Union[_T, _VT]: ... def oct(__i: Union[int, _SupportsIndex]) -> str: ... +if sys.version_info >= (3, 3): + # Changed in version 3.3: The 'x' mode was added. + _OPEN_TEXT_MODE = Literal[ + 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', + 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', + 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', + 'x', 'x+', '+x', 'xt', 'tx', 'xt+', 'x+t', '+xt', 'tx+', 't+x', '+tx', + 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', + ] + _OPEN_BINARY_MODE = Literal[ + 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', + 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', + 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', + 'xb', 'bx', 'xb+', 'x+b', '+xb', 'bx+', 'b+x', '+bx', + 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', + ] +else: + _OPEN_TEXT_MODE = Literal[ + 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', + 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', + 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', + 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', + ] + _OPEN_BINARY_MODE = Literal[ + 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', + 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', + 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', + 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', + ] + if sys.version_info >= (3, 6): - def open(file: Union[str, bytes, int, _PathLike[Any]], mode: str = ..., buffering: int = ..., encoding: Optional[str] = ..., - errors: Optional[str] = ..., newline: Optional[str] = ..., closefd: bool = ..., - opener: Optional[Callable[[str, int], int]] = ...) -> IO[Any]: ... + # Changed in version 3.6: Support added to accept objects implementing os.PathLike. + _OPEN_FILE = Union[str, bytes, int, _PathLike[Any]] elif sys.version_info >= (3,): - def open(file: Union[str, bytes, int], mode: str = ..., buffering: int = ..., encoding: Optional[str] = ..., + _OPEN_FILE = Union[str, bytes, int] +else: + _OPEN_FILE = Union[unicode, int] + +if sys.version_info >= (3, 3): + # Changed in version 3.3: The opener parameter was added. + @overload + def open(file: _OPEN_FILE, mode: _OPEN_TEXT_MODE = ..., buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., newline: Optional[str] = ..., closefd: bool = ..., - opener: Optional[Callable[[str, int], int]] = ...) -> IO[Any]: ... + opener: Optional[Callable[[str, int], int]] = ...) -> TextIO: ... + @overload + def open(file: _OPEN_FILE, mode: _OPEN_BINARY_MODE, buffering: int = ..., encoding: None = ..., errors: None = ..., + newline: None = ..., closefd: bool = ..., opener: Optional[Callable[[str, int], int]] = ...) -> BinaryIO: ... + @overload + def open(file: _OPEN_FILE, mode: str, buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., + newline: Optional[str] = ..., closefd: bool = ..., opener: Optional[Callable[[str, int], int]] = ...) \ + -> IO[Any]: ... +elif sys.version_info >= (3,): + @overload + def open(file: _OPEN_FILE, mode: _OPEN_TEXT_MODE = ..., buffering: int = ..., encoding: Optional[str] = ..., + errors: Optional[str] = ..., newline: Optional[str] = ..., closefd: bool = ...) -> TextIO: ... + @overload + def open(file: _OPEN_FILE, mode: _OPEN_BINARY_MODE, buffering: int = ..., encoding: None = ..., errors: None = ..., + newline: None = ..., closefd: bool = ...) -> BinaryIO: ... + @overload + def open(file: _OPEN_FILE, mode: str, buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., + newline: Optional[str] = ..., closefd: bool = ...) -> IO[Any]: ... else: - def open(name: Union[unicode, int], mode: unicode = ..., buffering: int = ...) -> BinaryIO: ... + @overload + def open(file: _OPEN_FILE, mode: _OPEN_TEXT_MODE = ..., buffering: int = ...) -> TextIO: ... + @overload + def open(file: _OPEN_FILE, mode: _OPEN_BINARY_MODE, buffering: int = ...) -> BinaryIO: ... + @overload + def open(file: _OPEN_FILE, mode: unicode, buffering: int = ...) -> IO[Any]: ... def ord(__c: Union[Text, bytes]) -> int: ... if sys.version_info >= (3,): diff --git a/third_party/2/pathlib2.pyi b/third_party/2/pathlib2.pyi index fb925e60121b..ddd02441afef 100644 --- a/third_party/2/pathlib2.pyi +++ b/third_party/2/pathlib2.pyi @@ -1,4 +1,6 @@ -from typing import Any, Generator, IO, Optional, Sequence, Tuple, Type, TypeVar, Union, List +from builtins import _OPEN_BINARY_MODE, _OPEN_TEXT_MODE +from typing import (Any, BinaryIO, Generator, IO, List, Optional, Sequence, + TextIO, Tuple, Type, TypeVar, Union, overload) from types import TracebackType import os import sys @@ -91,8 +93,14 @@ class Path(PurePath): else: def mkdir(self, mode: int = ..., parents: bool = ..., exist_ok: bool = ...) -> None: ... - def open(self, mode: str = ..., buffering: int = ..., - encoding: Optional[str] = ..., errors: Optional[str] = ..., + @overload + def open(self, mode: _OPEN_TEXT_MODE = ..., buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., + newline: Optional[str] = ...) -> TextIO: ... + @overload + def open(self, mode: _OPEN_BINARY_MODE, buffering: int = ..., encoding: None = ..., errors: None = ..., + newline: None = ...) -> BinaryIO: ... + @overload + def open(self, mode: str = ..., buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., newline: Optional[str] = ...) -> IO[Any]: ... def owner(self) -> str: ... def rename(self, target: Union[str, PurePath]) -> None: ... From 23749a63433c3a6ce4d5f0361537957fd03724fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ila=C3=AF=20Deutel?= Date: Tue, 15 Oct 2019 23:31:49 -0700 Subject: [PATCH 3/9] overload + Literal on BZ2, gzip, LZMA --- stdlib/2and3/bz2.pyi | 62 ++++++++++++++------ stdlib/3/gzip.pyi | 64 +++++++++++++++++++-- stdlib/3/lzma.pyi | 132 ++++++++++++++++++++++++++++++++----------- 3 files changed, 202 insertions(+), 56 deletions(-) diff --git a/stdlib/2and3/bz2.pyi b/stdlib/2and3/bz2.pyi index 2cb329ce93c8..480f67702d0d 100644 --- a/stdlib/2and3/bz2.pyi +++ b/stdlib/2and3/bz2.pyi @@ -1,32 +1,58 @@ import io import sys -from typing import Any, IO, Optional, Union +from os.path import _PathType +from typing import IO, Any, Optional, TextIO, Union, overload -if sys.version_info >= (3, 6): - from os import PathLike - _PathOrFile = Union[str, bytes, IO[Any], PathLike[Any]] -elif sys.version_info >= (3, 3): - _PathOrFile = Union[str, bytes, IO[Any]] +if sys.version_info >= (3, 8): + from typing import Literal else: - _PathOrFile = str + from typing_extensions import Literal + +_PathOrFile = Union[_PathType, IO[bytes]] def compress(data: bytes, compresslevel: int = ...) -> bytes: ... def decompress(data: bytes) -> bytes: ... if sys.version_info >= (3, 3): - def open(filename: _PathOrFile, - mode: str = ..., - compresslevel: int = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ...) -> IO[Any]: ... + if sys.version_info >= (3, 4): + # Changed in version 3.4: The 'x' (exclusive creation) mode was added. + _OPEN_BINARY_MODE = Literal["r", "rb", "w", "wb", "x", "xb", "a", "ab"] + _OPEN_TEXT_MODE = Literal["rt", "wt", "xt", "at"] + else: + _OPEN_BINARY_MODE = Literal["r", "rb", "w", "wb", "a", "ab"] + _OPEN_TEXT_MODE = Literal["rt", "wt", "at"] + @overload + def open( + filename: _PathOrFile, + mode: _OPEN_BINARY_MODE = ..., + compresslevel: int = ..., + encoding: None = ..., + errors: None = ..., + newline: None = ..., + ) -> BZ2File: ... + @overload + def open( + filename: _PathType, + mode: _OPEN_TEXT_MODE, + compresslevel: int = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., + ) -> TextIO: ... + @overload + def open( + filename: _PathOrFile, + mode: str = ..., + compresslevel: int = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., + ) -> Union[BZ2File, TextIO]: ... class BZ2File(io.BufferedIOBase, IO[bytes]): # type: ignore # python/mypy#5027 - def __init__(self, - filename: _PathOrFile, - mode: str = ..., - buffering: Optional[Any] = ..., - compresslevel: int = ...) -> None: ... + def __init__( + self, filename: _PathOrFile, mode: str = ..., buffering: Optional[Any] = ..., compresslevel: int = ... + ) -> None: ... class BZ2Compressor(object): def __init__(self, compresslevel: int = ...) -> None: ... diff --git a/stdlib/3/gzip.pyi b/stdlib/3/gzip.pyi index ce8152c34f09..24f08e7f6d8b 100644 --- a/stdlib/3/gzip.pyi +++ b/stdlib/3/gzip.pyi @@ -1,10 +1,55 @@ -from typing import Any, IO, Optional -from os.path import _PathType -import _compression import sys import zlib +from os.path import _PathType +from typing import IO, BinaryIO, Optional, TextIO, Union, overload -def open(filename, mode: str = ..., compresslevel: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., newline: Optional[str] = ...) -> IO[Any]: ... +import _compression + +if sys.version_info >= (3, 8): + from typing import Literal +else: + from typing_extensions import Literal + +if sys.version_info >= (3, 3): + # Changed in version 3.3: Added support for filename being a file object, support for text mode, and the encoding, errors and newline arguments. + + if sys.version_info >= (3, 4): + # Changed in version 3.4: Added support for the 'x', 'xb' and 'xt' modes. + _OPEN_BINARY_MODE = Literal["r", "rb", "a", "ab", "w", "wb", "x", "xb"] + _OPEN_TEXT_MODE = Literal["rt", "at", "wt", "xt"] + else: + _OPEN_BINARY_MODE = Literal["r", "rb", "a", "ab", "w", "wb"] + _OPEN_TEXT_MODE = Literal["rt", "at", "wt"] + @overload + def open( + filename: Union[_PathType, IO[bytes]], + mode: _OPEN_BINARY_MODE = ..., + compresslevel: int = ..., + encoding: None = ..., + errors: None = ..., + newline: None = ..., + ) -> GzipFile: ... + @overload + def open( + filename: _PathType, + mode: _OPEN_TEXT_MODE, + compresslevel: int = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., + ) -> TextIO: ... + @overload + def open( + filename: Union[_PathType, IO[bytes]], + mode: str = ..., + compresslevel: int = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., + ) -> Union[GzipFile, TextIO]: ... + +else: + def open(filename: _PathType, mode: str = ..., compresslevel: int = ...) -> GzipFile: ... class _PaddedFile: file: IO[bytes] @@ -20,7 +65,14 @@ class GzipFile(_compression.BaseStream): name: str compress: zlib._Compress fileobj: IO[bytes] - def __init__(self, filename: Optional[_PathType] = ..., mode: Optional[str] = ..., compresslevel: int = ..., fileobj: Optional[IO[bytes]] = ..., mtime: Optional[float] = ...) -> None: ... + def __init__( + self, + filename: Optional[_PathType] = ..., + mode: Optional[str] = ..., + compresslevel: int = ..., + fileobj: Optional[IO[bytes]] = ..., + mtime: Optional[float] = ..., + ) -> None: ... @property def filename(self) -> str: ... @property @@ -48,6 +100,8 @@ class _GzipReader(_compression.DecompressReader): if sys.version_info >= (3, 8): def compress(data, compresslevel: int = ..., *, mtime: Optional[float] = ...) -> bytes: ... + else: def compress(data, compresslevel: int = ...) -> bytes: ... + def decompress(data: bytes) -> bytes: ... diff --git a/stdlib/3/lzma.pyi b/stdlib/3/lzma.pyi index 37009c813cee..ef86c573984b 100644 --- a/stdlib/3/lzma.pyi +++ b/stdlib/3/lzma.pyi @@ -1,12 +1,22 @@ import io import sys -from typing import Any, IO, Mapping, Optional, Sequence, Union +from os.path import _PathType +from typing import IO, Any, Mapping, Optional, Sequence, TextIO, Union, overload -if sys.version_info >= (3, 6): - from os import PathLike - _PathOrFile = Union[str, bytes, IO[Any], PathLike[Any]] +if sys.version_info >= (3, 8): + from typing import Literal else: - _PathOrFile = Union[str, bytes, IO[Any]] + from typing_extensions import Literal + +if sys.version_info >= (3, 4): + # Changed in version 3.4: Added support for the "x", "xb" and "xt" modes. + _OPEN_BINARY_WRITING_MODE = Literal["w", "wb", "x", "xb", "a", "ab"] + _OPEN_TEXT_WRITING_MODE = Literal["wt", "xt", "at"] +else: + _OPEN_BINARY_WRITING_MODE = Literal["w", "wb", "a", "ab"] + _OPEN_TEXT_WRITING_MODE = Literal["wt", "at"] + +_PathOrFile = Union[_PathType, IO[bytes]] _FilterChain = Sequence[Mapping[str, Any]] @@ -41,7 +51,9 @@ PRESET_EXTREME: int # from _lzma.c class LZMADecompressor(object): - def __init__(self, format: Optional[int] = ..., memlimit: Optional[int] = ..., filters: Optional[_FilterChain] = ...) -> None: ... + def __init__( + self, format: Optional[int] = ..., memlimit: Optional[int] = ..., filters: Optional[_FilterChain] = ... + ) -> None: ... def decompress(self, data: bytes, max_length: int = ...) -> bytes: ... @property def check(self) -> int: ... @@ -54,27 +66,25 @@ class LZMADecompressor(object): # from _lzma.c class LZMACompressor(object): - def __init__(self, - format: Optional[int] = ..., - check: int = ..., - preset: Optional[int] = ..., - filters: Optional[_FilterChain] = ...) -> None: ... + def __init__( + self, format: Optional[int] = ..., check: int = ..., preset: Optional[int] = ..., filters: Optional[_FilterChain] = ... + ) -> None: ... def compress(self, data: bytes) -> bytes: ... def flush(self) -> bytes: ... - class LZMAError(Exception): ... - class LZMAFile(io.BufferedIOBase, IO[bytes]): # type: ignore # python/mypy#5027 - def __init__(self, - filename: Optional[_PathOrFile] = ..., - mode: str = ..., - *, - format: Optional[int] = ..., - check: int = ..., - preset: Optional[int] = ..., - filters: Optional[_FilterChain] = ...) -> None: ... + def __init__( + self, + filename: Optional[_PathOrFile] = ..., + mode: str = ..., + *, + format: Optional[int] = ..., + check: int = ..., + preset: Optional[int] = ..., + filters: Optional[_FilterChain] = ..., + ) -> None: ... def close(self) -> None: ... @property def closed(self) -> bool: ... @@ -90,17 +100,73 @@ class LZMAFile(io.BufferedIOBase, IO[bytes]): # type: ignore # python/mypy#502 def seek(self, offset: int, whence: int = ...) -> int: ... def tell(self) -> int: ... - -def open(filename: _PathOrFile, - mode: str = ..., - *, - format: Optional[int] = ..., - check: int = ..., - preset: Optional[int] = ..., - filters: Optional[_FilterChain] = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ...) -> IO[Any]: ... -def compress(data: bytes, format: int = ..., check: int = ..., preset: Optional[int] = ..., filters: Optional[_FilterChain] = ...) -> bytes: ... +@overload +def open( + filename: _PathOrFile, + mode: Literal["r", "rb"] = ..., + *, + format: Optional[int] = ..., + check: Literal[-1] = ..., + preset: None = ..., + filters: Optional[_FilterChain] = ..., + encoding: None = ..., + errors: None = ..., + newline: None = ..., +) -> LZMAFile: ... +@overload +def open( + filename: _PathOrFile, + mode: _OPEN_BINARY_WRITING_MODE, + *, + format: Optional[int] = ..., + check: int = ..., + preset: Optional[int] = ..., + filters: Optional[_FilterChain] = ..., + encoding: None = ..., + errors: None = ..., + newline: None = ..., +) -> LZMAFile: ... +@overload +def open( + filename: _PathType, + mode: Literal["rt"], + *, + format: Optional[int] = ..., + check: Literal[-1] = ..., + preset: None = ..., + filters: Optional[_FilterChain] = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., +) -> TextIO: ... +@overload +def open( + filename: _PathType, + mode: _OPEN_TEXT_WRITING_MODE, + *, + format: Optional[int] = ..., + check: int = ..., + preset: Optional[int] = ..., + filters: Optional[_FilterChain] = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., +) -> TextIO: ... +@overload +def open( + filename: _PathOrFile, + mode: str = ..., + *, + format: Optional[int] = ..., + check: int = ..., + preset: Optional[int] = ..., + filters: Optional[_FilterChain] = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., +) -> Union[LZMAFile, TextIO]: ... +def compress( + data: bytes, format: int = ..., check: int = ..., preset: Optional[int] = ..., filters: Optional[_FilterChain] = ... +) -> bytes: ... def decompress(data: bytes, format: int = ..., memlimit: Optional[int] = ..., filters: Optional[_FilterChain] = ...) -> bytes: ... def is_check_supported(check: int) -> bool: ... From 952dc90761e599aafb5f48cfd0d35e9f0a73379e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ila=C3=AF=20Deutel?= Date: Wed, 16 Oct 2019 00:18:58 -0700 Subject: [PATCH 4/9] Apply black partially to builtin.pyi --- stdlib/2/__builtin__.pyi | 89 +++++++++++++++++++++++++++++---------- stdlib/2and3/builtins.pyi | 89 +++++++++++++++++++++++++++++---------- 2 files changed, 134 insertions(+), 44 deletions(-) diff --git a/stdlib/2/__builtin__.pyi b/stdlib/2/__builtin__.pyi index 985c1b208bda..d8d4c23e9b5b 100644 --- a/stdlib/2/__builtin__.pyi +++ b/stdlib/2/__builtin__.pyi @@ -1335,14 +1335,14 @@ def oct(__i: Union[int, _SupportsIndex]) -> str: ... if sys.version_info >= (3, 3): # Changed in version 3.3: The 'x' mode was added. - _OPEN_TEXT_MODE = Literal[ + _OPEN_TEXT_MODE = Literal[ 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', 'x', 'x+', '+x', 'xt', 'tx', 'xt+', 'x+t', '+xt', 'tx+', 't+x', '+tx', 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', ] - _OPEN_BINARY_MODE = Literal[ + _OPEN_BINARY_MODE = Literal[ 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', @@ -1350,13 +1350,13 @@ if sys.version_info >= (3, 3): 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', ] else: - _OPEN_TEXT_MODE = Literal[ + _OPEN_TEXT_MODE = Literal[ 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', ] - _OPEN_BINARY_MODE = Literal[ + _OPEN_BINARY_MODE = Literal[ 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', @@ -1374,26 +1374,71 @@ else: if sys.version_info >= (3, 3): # Changed in version 3.3: The opener parameter was added. @overload - def open(file: _OPEN_FILE, mode: _OPEN_TEXT_MODE = ..., buffering: int = ..., encoding: Optional[str] = ..., - errors: Optional[str] = ..., newline: Optional[str] = ..., closefd: bool = ..., - opener: Optional[Callable[[str, int], int]] = ...) -> TextIO: ... - @overload - def open(file: _OPEN_FILE, mode: _OPEN_BINARY_MODE, buffering: int = ..., encoding: None = ..., errors: None = ..., - newline: None = ..., closefd: bool = ..., opener: Optional[Callable[[str, int], int]] = ...) -> BinaryIO: ... - @overload - def open(file: _OPEN_FILE, mode: str, buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., - newline: Optional[str] = ..., closefd: bool = ..., opener: Optional[Callable[[str, int], int]] = ...) \ - -> IO[Any]: ... + def open( + file: _OPEN_FILE, + mode: _OPEN_TEXT_MODE = ..., + buffering: int = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., + closefd: bool = ..., + opener: Optional[Callable[[str, int], int]] = ..., + ) -> TextIO: ... + @overload + def open( + file: _OPEN_FILE, + mode: _OPEN_BINARY_MODE, + buffering: int = ..., + encoding: None = ..., + errors: None = ..., + newline: None = ..., + closefd: bool = ..., + opener: Optional[Callable[[str, int], int]] = ..., + ) -> BinaryIO: ... + @overload + def open( + file: _OPEN_FILE, + mode: str, + buffering: int = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., + closefd: bool = ..., + opener: Optional[Callable[[str, int], int]] = ..., + ) -> IO[Any]: ... + elif sys.version_info >= (3,): @overload - def open(file: _OPEN_FILE, mode: _OPEN_TEXT_MODE = ..., buffering: int = ..., encoding: Optional[str] = ..., - errors: Optional[str] = ..., newline: Optional[str] = ..., closefd: bool = ...) -> TextIO: ... - @overload - def open(file: _OPEN_FILE, mode: _OPEN_BINARY_MODE, buffering: int = ..., encoding: None = ..., errors: None = ..., - newline: None = ..., closefd: bool = ...) -> BinaryIO: ... - @overload - def open(file: _OPEN_FILE, mode: str, buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., - newline: Optional[str] = ..., closefd: bool = ...) -> IO[Any]: ... + def open( + file: _OPEN_FILE, + mode: _OPEN_TEXT_MODE = ..., + buffering: int = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., + closefd: bool = ..., + ) -> TextIO: ... + @overload + def open( + file: _OPEN_FILE, + mode: _OPEN_BINARY_MODE, + buffering: int = ..., + encoding: None = ..., + errors: None = ..., + newline: None = ..., + closefd: bool = ..., + ) -> BinaryIO: ... + @overload + def open( + file: _OPEN_FILE, + mode: str, + buffering: int = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., + closefd: bool = ..., + ) -> IO[Any]: ... + else: @overload def open(file: _OPEN_FILE, mode: _OPEN_TEXT_MODE = ..., buffering: int = ...) -> TextIO: ... diff --git a/stdlib/2and3/builtins.pyi b/stdlib/2and3/builtins.pyi index 985c1b208bda..d8d4c23e9b5b 100644 --- a/stdlib/2and3/builtins.pyi +++ b/stdlib/2and3/builtins.pyi @@ -1335,14 +1335,14 @@ def oct(__i: Union[int, _SupportsIndex]) -> str: ... if sys.version_info >= (3, 3): # Changed in version 3.3: The 'x' mode was added. - _OPEN_TEXT_MODE = Literal[ + _OPEN_TEXT_MODE = Literal[ 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', 'x', 'x+', '+x', 'xt', 'tx', 'xt+', 'x+t', '+xt', 'tx+', 't+x', '+tx', 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', ] - _OPEN_BINARY_MODE = Literal[ + _OPEN_BINARY_MODE = Literal[ 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', @@ -1350,13 +1350,13 @@ if sys.version_info >= (3, 3): 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', ] else: - _OPEN_TEXT_MODE = Literal[ + _OPEN_TEXT_MODE = Literal[ 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', ] - _OPEN_BINARY_MODE = Literal[ + _OPEN_BINARY_MODE = Literal[ 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', @@ -1374,26 +1374,71 @@ else: if sys.version_info >= (3, 3): # Changed in version 3.3: The opener parameter was added. @overload - def open(file: _OPEN_FILE, mode: _OPEN_TEXT_MODE = ..., buffering: int = ..., encoding: Optional[str] = ..., - errors: Optional[str] = ..., newline: Optional[str] = ..., closefd: bool = ..., - opener: Optional[Callable[[str, int], int]] = ...) -> TextIO: ... - @overload - def open(file: _OPEN_FILE, mode: _OPEN_BINARY_MODE, buffering: int = ..., encoding: None = ..., errors: None = ..., - newline: None = ..., closefd: bool = ..., opener: Optional[Callable[[str, int], int]] = ...) -> BinaryIO: ... - @overload - def open(file: _OPEN_FILE, mode: str, buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., - newline: Optional[str] = ..., closefd: bool = ..., opener: Optional[Callable[[str, int], int]] = ...) \ - -> IO[Any]: ... + def open( + file: _OPEN_FILE, + mode: _OPEN_TEXT_MODE = ..., + buffering: int = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., + closefd: bool = ..., + opener: Optional[Callable[[str, int], int]] = ..., + ) -> TextIO: ... + @overload + def open( + file: _OPEN_FILE, + mode: _OPEN_BINARY_MODE, + buffering: int = ..., + encoding: None = ..., + errors: None = ..., + newline: None = ..., + closefd: bool = ..., + opener: Optional[Callable[[str, int], int]] = ..., + ) -> BinaryIO: ... + @overload + def open( + file: _OPEN_FILE, + mode: str, + buffering: int = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., + closefd: bool = ..., + opener: Optional[Callable[[str, int], int]] = ..., + ) -> IO[Any]: ... + elif sys.version_info >= (3,): @overload - def open(file: _OPEN_FILE, mode: _OPEN_TEXT_MODE = ..., buffering: int = ..., encoding: Optional[str] = ..., - errors: Optional[str] = ..., newline: Optional[str] = ..., closefd: bool = ...) -> TextIO: ... - @overload - def open(file: _OPEN_FILE, mode: _OPEN_BINARY_MODE, buffering: int = ..., encoding: None = ..., errors: None = ..., - newline: None = ..., closefd: bool = ...) -> BinaryIO: ... - @overload - def open(file: _OPEN_FILE, mode: str, buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., - newline: Optional[str] = ..., closefd: bool = ...) -> IO[Any]: ... + def open( + file: _OPEN_FILE, + mode: _OPEN_TEXT_MODE = ..., + buffering: int = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., + closefd: bool = ..., + ) -> TextIO: ... + @overload + def open( + file: _OPEN_FILE, + mode: _OPEN_BINARY_MODE, + buffering: int = ..., + encoding: None = ..., + errors: None = ..., + newline: None = ..., + closefd: bool = ..., + ) -> BinaryIO: ... + @overload + def open( + file: _OPEN_FILE, + mode: str, + buffering: int = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., + closefd: bool = ..., + ) -> IO[Any]: ... + else: @overload def open(file: _OPEN_FILE, mode: _OPEN_TEXT_MODE = ..., buffering: int = ...) -> TextIO: ... From 0335f3fe483dff43f477623da1b82be1de15c53d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ila=C3=AF=20Deutel?= Date: Wed, 16 Oct 2019 00:55:16 -0700 Subject: [PATCH 5/9] Fix pytype exceptions --- stdlib/3/gzip.pyi | 2 +- stdlib/3/pathlib.pyi | 36 +++++++++++++++++++++++++++++++++++- third_party/2/pathlib2.pyi | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/stdlib/3/gzip.pyi b/stdlib/3/gzip.pyi index 24f08e7f6d8b..b451338306f9 100644 --- a/stdlib/3/gzip.pyi +++ b/stdlib/3/gzip.pyi @@ -1,7 +1,7 @@ import sys import zlib from os.path import _PathType -from typing import IO, BinaryIO, Optional, TextIO, Union, overload +from typing import IO, Optional, TextIO, Union, overload import _compression diff --git a/stdlib/3/pathlib.pyi b/stdlib/3/pathlib.pyi index ddd02441afef..cc31f4e6a2da 100644 --- a/stdlib/3/pathlib.pyi +++ b/stdlib/3/pathlib.pyi @@ -1,10 +1,14 @@ -from builtins import _OPEN_BINARY_MODE, _OPEN_TEXT_MODE from typing import (Any, BinaryIO, Generator, IO, List, Optional, Sequence, TextIO, Tuple, Type, TypeVar, Union, overload) from types import TracebackType import os import sys +if sys.version_info >= (3, 8): + from typing import Literal +else: + from typing_extensions import Literal + _P = TypeVar('_P', bound=PurePath) if sys.version_info >= (3, 6): @@ -12,6 +16,36 @@ if sys.version_info >= (3, 6): else: _PurePathBase = object +if sys.version_info >= (3, 3): + # Changed in version 3.3: The 'x' mode was added. + _OPEN_TEXT_MODE = Literal[ + 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', + 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', + 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', + 'x', 'x+', '+x', 'xt', 'tx', 'xt+', 'x+t', '+xt', 'tx+', 't+x', '+tx', + 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', + ] + _OPEN_BINARY_MODE = Literal[ + 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', + 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', + 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', + 'xb', 'bx', 'xb+', 'x+b', '+xb', 'bx+', 'b+x', '+bx', + 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', + ] +else: + _OPEN_TEXT_MODE = Literal[ + 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', + 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', + 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', + 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', + ] + _OPEN_BINARY_MODE = Literal[ + 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', + 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', + 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', + 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', + ] + class PurePath(_PurePathBase): parts: Tuple[str, ...] drive: str diff --git a/third_party/2/pathlib2.pyi b/third_party/2/pathlib2.pyi index ddd02441afef..cc31f4e6a2da 100644 --- a/third_party/2/pathlib2.pyi +++ b/third_party/2/pathlib2.pyi @@ -1,10 +1,14 @@ -from builtins import _OPEN_BINARY_MODE, _OPEN_TEXT_MODE from typing import (Any, BinaryIO, Generator, IO, List, Optional, Sequence, TextIO, Tuple, Type, TypeVar, Union, overload) from types import TracebackType import os import sys +if sys.version_info >= (3, 8): + from typing import Literal +else: + from typing_extensions import Literal + _P = TypeVar('_P', bound=PurePath) if sys.version_info >= (3, 6): @@ -12,6 +16,36 @@ if sys.version_info >= (3, 6): else: _PurePathBase = object +if sys.version_info >= (3, 3): + # Changed in version 3.3: The 'x' mode was added. + _OPEN_TEXT_MODE = Literal[ + 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', + 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', + 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', + 'x', 'x+', '+x', 'xt', 'tx', 'xt+', 'x+t', '+xt', 'tx+', 't+x', '+tx', + 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', + ] + _OPEN_BINARY_MODE = Literal[ + 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', + 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', + 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', + 'xb', 'bx', 'xb+', 'x+b', '+xb', 'bx+', 'b+x', '+bx', + 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', + ] +else: + _OPEN_TEXT_MODE = Literal[ + 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', + 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', + 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', + 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', + ] + _OPEN_BINARY_MODE = Literal[ + 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', + 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', + 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', + 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', + ] + class PurePath(_PurePathBase): parts: Tuple[str, ...] drive: str From 38e04feee888fd58dd7b21d36b130c50612358f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ila=C3=AF=20Deutel?= Date: Thu, 17 Oct 2019 00:23:12 -0700 Subject: [PATCH 6/9] Move _OpenBinaryMode and _OpenTextMode to io; refactor code --- stdlib/2/__builtin__.pyi | 86 ++++++-------------------------------- stdlib/2/io.pyi | 14 +++++++ stdlib/2and3/builtins.pyi | 86 ++++++-------------------------------- stdlib/2and3/bz2.pyi | 14 +++---- stdlib/3/gzip.pyi | 73 +++++++++++++++----------------- stdlib/3/io.pyi | 20 +++++++++ stdlib/3/lzma.pyi | 14 +++---- stdlib/3/pathlib.pyi | 42 ++----------------- third_party/2/pathlib2.pyi | 42 ++----------------- 9 files changed, 114 insertions(+), 277 deletions(-) diff --git a/stdlib/2/__builtin__.pyi b/stdlib/2/__builtin__.pyi index d8d4c23e9b5b..c17b89849742 100644 --- a/stdlib/2/__builtin__.pyi +++ b/stdlib/2/__builtin__.pyi @@ -11,6 +11,7 @@ from typing import ( ) from abc import abstractmethod, ABCMeta from ast import mod, AST +from io import _OpenBinaryMode, _OpenTextMode from types import TracebackType, CodeType, ModuleType import sys @@ -1333,50 +1334,19 @@ def next(__i: Iterator[_T]) -> _T: ... def next(__i: Iterator[_T], default: _VT) -> Union[_T, _VT]: ... def oct(__i: Union[int, _SupportsIndex]) -> str: ... -if sys.version_info >= (3, 3): - # Changed in version 3.3: The 'x' mode was added. - _OPEN_TEXT_MODE = Literal[ - 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', - 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', - 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', - 'x', 'x+', '+x', 'xt', 'tx', 'xt+', 'x+t', '+xt', 'tx+', 't+x', '+tx', - 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', - ] - _OPEN_BINARY_MODE = Literal[ - 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', - 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', - 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', - 'xb', 'bx', 'xb+', 'x+b', '+xb', 'bx+', 'b+x', '+bx', - 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', - ] -else: - _OPEN_TEXT_MODE = Literal[ - 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', - 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', - 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', - 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', - ] - _OPEN_BINARY_MODE = Literal[ - 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', - 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', - 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', - 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', - ] - if sys.version_info >= (3, 6): # Changed in version 3.6: Support added to accept objects implementing os.PathLike. - _OPEN_FILE = Union[str, bytes, int, _PathLike[Any]] + _OpenFile = Union[str, bytes, int, _PathLike[Any]] elif sys.version_info >= (3,): - _OPEN_FILE = Union[str, bytes, int] + _OpenFile = Union[str, bytes, int] else: - _OPEN_FILE = Union[unicode, int] + _OpenFile = Union[unicode, int] if sys.version_info >= (3, 3): - # Changed in version 3.3: The opener parameter was added. @overload def open( - file: _OPEN_FILE, - mode: _OPEN_TEXT_MODE = ..., + file: _OpenFile, + mode: _OpenTextMode = ..., buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., @@ -1386,8 +1356,8 @@ if sys.version_info >= (3, 3): ) -> TextIO: ... @overload def open( - file: _OPEN_FILE, - mode: _OPEN_BINARY_MODE, + file: _OpenFile, + mode: _OpenBinaryMode, buffering: int = ..., encoding: None = ..., errors: None = ..., @@ -1397,7 +1367,7 @@ if sys.version_info >= (3, 3): ) -> BinaryIO: ... @overload def open( - file: _OPEN_FILE, + file: _OpenFile, mode: str, buffering: int = ..., encoding: Optional[str] = ..., @@ -1407,45 +1377,13 @@ if sys.version_info >= (3, 3): opener: Optional[Callable[[str, int], int]] = ..., ) -> IO[Any]: ... -elif sys.version_info >= (3,): - @overload - def open( - file: _OPEN_FILE, - mode: _OPEN_TEXT_MODE = ..., - buffering: int = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., - closefd: bool = ..., - ) -> TextIO: ... - @overload - def open( - file: _OPEN_FILE, - mode: _OPEN_BINARY_MODE, - buffering: int = ..., - encoding: None = ..., - errors: None = ..., - newline: None = ..., - closefd: bool = ..., - ) -> BinaryIO: ... - @overload - def open( - file: _OPEN_FILE, - mode: str, - buffering: int = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., - closefd: bool = ..., - ) -> IO[Any]: ... - else: @overload - def open(file: _OPEN_FILE, mode: _OPEN_TEXT_MODE = ..., buffering: int = ...) -> TextIO: ... + def open(file: _OpenFile, mode: _OpenTextMode = ..., buffering: int = ...) -> TextIO: ... @overload - def open(file: _OPEN_FILE, mode: _OPEN_BINARY_MODE, buffering: int = ...) -> BinaryIO: ... + def open(file: _OpenFile, mode: _OpenBinaryMode, buffering: int = ...) -> BinaryIO: ... @overload - def open(file: _OPEN_FILE, mode: unicode, buffering: int = ...) -> IO[Any]: ... + def open(file: _OpenFile, mode: unicode, buffering: int = ...) -> IO[Any]: ... def ord(__c: Union[Text, bytes]) -> int: ... if sys.version_info >= (3,): diff --git a/stdlib/2/io.pyi b/stdlib/2/io.pyi index 1ab6b9069505..0626df42778f 100644 --- a/stdlib/2/io.pyi +++ b/stdlib/2/io.pyi @@ -5,6 +5,7 @@ # Only a subset of functionality is included. from typing import List, BinaryIO, TextIO, IO, overload, Iterator, Iterable, Any, Union, Optional +from typing_extensions import Literal import _io from _io import BlockingIOError as BlockingIOError @@ -21,6 +22,19 @@ from _io import TextIOWrapper as TextIOWrapper from _io import UnsupportedOperation as UnsupportedOperation from _io import open as open +_OpenTextMode = Literal[ + 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', + 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', + 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', + 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', +] +_OpenBinaryMode = Literal[ + 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', + 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', + 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', + 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', +] + def _OpenWrapper(file: Union[str, unicode, int], mode: unicode = ..., buffering: int = ..., encoding: unicode = ..., errors: unicode = ..., newline: unicode = ..., diff --git a/stdlib/2and3/builtins.pyi b/stdlib/2and3/builtins.pyi index d8d4c23e9b5b..c17b89849742 100644 --- a/stdlib/2and3/builtins.pyi +++ b/stdlib/2and3/builtins.pyi @@ -11,6 +11,7 @@ from typing import ( ) from abc import abstractmethod, ABCMeta from ast import mod, AST +from io import _OpenBinaryMode, _OpenTextMode from types import TracebackType, CodeType, ModuleType import sys @@ -1333,50 +1334,19 @@ def next(__i: Iterator[_T]) -> _T: ... def next(__i: Iterator[_T], default: _VT) -> Union[_T, _VT]: ... def oct(__i: Union[int, _SupportsIndex]) -> str: ... -if sys.version_info >= (3, 3): - # Changed in version 3.3: The 'x' mode was added. - _OPEN_TEXT_MODE = Literal[ - 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', - 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', - 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', - 'x', 'x+', '+x', 'xt', 'tx', 'xt+', 'x+t', '+xt', 'tx+', 't+x', '+tx', - 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', - ] - _OPEN_BINARY_MODE = Literal[ - 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', - 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', - 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', - 'xb', 'bx', 'xb+', 'x+b', '+xb', 'bx+', 'b+x', '+bx', - 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', - ] -else: - _OPEN_TEXT_MODE = Literal[ - 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', - 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', - 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', - 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', - ] - _OPEN_BINARY_MODE = Literal[ - 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', - 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', - 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', - 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', - ] - if sys.version_info >= (3, 6): # Changed in version 3.6: Support added to accept objects implementing os.PathLike. - _OPEN_FILE = Union[str, bytes, int, _PathLike[Any]] + _OpenFile = Union[str, bytes, int, _PathLike[Any]] elif sys.version_info >= (3,): - _OPEN_FILE = Union[str, bytes, int] + _OpenFile = Union[str, bytes, int] else: - _OPEN_FILE = Union[unicode, int] + _OpenFile = Union[unicode, int] if sys.version_info >= (3, 3): - # Changed in version 3.3: The opener parameter was added. @overload def open( - file: _OPEN_FILE, - mode: _OPEN_TEXT_MODE = ..., + file: _OpenFile, + mode: _OpenTextMode = ..., buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., @@ -1386,8 +1356,8 @@ if sys.version_info >= (3, 3): ) -> TextIO: ... @overload def open( - file: _OPEN_FILE, - mode: _OPEN_BINARY_MODE, + file: _OpenFile, + mode: _OpenBinaryMode, buffering: int = ..., encoding: None = ..., errors: None = ..., @@ -1397,7 +1367,7 @@ if sys.version_info >= (3, 3): ) -> BinaryIO: ... @overload def open( - file: _OPEN_FILE, + file: _OpenFile, mode: str, buffering: int = ..., encoding: Optional[str] = ..., @@ -1407,45 +1377,13 @@ if sys.version_info >= (3, 3): opener: Optional[Callable[[str, int], int]] = ..., ) -> IO[Any]: ... -elif sys.version_info >= (3,): - @overload - def open( - file: _OPEN_FILE, - mode: _OPEN_TEXT_MODE = ..., - buffering: int = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., - closefd: bool = ..., - ) -> TextIO: ... - @overload - def open( - file: _OPEN_FILE, - mode: _OPEN_BINARY_MODE, - buffering: int = ..., - encoding: None = ..., - errors: None = ..., - newline: None = ..., - closefd: bool = ..., - ) -> BinaryIO: ... - @overload - def open( - file: _OPEN_FILE, - mode: str, - buffering: int = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., - closefd: bool = ..., - ) -> IO[Any]: ... - else: @overload - def open(file: _OPEN_FILE, mode: _OPEN_TEXT_MODE = ..., buffering: int = ...) -> TextIO: ... + def open(file: _OpenFile, mode: _OpenTextMode = ..., buffering: int = ...) -> TextIO: ... @overload - def open(file: _OPEN_FILE, mode: _OPEN_BINARY_MODE, buffering: int = ...) -> BinaryIO: ... + def open(file: _OpenFile, mode: _OpenBinaryMode, buffering: int = ...) -> BinaryIO: ... @overload - def open(file: _OPEN_FILE, mode: unicode, buffering: int = ...) -> IO[Any]: ... + def open(file: _OpenFile, mode: unicode, buffering: int = ...) -> IO[Any]: ... def ord(__c: Union[Text, bytes]) -> int: ... if sys.version_info >= (3,): diff --git a/stdlib/2and3/bz2.pyi b/stdlib/2and3/bz2.pyi index 480f67702d0d..928fcefa65fc 100644 --- a/stdlib/2and3/bz2.pyi +++ b/stdlib/2and3/bz2.pyi @@ -16,15 +16,15 @@ def decompress(data: bytes) -> bytes: ... if sys.version_info >= (3, 3): if sys.version_info >= (3, 4): # Changed in version 3.4: The 'x' (exclusive creation) mode was added. - _OPEN_BINARY_MODE = Literal["r", "rb", "w", "wb", "x", "xb", "a", "ab"] - _OPEN_TEXT_MODE = Literal["rt", "wt", "xt", "at"] + _OpenBinaryMode = Literal["r", "rb", "w", "wb", "x", "xb", "a", "ab"] + _OpenTextMode = Literal["rt", "wt", "xt", "at"] else: - _OPEN_BINARY_MODE = Literal["r", "rb", "w", "wb", "a", "ab"] - _OPEN_TEXT_MODE = Literal["rt", "wt", "at"] + _OpenBinaryMode = Literal["r", "rb", "w", "wb", "a", "ab"] + _OpenTextMode = Literal["rt", "wt", "at"] @overload def open( filename: _PathOrFile, - mode: _OPEN_BINARY_MODE = ..., + mode: _OpenBinaryMode = ..., compresslevel: int = ..., encoding: None = ..., errors: None = ..., @@ -33,7 +33,7 @@ if sys.version_info >= (3, 3): @overload def open( filename: _PathType, - mode: _OPEN_TEXT_MODE, + mode: _OpenTextMode, compresslevel: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., @@ -42,7 +42,7 @@ if sys.version_info >= (3, 3): @overload def open( filename: _PathOrFile, - mode: str = ..., + mode: str, compresslevel: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., diff --git a/stdlib/3/gzip.pyi b/stdlib/3/gzip.pyi index b451338306f9..69027ad1aed5 100644 --- a/stdlib/3/gzip.pyi +++ b/stdlib/3/gzip.pyi @@ -10,46 +10,41 @@ if sys.version_info >= (3, 8): else: from typing_extensions import Literal -if sys.version_info >= (3, 3): - # Changed in version 3.3: Added support for filename being a file object, support for text mode, and the encoding, errors and newline arguments. - - if sys.version_info >= (3, 4): - # Changed in version 3.4: Added support for the 'x', 'xb' and 'xt' modes. - _OPEN_BINARY_MODE = Literal["r", "rb", "a", "ab", "w", "wb", "x", "xb"] - _OPEN_TEXT_MODE = Literal["rt", "at", "wt", "xt"] - else: - _OPEN_BINARY_MODE = Literal["r", "rb", "a", "ab", "w", "wb"] - _OPEN_TEXT_MODE = Literal["rt", "at", "wt"] - @overload - def open( - filename: Union[_PathType, IO[bytes]], - mode: _OPEN_BINARY_MODE = ..., - compresslevel: int = ..., - encoding: None = ..., - errors: None = ..., - newline: None = ..., - ) -> GzipFile: ... - @overload - def open( - filename: _PathType, - mode: _OPEN_TEXT_MODE, - compresslevel: int = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., - ) -> TextIO: ... - @overload - def open( - filename: Union[_PathType, IO[bytes]], - mode: str = ..., - compresslevel: int = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., - ) -> Union[GzipFile, TextIO]: ... - +if sys.version_info >= (3, 4): + # Changed in version 3.4: Added support for the 'x', 'xb' and 'xt' modes. + _OpenBinaryMode = Literal["r", "rb", "a", "ab", "w", "wb", "x", "xb"] + _OpenTextMode = Literal["rt", "at", "wt", "xt"] else: - def open(filename: _PathType, mode: str = ..., compresslevel: int = ...) -> GzipFile: ... + _OpenBinaryMode = Literal["r", "rb", "a", "ab", "w", "wb"] + _OpenTextMode = Literal["rt", "at", "wt"] + +@overload +def open( + filename: Union[_PathType, IO[bytes]], + mode: _OpenBinaryMode = ..., + compresslevel: int = ..., + encoding: None = ..., + errors: None = ..., + newline: None = ..., +) -> GzipFile: ... +@overload +def open( + filename: _PathType, + mode: _OpenTextMode, + compresslevel: int = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., +) -> TextIO: ... +@overload +def open( + filename: Union[_PathType, IO[bytes]], + mode: str, + compresslevel: int = ..., + encoding: Optional[str] = ..., + errors: Optional[str] = ..., + newline: Optional[str] = ..., +) -> Union[GzipFile, TextIO]: ... class _PaddedFile: file: IO[bytes] diff --git a/stdlib/3/io.pyi b/stdlib/3/io.pyi index d3cc6948b499..22b0564cba8d 100644 --- a/stdlib/3/io.pyi +++ b/stdlib/3/io.pyi @@ -7,6 +7,11 @@ from mmap import mmap from types import TracebackType from typing import TypeVar +if sys.version_info >= (3, 8): + from typing import Literal +else: + from typing_extensions import Literal + _bytearray_like = Union[bytearray, mmap] DEFAULT_BUFFER_SIZE: int @@ -17,6 +22,21 @@ SEEK_END: int _T = TypeVar('_T', bound=IOBase) +_OpenTextMode = Literal[ + 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', + 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', + 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', + 'x', 'x+', '+x', 'xt', 'tx', 'xt+', 'x+t', '+xt', 'tx+', 't+x', '+tx', + 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', +] +_OpenBinaryMode = Literal[ + 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', + 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', + 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', + 'xb', 'bx', 'xb+', 'x+b', '+xb', 'bx+', 'b+x', '+bx', + 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', +] + open = builtins.open BlockingIOError = builtins.BlockingIOError diff --git a/stdlib/3/lzma.pyi b/stdlib/3/lzma.pyi index ef86c573984b..ca3975b53e9c 100644 --- a/stdlib/3/lzma.pyi +++ b/stdlib/3/lzma.pyi @@ -10,11 +10,11 @@ else: if sys.version_info >= (3, 4): # Changed in version 3.4: Added support for the "x", "xb" and "xt" modes. - _OPEN_BINARY_WRITING_MODE = Literal["w", "wb", "x", "xb", "a", "ab"] - _OPEN_TEXT_WRITING_MODE = Literal["wt", "xt", "at"] + _OpenBinaryWritingMode = Literal["w", "wb", "x", "xb", "a", "ab"] + _OpenTextWritingMode = Literal["wt", "xt", "at"] else: - _OPEN_BINARY_WRITING_MODE = Literal["w", "wb", "a", "ab"] - _OPEN_TEXT_WRITING_MODE = Literal["wt", "at"] + _OpenBinaryWritingMode = Literal["w", "wb", "a", "ab"] + _OpenTextWritingMode = Literal["wt", "at"] _PathOrFile = Union[_PathType, IO[bytes]] @@ -116,7 +116,7 @@ def open( @overload def open( filename: _PathOrFile, - mode: _OPEN_BINARY_WRITING_MODE, + mode: _OpenBinaryWritingMode, *, format: Optional[int] = ..., check: int = ..., @@ -142,7 +142,7 @@ def open( @overload def open( filename: _PathType, - mode: _OPEN_TEXT_WRITING_MODE, + mode: _OpenTextWritingMode, *, format: Optional[int] = ..., check: int = ..., @@ -155,7 +155,7 @@ def open( @overload def open( filename: _PathOrFile, - mode: str = ..., + mode: str, *, format: Optional[int] = ..., check: int = ..., diff --git a/stdlib/3/pathlib.pyi b/stdlib/3/pathlib.pyi index cc31f4e6a2da..d46639cc40c0 100644 --- a/stdlib/3/pathlib.pyi +++ b/stdlib/3/pathlib.pyi @@ -1,14 +1,10 @@ +from io import _OpenBinaryMode, _OpenTextMode from typing import (Any, BinaryIO, Generator, IO, List, Optional, Sequence, TextIO, Tuple, Type, TypeVar, Union, overload) from types import TracebackType import os import sys -if sys.version_info >= (3, 8): - from typing import Literal -else: - from typing_extensions import Literal - _P = TypeVar('_P', bound=PurePath) if sys.version_info >= (3, 6): @@ -16,36 +12,6 @@ if sys.version_info >= (3, 6): else: _PurePathBase = object -if sys.version_info >= (3, 3): - # Changed in version 3.3: The 'x' mode was added. - _OPEN_TEXT_MODE = Literal[ - 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', - 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', - 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', - 'x', 'x+', '+x', 'xt', 'tx', 'xt+', 'x+t', '+xt', 'tx+', 't+x', '+tx', - 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', - ] - _OPEN_BINARY_MODE = Literal[ - 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', - 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', - 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', - 'xb', 'bx', 'xb+', 'x+b', '+xb', 'bx+', 'b+x', '+bx', - 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', - ] -else: - _OPEN_TEXT_MODE = Literal[ - 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', - 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', - 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', - 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', - ] - _OPEN_BINARY_MODE = Literal[ - 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', - 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', - 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', - 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', - ] - class PurePath(_PurePathBase): parts: Tuple[str, ...] drive: str @@ -128,13 +94,13 @@ class Path(PurePath): def mkdir(self, mode: int = ..., parents: bool = ..., exist_ok: bool = ...) -> None: ... @overload - def open(self, mode: _OPEN_TEXT_MODE = ..., buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., + def open(self, mode: _OpenTextMode = ..., buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., newline: Optional[str] = ...) -> TextIO: ... @overload - def open(self, mode: _OPEN_BINARY_MODE, buffering: int = ..., encoding: None = ..., errors: None = ..., + def open(self, mode: _OpenBinaryMode, buffering: int = ..., encoding: None = ..., errors: None = ..., newline: None = ...) -> BinaryIO: ... @overload - def open(self, mode: str = ..., buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., + def open(self, mode: str, buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., newline: Optional[str] = ...) -> IO[Any]: ... def owner(self) -> str: ... def rename(self, target: Union[str, PurePath]) -> None: ... diff --git a/third_party/2/pathlib2.pyi b/third_party/2/pathlib2.pyi index cc31f4e6a2da..d46639cc40c0 100644 --- a/third_party/2/pathlib2.pyi +++ b/third_party/2/pathlib2.pyi @@ -1,14 +1,10 @@ +from io import _OpenBinaryMode, _OpenTextMode from typing import (Any, BinaryIO, Generator, IO, List, Optional, Sequence, TextIO, Tuple, Type, TypeVar, Union, overload) from types import TracebackType import os import sys -if sys.version_info >= (3, 8): - from typing import Literal -else: - from typing_extensions import Literal - _P = TypeVar('_P', bound=PurePath) if sys.version_info >= (3, 6): @@ -16,36 +12,6 @@ if sys.version_info >= (3, 6): else: _PurePathBase = object -if sys.version_info >= (3, 3): - # Changed in version 3.3: The 'x' mode was added. - _OPEN_TEXT_MODE = Literal[ - 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', - 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', - 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', - 'x', 'x+', '+x', 'xt', 'tx', 'xt+', 'x+t', '+xt', 'tx+', 't+x', '+tx', - 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', - ] - _OPEN_BINARY_MODE = Literal[ - 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', - 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', - 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', - 'xb', 'bx', 'xb+', 'x+b', '+xb', 'bx+', 'b+x', '+bx', - 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', - ] -else: - _OPEN_TEXT_MODE = Literal[ - 'r', 'r+', '+r', 'rt', 'tr', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr', - 'w', 'w+', '+w', 'wt', 'tw', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw', - 'a', 'a+', '+a', 'at', 'ta', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta', - 'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr', - ] - _OPEN_BINARY_MODE = Literal[ - 'rb', 'br', 'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br', - 'wb', 'bw', 'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw', - 'ab', 'ba', 'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba', - 'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr', - ] - class PurePath(_PurePathBase): parts: Tuple[str, ...] drive: str @@ -128,13 +94,13 @@ class Path(PurePath): def mkdir(self, mode: int = ..., parents: bool = ..., exist_ok: bool = ...) -> None: ... @overload - def open(self, mode: _OPEN_TEXT_MODE = ..., buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., + def open(self, mode: _OpenTextMode = ..., buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., newline: Optional[str] = ...) -> TextIO: ... @overload - def open(self, mode: _OPEN_BINARY_MODE, buffering: int = ..., encoding: None = ..., errors: None = ..., + def open(self, mode: _OpenBinaryMode, buffering: int = ..., encoding: None = ..., errors: None = ..., newline: None = ...) -> BinaryIO: ... @overload - def open(self, mode: str = ..., buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., + def open(self, mode: str, buffering: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., newline: Optional[str] = ...) -> IO[Any]: ... def owner(self) -> str: ... def rename(self, target: Union[str, PurePath]) -> None: ... From 1619c9b39f45dcc58ae606f5fee77ad2f8c0313d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ila=C3=AF=20Deutel?= Date: Thu, 17 Oct 2019 01:10:47 -0700 Subject: [PATCH 7/9] Fix missing import in io --- stdlib/3/io.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/3/io.pyi b/stdlib/3/io.pyi index 22b0564cba8d..8455e7a5f4f7 100644 --- a/stdlib/3/io.pyi +++ b/stdlib/3/io.pyi @@ -3,6 +3,7 @@ from typing import ( ) import builtins import codecs +import sys from mmap import mmap from types import TracebackType from typing import TypeVar From 7455811ca5d644a01cdfbf250732a1c77d595ecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ila=C3=AF=20Deutel?= Date: Wed, 15 Jan 2020 09:36:03 -0800 Subject: [PATCH 8/9] open() always return BinaryIO in Python2 --- stdlib/2/__builtin__.pyi | 21 +++++++-------------- stdlib/2and3/builtins.pyi | 21 +++++++-------------- 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/stdlib/2/__builtin__.pyi b/stdlib/2/__builtin__.pyi index a54b1c072fe9..4f2dd9fb4478 100644 --- a/stdlib/2/__builtin__.pyi +++ b/stdlib/2/__builtin__.pyi @@ -1346,15 +1346,13 @@ def next(__i: Iterator[_T]) -> _T: ... def next(__i: Iterator[_T], default: _VT) -> Union[_T, _VT]: ... def oct(__i: Union[int, _SupportsIndex]) -> str: ... -if sys.version_info >= (3, 6): - # Changed in version 3.6: Support added to accept objects implementing os.PathLike. - _OpenFile = Union[str, bytes, int, _PathLike[Any]] -elif sys.version_info >= (3,): - _OpenFile = Union[str, bytes, int] -else: - _OpenFile = Union[unicode, int] +if sys.version_info >= (3,): + if sys.version_info >= (3, 6): + # Changed in version 3.6: Support added to accept objects implementing os.PathLike. + _OpenFile = Union[str, bytes, int, _PathLike[Any]] + else: + _OpenFile = Union[str, bytes, int] -if sys.version_info >= (3, 3): @overload def open( file: _OpenFile, @@ -1390,12 +1388,7 @@ if sys.version_info >= (3, 3): ) -> IO[Any]: ... else: - @overload - def open(file: _OpenFile, mode: _OpenTextMode = ..., buffering: int = ...) -> TextIO: ... - @overload - def open(file: _OpenFile, mode: _OpenBinaryMode, buffering: int = ...) -> BinaryIO: ... - @overload - def open(file: _OpenFile, mode: unicode, buffering: int = ...) -> IO[Any]: ... + def open(name: Union[unicode, int], mode: unicode = ..., buffering: int = ...) -> BinaryIO: ... def ord(__c: Union[Text, bytes]) -> int: ... if sys.version_info >= (3,): diff --git a/stdlib/2and3/builtins.pyi b/stdlib/2and3/builtins.pyi index a54b1c072fe9..4f2dd9fb4478 100644 --- a/stdlib/2and3/builtins.pyi +++ b/stdlib/2and3/builtins.pyi @@ -1346,15 +1346,13 @@ def next(__i: Iterator[_T]) -> _T: ... def next(__i: Iterator[_T], default: _VT) -> Union[_T, _VT]: ... def oct(__i: Union[int, _SupportsIndex]) -> str: ... -if sys.version_info >= (3, 6): - # Changed in version 3.6: Support added to accept objects implementing os.PathLike. - _OpenFile = Union[str, bytes, int, _PathLike[Any]] -elif sys.version_info >= (3,): - _OpenFile = Union[str, bytes, int] -else: - _OpenFile = Union[unicode, int] +if sys.version_info >= (3,): + if sys.version_info >= (3, 6): + # Changed in version 3.6: Support added to accept objects implementing os.PathLike. + _OpenFile = Union[str, bytes, int, _PathLike[Any]] + else: + _OpenFile = Union[str, bytes, int] -if sys.version_info >= (3, 3): @overload def open( file: _OpenFile, @@ -1390,12 +1388,7 @@ if sys.version_info >= (3, 3): ) -> IO[Any]: ... else: - @overload - def open(file: _OpenFile, mode: _OpenTextMode = ..., buffering: int = ...) -> TextIO: ... - @overload - def open(file: _OpenFile, mode: _OpenBinaryMode, buffering: int = ...) -> BinaryIO: ... - @overload - def open(file: _OpenFile, mode: unicode, buffering: int = ...) -> IO[Any]: ... + def open(name: Union[unicode, int], mode: unicode = ..., buffering: int = ...) -> BinaryIO: ... def ord(__c: Union[Text, bytes]) -> int: ... if sys.version_info >= (3,): From 7bc89fe5937fbef3564481b90a7f153137a39202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ila=C3=AF=20Deutel?= Date: Thu, 28 May 2020 08:41:07 -0700 Subject: [PATCH 9/9] Remove support for Python 3.4 Co-authored-by: Sebastian Rittau --- stdlib/2and3/bz2.pyi | 9 ++------- stdlib/3/gzip.pyi | 9 ++------- stdlib/3/lzma.pyi | 9 ++------- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/stdlib/2and3/bz2.pyi b/stdlib/2and3/bz2.pyi index 609fd88683fc..77658a793482 100644 --- a/stdlib/2and3/bz2.pyi +++ b/stdlib/2and3/bz2.pyi @@ -14,13 +14,8 @@ def compress(data: bytes, compresslevel: int = ...) -> bytes: ... def decompress(data: bytes) -> bytes: ... if sys.version_info >= (3, 3): - if sys.version_info >= (3, 4): - # Changed in version 3.4: The 'x' (exclusive creation) mode was added. - _OpenBinaryMode = Literal["r", "rb", "w", "wb", "x", "xb", "a", "ab"] - _OpenTextMode = Literal["rt", "wt", "xt", "at"] - else: - _OpenBinaryMode = Literal["r", "rb", "w", "wb", "a", "ab"] - _OpenTextMode = Literal["rt", "wt", "at"] + _OpenBinaryMode = Literal["r", "rb", "w", "wb", "x", "xb", "a", "ab"] + _OpenTextMode = Literal["rt", "wt", "xt", "at"] @overload def open( filename: _PathOrFile, diff --git a/stdlib/3/gzip.pyi b/stdlib/3/gzip.pyi index 69027ad1aed5..9357a71a2de9 100644 --- a/stdlib/3/gzip.pyi +++ b/stdlib/3/gzip.pyi @@ -10,13 +10,8 @@ if sys.version_info >= (3, 8): else: from typing_extensions import Literal -if sys.version_info >= (3, 4): - # Changed in version 3.4: Added support for the 'x', 'xb' and 'xt' modes. - _OpenBinaryMode = Literal["r", "rb", "a", "ab", "w", "wb", "x", "xb"] - _OpenTextMode = Literal["rt", "at", "wt", "xt"] -else: - _OpenBinaryMode = Literal["r", "rb", "a", "ab", "w", "wb"] - _OpenTextMode = Literal["rt", "at", "wt"] +_OpenBinaryMode = Literal["r", "rb", "a", "ab", "w", "wb", "x", "xb"] +_OpenTextMode = Literal["rt", "at", "wt", "xt"] @overload def open( diff --git a/stdlib/3/lzma.pyi b/stdlib/3/lzma.pyi index ca3975b53e9c..f192c43628b8 100644 --- a/stdlib/3/lzma.pyi +++ b/stdlib/3/lzma.pyi @@ -8,13 +8,8 @@ if sys.version_info >= (3, 8): else: from typing_extensions import Literal -if sys.version_info >= (3, 4): - # Changed in version 3.4: Added support for the "x", "xb" and "xt" modes. - _OpenBinaryWritingMode = Literal["w", "wb", "x", "xb", "a", "ab"] - _OpenTextWritingMode = Literal["wt", "xt", "at"] -else: - _OpenBinaryWritingMode = Literal["w", "wb", "a", "ab"] - _OpenTextWritingMode = Literal["wt", "at"] +_OpenBinaryWritingMode = Literal["w", "wb", "x", "xb", "a", "ab"] +_OpenTextWritingMode = Literal["wt", "xt", "at"] _PathOrFile = Union[_PathType, IO[bytes]]