From 2ae49e130751b9404e6703abe90fe869d63ef065 Mon Sep 17 00:00:00 2001 From: hatal175 Date: Mon, 12 Apr 2021 14:37:09 +0300 Subject: [PATCH] http/html stubtest fixes (#5208) --- stdlib/html/parser.pyi | 2 +- stdlib/http/client.pyi | 23 +++++++++++++++++++---- stdlib/http/cookiejar.pyi | 10 ++++++---- stdlib/http/cookies.pyi | 9 ++++++++- stdlib/http/server.pyi | 3 +-- tests/stubtest_whitelists/py3_common.txt | 19 ++----------------- 6 files changed, 37 insertions(+), 29 deletions(-) diff --git a/stdlib/html/parser.pyi b/stdlib/html/parser.pyi index 31240f78c..b49766bfc 100644 --- a/stdlib/html/parser.pyi +++ b/stdlib/html/parser.pyi @@ -3,7 +3,7 @@ from typing import List, Optional, Tuple class HTMLParser(ParserBase): def __init__(self, *, convert_charrefs: bool = ...) -> None: ... - def feed(self, feed: str) -> None: ... + def feed(self, data: str) -> None: ... def close(self) -> None: ... def reset(self) -> None: ... def getpos(self) -> Tuple[int, int]: ... diff --git a/stdlib/http/client.pyi b/stdlib/http/client.pyi index 34402e40b..2f6b2df08 100644 --- a/stdlib/http/client.pyi +++ b/stdlib/http/client.pyi @@ -3,6 +3,7 @@ import io import ssl import sys import types +from _typeshed import WriteableBuffer from socket import socket from typing import ( IO, @@ -103,6 +104,9 @@ class HTTPResponse(io.BufferedIOBase, BinaryIO): reason: str def __init__(self, sock: socket, debuglevel: int = ..., method: Optional[str] = ..., url: Optional[str] = ...) -> None: ... def read(self, amt: Optional[int] = ...) -> bytes: ... + def read1(self, n: int = ...) -> bytes: ... + def readinto(self, b: WriteableBuffer) -> int: ... + def readline(self, limit: int = ...) -> bytes: ... # type: ignore @overload def getheader(self, name: str) -> Optional[str]: ... @overload @@ -198,14 +202,25 @@ error = HTTPException class NotConnected(HTTPException): ... class InvalidURL(HTTPException): ... -class UnknownProtocol(HTTPException): ... + +class UnknownProtocol(HTTPException): + def __init__(self, version: str) -> None: ... + class UnknownTransferEncoding(HTTPException): ... class UnimplementedFileMode(HTTPException): ... -class IncompleteRead(HTTPException): ... + +class IncompleteRead(HTTPException): + def __init__(self, partial: bytes, expected: Optional[int] = ...) -> None: ... + class ImproperConnectionState(HTTPException): ... class CannotSendRequest(ImproperConnectionState): ... class CannotSendHeader(ImproperConnectionState): ... class ResponseNotReady(ImproperConnectionState): ... -class BadStatusLine(HTTPException): ... -class LineTooLong(HTTPException): ... + +class BadStatusLine(HTTPException): + def __init__(self, line: str) -> None: ... + +class LineTooLong(HTTPException): + def __init__(self, line_type: str) -> None: ... + class RemoteDisconnected(ConnectionResetError, BadStatusLine): ... diff --git a/stdlib/http/cookiejar.pyi b/stdlib/http/cookiejar.pyi index 310ccee71..a57c7c0fb 100644 --- a/stdlib/http/cookiejar.pyi +++ b/stdlib/http/cookiejar.pyi @@ -16,7 +16,7 @@ class CookieJar(Iterable[Cookie]): def make_cookies(self, response: HTTPResponse, request: Request) -> Sequence[Cookie]: ... def set_cookie(self, cookie: Cookie) -> None: ... def set_cookie_if_ok(self, cookie: Cookie, request: Request) -> None: ... - def clear(self, domain: str = ..., path: str = ..., name: str = ...) -> None: ... + def clear(self, domain: Optional[str] = ..., path: Optional[str] = ..., name: Optional[str] = ...) -> None: ... def clear_session_cookies(self) -> None: ... def __iter__(self) -> Iterator[Cookie]: ... def __len__(self) -> int: ... @@ -26,10 +26,12 @@ class FileCookieJar(CookieJar): delayload: bool if sys.version_info >= (3, 8): def __init__( - self, filename: Union[str, PathLike[str]] = ..., delayload: bool = ..., policy: Optional[CookiePolicy] = ... + self, filename: Optional[Union[str, PathLike[str]]] = ..., delayload: bool = ..., policy: Optional[CookiePolicy] = ... ) -> None: ... else: - def __init__(self, filename: str = ..., delayload: bool = ..., policy: Optional[CookiePolicy] = ...) -> None: ... + def __init__( + self, filename: Optional[str] = ..., delayload: bool = ..., policy: Optional[CookiePolicy] = ... + ) -> None: ... def save(self, filename: Optional[str] = ..., ignore_discard: bool = ..., ignore_expires: bool = ...) -> None: ... def load(self, filename: Optional[str] = ..., ignore_discard: bool = ..., ignore_expires: bool = ...) -> None: ... def revert(self, filename: Optional[str] = ..., ignore_discard: bool = ..., ignore_expires: bool = ...) -> None: ... @@ -126,4 +128,4 @@ class Cookie: @overload def get_nonstandard_attr(self, name: str, default: _T) -> Union[str, _T]: ... def set_nonstandard_attr(self, name: str, value: str) -> None: ... - def is_expired(self, now: int = ...) -> bool: ... + def is_expired(self, now: Optional[int] = ...) -> bool: ... diff --git a/stdlib/http/cookies.pyi b/stdlib/http/cookies.pyi index 14b0f817e..18dfdb9aa 100644 --- a/stdlib/http/cookies.pyi +++ b/stdlib/http/cookies.pyi @@ -1,5 +1,5 @@ import sys -from typing import Any, Dict, Generic, List, Mapping, Optional, TypeVar, Union, overload +from typing import Any, Dict, Generic, Iterable, List, Mapping, Optional, Tuple, TypeVar, Union, overload _DataType = Union[str, Mapping[str, Union[str, Morsel[Any]]]] _T = TypeVar("_T") @@ -18,10 +18,17 @@ class Morsel(Dict[str, Any], Generic[_T]): value: str coded_value: _T key: str + def __init__(self) -> None: ... if sys.version_info >= (3, 7): def set(self, key: str, val: str, coded_val: _T) -> None: ... else: def set(self, key: str, val: str, coded_val: _T, LegalChars: str = ...) -> None: ... + def setdefault(self, key: str, val: Optional[str] = ...) -> str: ... + # The dict update can also get a keywords argument so this is incompatible + @overload # type: ignore + def update(self, values: Mapping[str, str]) -> None: ... + @overload + def update(self, values: Iterable[Tuple[str, str]]) -> None: ... def isReservedKey(self, K: str) -> bool: ... def output(self, attrs: Optional[List[str]] = ..., header: str = ...) -> str: ... def js_output(self, attrs: Optional[List[str]] = ...) -> str: ... diff --git a/stdlib/http/server.pyi b/stdlib/http/server.pyi index 995a089ad..7d61bc4d2 100644 --- a/stdlib/http/server.pyi +++ b/stdlib/http/server.pyi @@ -1,7 +1,7 @@ import email.message import socketserver import sys -from typing import Any, Callable, ClassVar, Dict, List, Mapping, Optional, Sequence, Tuple, Union +from typing import Any, ClassVar, Dict, List, Mapping, Optional, Sequence, Tuple, Union if sys.version_info >= (3, 7): from builtins import _PathLike @@ -9,7 +9,6 @@ if sys.version_info >= (3, 7): class HTTPServer(socketserver.TCPServer): server_name: str server_port: int - def __init__(self, server_address: Tuple[str, int], RequestHandlerClass: Callable[..., BaseHTTPRequestHandler]) -> None: ... if sys.version_info >= (3, 7): class ThreadingHTTPServer(socketserver.ThreadingMixIn, HTTPServer): diff --git a/tests/stubtest_whitelists/py3_common.txt b/tests/stubtest_whitelists/py3_common.txt index 4a3076bfe..6b26f2fb4 100644 --- a/tests/stubtest_whitelists/py3_common.txt +++ b/tests/stubtest_whitelists/py3_common.txt @@ -147,23 +147,8 @@ encodings.utf_8.encode enum.EnumMeta.__call__ enum.EnumMeta.__new__ getopt.GetoptError.__init__ -html.parser.HTMLParser.feed -http.HTTPStatus.description -http.HTTPStatus.phrase -http.client.BadStatusLine.__init__ -http.client.HTTPResponse.read1 -http.client.HTTPResponse.readinto -http.client.HTTPResponse.readline -http.client.IncompleteRead.__init__ -http.client.LineTooLong.__init__ -http.client.UnknownProtocol.__init__ -http.cookiejar.Cookie.is_expired -http.cookiejar.CookieJar.clear -http.cookiejar.FileCookieJar.__init__ -http.cookies.Morsel.__init__ -http.cookies.Morsel.setdefault -http.cookies.Morsel.update -http.server.HTTPServer.__init__ +http.HTTPStatus.description # set in __new__ +http.HTTPStatus.phrase # set in __new__ imaplib.IMAP4_SSL.ssl importlib.abc.FileLoader.get_filename # Wrapped with _check_name decorator which changes runtime signature importlib.abc.FileLoader.load_module # Wrapped with _check_name decorator which changes runtime signature