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 2 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
6 changes: 3 additions & 3 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
8 changes: 6 additions & 2 deletions stdlib/http/server.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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 @@ -52,7 +51,12 @@ 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 = ...
self,
request: socketserver._RequestType,
client_address: socketserver._AddressType,
server: socketserver.BaseServer,
directory: str | None = ...,
index_pages: Sequence[str] | None = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

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

As directory is keyword only and I can't find index_pages anywhere (on 3.9):

Suggested change
server: socketserver.BaseServer,
directory: str | None = ...,
index_pages: Sequence[str] | None = ...,
server: socketserver.BaseServer,
*,
directory: str | None = ...,

(directory is also passed to os.fspath() in later version (but not 3.7), but that's out of scope for this PR, since it needs overloads.)

Copy link
Member

@AlexWaygood AlexWaygood Nov 1, 2022

Choose a reason for hiding this comment

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

index_pages is new in 3.12: python/cpython#31985. So we should branch on if sys.version_info >= (3, 12).

) -> None: ...
def do_GET(self) -> None: ...
def do_HEAD(self) -> None: ...
Expand Down