From 1294d4fbc0748ed91b9af8a78f80bb209250d60c Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Fri, 21 Feb 2025 18:17:03 +0000 Subject: [PATCH 1/5] Return BufferedIncrementalReader for some encodings --- stdlib/codecs.pyi | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/stdlib/codecs.pyi b/stdlib/codecs.pyi index c6f517adb3cd..4044d5f5a207 100644 --- a/stdlib/codecs.pyi +++ b/stdlib/codecs.pyi @@ -3,7 +3,7 @@ from _codecs import * from _typeshed import ReadableBuffer from abc import abstractmethod from collections.abc import Callable, Generator, Iterable -from typing import Any, BinaryIO, ClassVar, Final, Literal, Protocol, TextIO +from typing import Any, BinaryIO, ClassVar, Final, Literal, Protocol, TextIO, overload from typing_extensions import Self __all__ = [ @@ -58,6 +58,8 @@ BOM32_LE: Final = b"\xff\xfe" BOM64_BE: Final = b"\x00\x00\xfe\xff" BOM64_LE: Final = b"\xff\xfe\x00\x00" +_BufferedEncoding = Literal["idna", "raw-unicode-escape", "unicode-escape", "utf-16", "utf-16-be", "utf-16-le", "utf-32", "utf-32-be", "utf-32-le", "utf-7", "utf-8", "utf-8-sig"] + class _WritableStream(Protocol): def write(self, data: bytes, /) -> object: ... def seek(self, offset: int, whence: int, /) -> object: ... @@ -125,6 +127,9 @@ class CodecInfo(tuple[_Encoder, _Decoder, _StreamReader, _StreamWriter]): def getencoder(encoding: str) -> _Encoder: ... def getdecoder(encoding: str) -> _Decoder: ... def getincrementalencoder(encoding: str) -> _IncrementalEncoder: ... +@overload +def getincrementaldecoder(encoding: _BufferedEncoding) -> BufferedIncrementalDecoder: ... +@overload def getincrementaldecoder(encoding: str) -> _IncrementalDecoder: ... def getreader(encoding: str) -> _StreamReader: ... def getwriter(encoding: str) -> _StreamWriter: ... From 9acf32e8e9be299fb4d1513a7786821f43d310dd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 21 Feb 2025 18:22:56 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/codecs.pyi | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/stdlib/codecs.pyi b/stdlib/codecs.pyi index 4044d5f5a207..b5a2c4e3042f 100644 --- a/stdlib/codecs.pyi +++ b/stdlib/codecs.pyi @@ -58,7 +58,20 @@ BOM32_LE: Final = b"\xff\xfe" BOM64_BE: Final = b"\x00\x00\xfe\xff" BOM64_LE: Final = b"\xff\xfe\x00\x00" -_BufferedEncoding = Literal["idna", "raw-unicode-escape", "unicode-escape", "utf-16", "utf-16-be", "utf-16-le", "utf-32", "utf-32-be", "utf-32-le", "utf-7", "utf-8", "utf-8-sig"] +_BufferedEncoding = Literal[ + "idna", + "raw-unicode-escape", + "unicode-escape", + "utf-16", + "utf-16-be", + "utf-16-le", + "utf-32", + "utf-32-be", + "utf-32-le", + "utf-7", + "utf-8", + "utf-8-sig", +] class _WritableStream(Protocol): def write(self, data: bytes, /) -> object: ... From 7c4416fe5494390d3880ae4b7557b493c912a96e Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Fri, 21 Feb 2025 18:25:00 +0000 Subject: [PATCH 3/5] Update codecs.pyi --- stdlib/codecs.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/codecs.pyi b/stdlib/codecs.pyi index b5a2c4e3042f..65c8091f43d7 100644 --- a/stdlib/codecs.pyi +++ b/stdlib/codecs.pyi @@ -143,7 +143,7 @@ def getincrementalencoder(encoding: str) -> _IncrementalEncoder: ... @overload def getincrementaldecoder(encoding: _BufferedEncoding) -> BufferedIncrementalDecoder: ... @overload -def getincrementaldecoder(encoding: str) -> _IncrementalDecoder: ... +def getincrementaldecoder(encoding: str) -> IncrementalDecoder: ... def getreader(encoding: str) -> _StreamReader: ... def getwriter(encoding: str) -> _StreamWriter: ... def open( From eb4991b638b7309a2e203a4ecc906c23873c5a93 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Fri, 21 Feb 2025 18:29:10 +0000 Subject: [PATCH 4/5] Update codecs.pyi --- stdlib/codecs.pyi | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/stdlib/codecs.pyi b/stdlib/codecs.pyi index 65c8091f43d7..351b84e756ae 100644 --- a/stdlib/codecs.pyi +++ b/stdlib/codecs.pyi @@ -109,6 +109,9 @@ class _IncrementalEncoder(Protocol): class _IncrementalDecoder(Protocol): def __call__(self, errors: str = ...) -> IncrementalDecoder: ... +class _BufferedIncrementalDecoder(Protocol): + def __call__(self, errors: str = ...) -> BufferedIncrementalDecoder: ... + class CodecInfo(tuple[_Encoder, _Decoder, _StreamReader, _StreamWriter]): _is_text_encoding: bool @property @@ -141,9 +144,9 @@ def getencoder(encoding: str) -> _Encoder: ... def getdecoder(encoding: str) -> _Decoder: ... def getincrementalencoder(encoding: str) -> _IncrementalEncoder: ... @overload -def getincrementaldecoder(encoding: _BufferedEncoding) -> BufferedIncrementalDecoder: ... +def getincrementaldecoder(encoding: _BufferedEncoding) -> _BufferedIncrementalDecoder: ... @overload -def getincrementaldecoder(encoding: str) -> IncrementalDecoder: ... +def getincrementaldecoder(encoding: str) -> _IncrementalDecoder: ... def getreader(encoding: str) -> _StreamReader: ... def getwriter(encoding: str) -> _StreamWriter: ... def open( From be9be0ce31944ff0a21d255a61395a94d928651f Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Fri, 21 Feb 2025 18:38:52 +0000 Subject: [PATCH 5/5] Update codecs.pyi --- stdlib/codecs.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/codecs.pyi b/stdlib/codecs.pyi index 351b84e756ae..579d09c66a1b 100644 --- a/stdlib/codecs.pyi +++ b/stdlib/codecs.pyi @@ -4,7 +4,7 @@ from _typeshed import ReadableBuffer from abc import abstractmethod from collections.abc import Callable, Generator, Iterable from typing import Any, BinaryIO, ClassVar, Final, Literal, Protocol, TextIO, overload -from typing_extensions import Self +from typing_extensions import Self, TypeAlias __all__ = [ "register", @@ -58,7 +58,7 @@ BOM32_LE: Final = b"\xff\xfe" BOM64_BE: Final = b"\x00\x00\xfe\xff" BOM64_LE: Final = b"\xff\xfe\x00\x00" -_BufferedEncoding = Literal[ +_BufferedEncoding: TypeAlias = Literal[ "idna", "raw-unicode-escape", "unicode-escape",