From 53ad1a3aa24994b934c26a094189267807976d1e Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 16 May 2021 15:43:08 +0200 Subject: [PATCH 1/3] Replace an instance of IO with a protocol --- stdlib/zipfile.pyi | 59 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/stdlib/zipfile.pyi b/stdlib/zipfile.pyi index 23d5e3b45d31..425a367cb99a 100644 --- a/stdlib/zipfile.pyi +++ b/stdlib/zipfile.pyi @@ -2,7 +2,8 @@ import io import sys from _typeshed import StrPath from types import TracebackType -from typing import IO, Any, Callable, Dict, Iterable, Iterator, List, Optional, Protocol, Sequence, Tuple, Type, Union +from typing import IO, Any, Callable, Dict, Iterable, Iterator, List, Optional, Protocol, Sequence, Tuple, Type, Union, overload +from typing_extensions import Literal _DateTuple = Tuple[int, int, int, int, int, int] @@ -13,6 +14,16 @@ error = BadZipfile class LargeZipFile(Exception): ... +class _ZipStream(Protocol): + def read(self, __n: int) -> bytes: ... + # The following methods are optional: + # def seekable(self) -> bool: ... + # def tell(self) -> int: ... + # def seek(self, __n: int) -> Any: ... + +class _ClosableZipStream(_ZipStream, Protocol): + def close(self) -> Any: ... + class ZipExtFile(io.BufferedIOBase): MAX_N: int = ... MIN_READ_SIZE: int = ... @@ -24,17 +35,57 @@ class ZipExtFile(io.BufferedIOBase): mode: str name: str if sys.version_info >= (3, 7): + @overload + def __init__( + self, fileobj: _ClosableZipStream, mode: str, zipinfo: ZipInfo, pwd: Optional[bytes], close_fileobj: Literal[True] + ) -> None: ... + @overload def __init__( - self, fileobj: IO[bytes], mode: str, zipinfo: ZipInfo, pwd: Optional[bytes] = ..., close_fileobj: bool = ... + self, + fileobj: _ClosableZipStream, + mode: str, + zipinfo: ZipInfo, + pwd: Optional[bytes] = ..., + *, + close_fileobj: Literal[True], + ) -> None: ... + @overload + def __init__( + self, + fileobj: _ZipStream, + mode: str, + zipinfo: ZipInfo, + pwd: Optional[bytes] = ..., + close_fileobj: Literal[False] = ..., ) -> None: ... else: + @overload + def __init__( + self, + fileobj: _ClosableZipStream, + mode: str, + zipinfo: ZipInfo, + decrypter: Optional[Callable[[Sequence[int]], bytes]], + close_fileobj: Literal[True], + ) -> None: ... + @overload + def __init__( + self, + fileobj: _ClosableZipStream, + mode: str, + zipinfo: ZipInfo, + decrypter: Optional[Callable[[Sequence[int]], bytes]] = ..., + *, + close_fileobj: Literal[True], + ) -> None: ... + @overload def __init__( self, - fileobj: IO[bytes], + fileobj: _ZipStream, mode: str, zipinfo: ZipInfo, decrypter: Optional[Callable[[Sequence[int]], bytes]] = ..., - close_fileobj: bool = ..., + close_fileobj: Literal[False] = ..., ) -> None: ... def read(self, n: Optional[int] = ...) -> bytes: ... def readline(self, limit: int = ...) -> bytes: ... # type: ignore From 3447d14720bf7c511daaa84e3e48503112c5fb1c Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 16 May 2021 15:57:24 +0200 Subject: [PATCH 2/3] Use bool instead of Literal[False] --- stdlib/zipfile.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/zipfile.pyi b/stdlib/zipfile.pyi index 425a367cb99a..115a3747c5a1 100644 --- a/stdlib/zipfile.pyi +++ b/stdlib/zipfile.pyi @@ -56,7 +56,7 @@ class ZipExtFile(io.BufferedIOBase): mode: str, zipinfo: ZipInfo, pwd: Optional[bytes] = ..., - close_fileobj: Literal[False] = ..., + close_fileobj: bool = ..., ) -> None: ... else: @overload @@ -85,7 +85,7 @@ class ZipExtFile(io.BufferedIOBase): mode: str, zipinfo: ZipInfo, decrypter: Optional[Callable[[Sequence[int]], bytes]] = ..., - close_fileobj: Literal[False] = ..., + close_fileobj: bool = ..., ) -> None: ... def read(self, n: Optional[int] = ...) -> bytes: ... def readline(self, limit: int = ...) -> bytes: ... # type: ignore From 6101f59c0b9a4f49cfd9b5c8b3d7c8c58f2b71ad Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 16 May 2021 16:36:08 +0200 Subject: [PATCH 3/3] Use 'object' over 'Any' for protocol returns --- stdlib/zipfile.pyi | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/stdlib/zipfile.pyi b/stdlib/zipfile.pyi index 115a3747c5a1..619c467f445b 100644 --- a/stdlib/zipfile.pyi +++ b/stdlib/zipfile.pyi @@ -2,7 +2,7 @@ import io import sys from _typeshed import StrPath from types import TracebackType -from typing import IO, Any, Callable, Dict, Iterable, Iterator, List, Optional, Protocol, Sequence, Tuple, Type, Union, overload +from typing import IO, Callable, Dict, Iterable, Iterator, List, Optional, Protocol, Sequence, Tuple, Type, Union, overload from typing_extensions import Literal _DateTuple = Tuple[int, int, int, int, int, int] @@ -19,10 +19,10 @@ class _ZipStream(Protocol): # The following methods are optional: # def seekable(self) -> bool: ... # def tell(self) -> int: ... - # def seek(self, __n: int) -> Any: ... + # def seek(self, __n: int) -> object: ... class _ClosableZipStream(_ZipStream, Protocol): - def close(self) -> Any: ... + def close(self) -> object: ... class ZipExtFile(io.BufferedIOBase): MAX_N: int = ... @@ -51,12 +51,7 @@ class ZipExtFile(io.BufferedIOBase): ) -> None: ... @overload def __init__( - self, - fileobj: _ZipStream, - mode: str, - zipinfo: ZipInfo, - pwd: Optional[bytes] = ..., - close_fileobj: bool = ..., + self, fileobj: _ZipStream, mode: str, zipinfo: ZipInfo, pwd: Optional[bytes] = ..., close_fileobj: bool = ... ) -> None: ... else: @overload @@ -94,7 +89,7 @@ class ZipExtFile(io.BufferedIOBase): def read1(self, n: Optional[int]) -> bytes: ... # type: ignore class _Writer(Protocol): - def write(self, __s: str) -> Any: ... + def write(self, __s: str) -> object: ... class ZipFile: filename: Optional[str]