From 94592d47b338b1656daf47439c80a382d57bdda2 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Wed, 31 May 2017 23:20:41 -0700 Subject: [PATCH 1/3] Make HTTPError instantiable by introducing fictional _ConcreteBinaryIO in typing.pyi. --- stdlib/3/typing.pyi | 35 +++++++++++++++++++++++++++++++++++ stdlib/3/urllib/response.pyi | 4 ++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/stdlib/3/typing.pyi b/stdlib/3/typing.pyi index a7899a4a210c..26ca33352a36 100644 --- a/stdlib/3/typing.pyi +++ b/stdlib/3/typing.pyi @@ -404,6 +404,41 @@ class BinaryIO(IO[bytes]): @abstractmethod def __enter__(self) -> BinaryIO: ... +class _ConcreteBinaryIO(BinaryIO): + """Concretized typeshed-only version of BinaryIO. + + Doesn't actually exist; for inheritance in typeshed by classes that + support a "file-like" interface (e.g. urllib.response.addbase). + + If these classes inherit BinaryIO directly, mypy won't allow them to + be instantiated, due to the abstractmethods. + """ + def close(self) -> None: ... + def fileno(self) -> int: ... + def flush(self) -> None: ... + def isatty(self) -> bool: ... + def read(self, n: int = ...) -> AnyStr: ... + def readable(self) -> bool: ... + 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: AnyStr) -> int: ... + def writelines(self, lines: Iterable[AnyStr]) -> None: ... + def __next__(self) -> AnyStr: ... + def __iter__(self) -> Iterator[AnyStr]: ... + def __enter__(self) -> BinaryIO: ... + def __exit__(self, t: Optional[Type[BaseException]], value: Optional[BaseException], + # TODO: traceback should be TracebackType but that's defined in types + traceback: Optional[Any]) -> bool: ... + @overload + def write(self, s: bytes) -> int: ... + @overload + def write(self, s: bytearray) -> int: ... + class TextIO(IO[str]): # TODO use abstractproperty @property diff --git a/stdlib/3/urllib/response.pyi b/stdlib/3/urllib/response.pyi index 2c16cb679c0a..71daa5bc93db 100644 --- a/stdlib/3/urllib/response.pyi +++ b/stdlib/3/urllib/response.pyi @@ -1,9 +1,9 @@ # private module, we only expose what's needed -from typing import BinaryIO, Mapping, Optional +from typing import _ConcreteBinaryIO, Mapping, Optional from types import TracebackType -class addinfourl(BinaryIO): +class addinfourl(_ConcreteBinaryIO): headers = ... # type: Mapping[str, str] url = ... # type: str code = ... # type: int From 14a0c0ef2108aaf14eefae7c17b8b578abc241d7 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Wed, 31 May 2017 23:44:33 -0700 Subject: [PATCH 2/3] Fix obsolete TODO regarding TracebackType. --- stdlib/3/typing.pyi | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/stdlib/3/typing.pyi b/stdlib/3/typing.pyi index 26ca33352a36..2c5d62db03e4 100644 --- a/stdlib/3/typing.pyi +++ b/stdlib/3/typing.pyi @@ -387,8 +387,7 @@ class IO(Iterator[AnyStr], Generic[AnyStr]): def __enter__(self) -> 'IO[AnyStr]': ... @abstractmethod def __exit__(self, t: Optional[Type[BaseException]], value: Optional[BaseException], - # TODO: traceback should be TracebackType but that's defined in types - traceback: Optional[Any]) -> bool: ... + traceback: Optional[TracebackType]) -> bool: ... class BinaryIO(IO[bytes]): # TODO readinto @@ -432,8 +431,7 @@ class _ConcreteBinaryIO(BinaryIO): def __iter__(self) -> Iterator[AnyStr]: ... def __enter__(self) -> BinaryIO: ... def __exit__(self, t: Optional[Type[BaseException]], value: Optional[BaseException], - # TODO: traceback should be TracebackType but that's defined in types - traceback: Optional[Any]) -> bool: ... + traceback: Optional[TracebackType]) -> bool: ... @overload def write(self, s: bytes) -> int: ... @overload From 9848b6b07a97b9263be0d7556327e4d5f99cc69d Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Fri, 2 Jun 2017 09:32:09 -0700 Subject: [PATCH 3/3] Use BufferedRandom instead of _ConcreteBinaryIO. --- stdlib/3/typing.pyi | 34 ---------------------------------- stdlib/3/urllib/response.pyi | 5 +++-- 2 files changed, 3 insertions(+), 36 deletions(-) diff --git a/stdlib/3/typing.pyi b/stdlib/3/typing.pyi index 2c5d62db03e4..0cdcba3b247a 100644 --- a/stdlib/3/typing.pyi +++ b/stdlib/3/typing.pyi @@ -403,40 +403,6 @@ class BinaryIO(IO[bytes]): @abstractmethod def __enter__(self) -> BinaryIO: ... -class _ConcreteBinaryIO(BinaryIO): - """Concretized typeshed-only version of BinaryIO. - - Doesn't actually exist; for inheritance in typeshed by classes that - support a "file-like" interface (e.g. urllib.response.addbase). - - If these classes inherit BinaryIO directly, mypy won't allow them to - be instantiated, due to the abstractmethods. - """ - def close(self) -> None: ... - def fileno(self) -> int: ... - def flush(self) -> None: ... - def isatty(self) -> bool: ... - def read(self, n: int = ...) -> AnyStr: ... - def readable(self) -> bool: ... - 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: AnyStr) -> int: ... - def writelines(self, lines: Iterable[AnyStr]) -> None: ... - def __next__(self) -> AnyStr: ... - def __iter__(self) -> Iterator[AnyStr]: ... - def __enter__(self) -> BinaryIO: ... - def __exit__(self, t: Optional[Type[BaseException]], value: Optional[BaseException], - traceback: Optional[TracebackType]) -> bool: ... - @overload - def write(self, s: bytes) -> int: ... - @overload - def write(self, s: bytearray) -> int: ... - class TextIO(IO[str]): # TODO use abstractproperty @property diff --git a/stdlib/3/urllib/response.pyi b/stdlib/3/urllib/response.pyi index 71daa5bc93db..67c4f193283d 100644 --- a/stdlib/3/urllib/response.pyi +++ b/stdlib/3/urllib/response.pyi @@ -1,9 +1,10 @@ # private module, we only expose what's needed -from typing import _ConcreteBinaryIO, Mapping, Optional +from io import BufferedRandom +from typing import Mapping, Optional from types import TracebackType -class addinfourl(_ConcreteBinaryIO): +class addinfourl(BufferedRandom): headers = ... # type: Mapping[str, str] url = ... # type: str code = ... # type: int