Skip to content

http: improve types #9055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions stdlib/http/client.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import email.message
import io
import ssl
import types
from _typeshed import Self, WriteableBuffer
from _typeshed import ReadableBuffer, Self, SupportsRead, WriteableBuffer
from collections.abc import Callable, Iterable, Iterator, Mapping
from socket import socket
from typing import IO, Any, BinaryIO, TypeVar, overload
from typing import Any, BinaryIO, TypeVar, overload
from typing_extensions import TypeAlias

__all__ = [
Expand All @@ -30,7 +30,7 @@ __all__ = [
"HTTPSConnection",
]

_DataType: TypeAlias = bytes | IO[Any] | Iterable[bytes] | str
_DataType: TypeAlias = SupportsRead[bytes] | Iterable[ReadableBuffer] | ReadableBuffer
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTTPConnection.send() also allows str:

    def request(self, method, url, body=None, headers={}, *,
                encode_chunked=False):
        """Send a complete request to the server."""
        self._send_request(method, url, body, headers, encode_chunked)

    def _send_request(self, method, url, body, headers, encode_chunked):
        # ...
        # It also gets passed to _get_content_length(), which also supports str
        # ...

        if isinstance(body, str):
            # RFC 2616 Section 3.7.1 says that text default has a
            # default charset of iso-8859-1.
            body = _encode(body, 'body')
        self.endheaders(body, encode_chunked=encode_chunked)

I think the best solution is just to add str to the body argument of request manually.

_T = TypeVar("_T")

HTTP_PORT: int
Expand Down Expand Up @@ -164,7 +164,7 @@ class HTTPConnection:
def putrequest(self, method: str, url: str, skip_host: bool = ..., skip_accept_encoding: bool = ...) -> None: ...
def putheader(self, header: str, *argument: str) -> None: ...
def endheaders(self, message_body: _DataType | None = ..., *, encode_chunked: bool = ...) -> None: ...
def send(self, data: _DataType) -> None: ...
def send(self, data: _DataType | str) -> None: ...

class HTTPSConnection(HTTPConnection):
# Can be `None` if `.connect()` was not called:
Expand Down
25 changes: 21 additions & 4 deletions stdlib/http/server.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import email.message
import io
import socketserver
import sys
from _typeshed import StrPath, SupportsRead, SupportsWrite
from collections.abc import Mapping, Sequence
from typing import Any, AnyStr, BinaryIO, ClassVar
Expand Down Expand Up @@ -31,7 +32,6 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
default_request_version: str # undocumented
weekdayname: ClassVar[Sequence[str]] # undocumented
monthname: ClassVar[Sequence[str | None]] # undocumented
def __init__(self, request: bytes, client_address: tuple[str, int], server: socketserver.BaseServer) -> None: ...
def handle_one_request(self) -> None: ...
def handle_expect_100(self) -> bool: ...
def send_error(self, code: int, message: str | None = ..., explain: str | None = ...) -> None: ...
Expand All @@ -51,9 +51,26 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
extensions_map: dict[str, str]
def __init__(
self, request: bytes, client_address: tuple[str, int], server: socketserver.BaseServer, directory: str | None = ...
) -> None: ...
if sys.version_info >= (3, 12):
def __init__(
self,
request: socketserver._RequestType,
client_address: socketserver._AddressType,
server: socketserver.BaseServer,
*,
directory: str | None = ...,
) -> None: ...
else:
def __init__(
self,
request: socketserver._RequestType,
client_address: socketserver._AddressType,
server: socketserver.BaseServer,
*,
directory: str | None = ...,
index_pages: Sequence[str] | None = ...,
) -> None: ...

def do_GET(self) -> None: ...
def do_HEAD(self) -> None: ...
def send_head(self) -> io.BytesIO | BinaryIO | None: ... # undocumented
Expand Down
1 change: 0 additions & 1 deletion tests/stubtest_allowlists/py3_common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ ssl.RAND_egd
collections.abc.* # Types are re-exported from _collections_abc, so errors should be fixed there
distutils.command.check.SilentReporter # only defined if docutils in installed
hmac.HMAC.blocksize # use block_size instead
http.server.SimpleHTTPRequestHandler.__init__ # *args is expanded
pickle.Pickler.memo # undocumented implementation detail, has different type in C/Python implementations
pickle.Unpickler.memo # undocumented implementation detail, has different type in C/Python implementations
re.Pattern.scanner # Undocumented and not useful. #6405
Expand Down