From 12bcba3b502c1e5b0ba584d0e764313fb0040d86 Mon Sep 17 00:00:00 2001 From: Dima Tisnek Date: Mon, 30 Sep 2024 14:55:34 +0900 Subject: [PATCH 1/4] fix: correct headers= kwarg in HTTP[S]Connection --- stdlib/http/client.pyi | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/stdlib/http/client.pyi b/stdlib/http/client.pyi index f68d9d0ca7d7..6675287ed33b 100644 --- a/stdlib/http/client.pyi +++ b/stdlib/http/client.pyi @@ -3,6 +3,7 @@ import io import ssl import sys import types +from typing import Protocol from _typeshed import ReadableBuffer, SupportsRead, SupportsReadline, WriteableBuffer from collections.abc import Callable, Iterable, Iterator, Mapping from socket import socket @@ -35,6 +36,11 @@ _DataType: TypeAlias = SupportsRead[bytes] | Iterable[ReadableBuffer] | Readable _T = TypeVar("_T") _MessageT = TypeVar("_MessageT", bound=email.message.Message) +class _HasEncode(Protocol): + def encode(encoding: str) -> bytes: ... + +_HeaderValue = ReadableBuffer | _HasEncode | int + HTTP_PORT: int HTTPS_PORT: int @@ -167,7 +173,7 @@ class HTTPConnection: method: str, url: str, body: _DataType | str | None = None, - headers: Mapping[str, str] = {}, + headers: Mapping[str, _HeaderValue] = {}, *, encode_chunked: bool = False, ) -> None: ... @@ -180,7 +186,7 @@ class HTTPConnection: def connect(self) -> None: ... def close(self) -> None: ... def putrequest(self, method: str, url: str, skip_host: bool = False, skip_accept_encoding: bool = False) -> None: ... - def putheader(self, header: str | bytes, *argument: str | bytes) -> None: ... + def putheader(self, header: str | bytes, *argument: _HeaderValue) -> None: ... def endheaders(self, message_body: _DataType | None = None, *, encode_chunked: bool = False) -> None: ... def send(self, data: _DataType | str) -> None: ... From c553424c9659298e547c97d8a7cd736f58796769 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 30 Sep 2024 06:04:32 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/http/client.pyi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/http/client.pyi b/stdlib/http/client.pyi index 6675287ed33b..40669d410e04 100644 --- a/stdlib/http/client.pyi +++ b/stdlib/http/client.pyi @@ -3,11 +3,10 @@ import io import ssl import sys import types -from typing import Protocol from _typeshed import ReadableBuffer, SupportsRead, SupportsReadline, WriteableBuffer from collections.abc import Callable, Iterable, Iterator, Mapping from socket import socket -from typing import Any, BinaryIO, TypeVar, overload +from typing import Any, BinaryIO, Protocol, TypeVar, overload from typing_extensions import Self, TypeAlias __all__ = [ From fef880c87ec05a5b12e972a8b16024b804559c68 Mon Sep 17 00:00:00 2001 From: Dima Tisnek Date: Mon, 30 Sep 2024 15:15:18 +0900 Subject: [PATCH 3/4] fix: missing self in protocol method --- stdlib/http/client.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/http/client.pyi b/stdlib/http/client.pyi index 6675287ed33b..09c5aa32a798 100644 --- a/stdlib/http/client.pyi +++ b/stdlib/http/client.pyi @@ -37,7 +37,7 @@ _T = TypeVar("_T") _MessageT = TypeVar("_MessageT", bound=email.message.Message) class _HasEncode(Protocol): - def encode(encoding: str) -> bytes: ... + def encode(self, encoding: str) -> bytes: ... _HeaderValue = ReadableBuffer | _HasEncode | int From 5fc861a01f8719bac4095ad470bb95a163e26a0d Mon Sep 17 00:00:00 2001 From: Dima Tisnek Date: Tue, 1 Oct 2024 11:52:23 +0900 Subject: [PATCH 4/4] use `str` instead of a Protocol --- stdlib/http/client.pyi | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/stdlib/http/client.pyi b/stdlib/http/client.pyi index ffa192af74ad..f47c744a6c7d 100644 --- a/stdlib/http/client.pyi +++ b/stdlib/http/client.pyi @@ -6,7 +6,7 @@ import types from _typeshed import ReadableBuffer, SupportsRead, SupportsReadline, WriteableBuffer from collections.abc import Callable, Iterable, Iterator, Mapping from socket import socket -from typing import Any, BinaryIO, Protocol, TypeVar, overload +from typing import Any, BinaryIO, TypeVar, overload from typing_extensions import Self, TypeAlias __all__ = [ @@ -34,11 +34,7 @@ __all__ = [ _DataType: TypeAlias = SupportsRead[bytes] | Iterable[ReadableBuffer] | ReadableBuffer _T = TypeVar("_T") _MessageT = TypeVar("_MessageT", bound=email.message.Message) - -class _HasEncode(Protocol): - def encode(self, encoding: str) -> bytes: ... - -_HeaderValue = ReadableBuffer | _HasEncode | int +_HeaderValue: TypeAlias = ReadableBuffer | str | int HTTP_PORT: int HTTPS_PORT: int @@ -185,7 +181,7 @@ class HTTPConnection: def connect(self) -> None: ... def close(self) -> None: ... def putrequest(self, method: str, url: str, skip_host: bool = False, skip_accept_encoding: bool = False) -> None: ... - def putheader(self, header: str | bytes, *argument: _HeaderValue) -> None: ... + def putheader(self, header: str | bytes, *values: _HeaderValue) -> None: ... def endheaders(self, message_body: _DataType | None = None, *, encode_chunked: bool = False) -> None: ... def send(self, data: _DataType | str) -> None: ...