From ced5d61bb60758ef15f2bd829406dfbe216f5241 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 11 Jun 2018 15:43:53 -0700 Subject: [PATCH] make urllib.response.addinfourl instantiable (#2134) Fixes #1377 (because HTTPError is a subclass of addinfourl). Also part of #1476. --- stdlib/3/urllib/response.pyi | 40 +++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/stdlib/3/urllib/response.pyi b/stdlib/3/urllib/response.pyi index 2c16cb679..ef3507d7d 100644 --- a/stdlib/3/urllib/response.pyi +++ b/stdlib/3/urllib/response.pyi @@ -1,11 +1,41 @@ # private module, we only expose what's needed -from typing import BinaryIO, Mapping, Optional +from typing import BinaryIO, Iterable, List, Mapping, Optional, Type, TypeVar from types import TracebackType -class addinfourl(BinaryIO): - headers = ... # type: Mapping[str, str] - url = ... # type: str - code = ... # type: int +_AIUT = TypeVar("_AIUT", bound=addbase) + +class addbase(BinaryIO): + def __enter__(self: _AIUT) -> _AIUT: ... + def __exit__(self, type: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]) -> bool: ... + def __iter__(self: _AIUT) -> _AIUT: ... + def __next__(self) -> bytes: ... + def close(self) -> None: ... + # These methods don't actually exist, but the class inherits at runtime from + # tempfile._TemporaryFileWrapper, which uses __getattr__ to delegate to the + # underlying file object. To satisfy the BinaryIO interface, we pretend that this + # class has these additional methods. + def fileno(self) -> int: ... + def flush(self) -> None: ... + def isatty(self) -> bool: ... + def read(self, n: int = ...) -> bytes: ... + def readable(self) -> bool: ... + def readline(self, limit: int = ...) -> bytes: ... + def readlines(self, hint: int = ...) -> List[bytes]: ... + 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: bytes) -> int: ... + def writelines(self, lines: Iterable[bytes]) -> None: ... + +class addinfo(addbase): + headers: Mapping[str, str] def info(self) -> Mapping[str, str]: ... + +class addinfourl(addinfo): + url: str + code: int def geturl(self) -> str: ... + def getcode(self) -> int: ...