diff --git a/stdlib/@tests/test_cases/check_codecs.py b/stdlib/@tests/test_cases/check_codecs.py index 19e663ceeaaf..2b5579d36444 100644 --- a/stdlib/@tests/test_cases/check_codecs.py +++ b/stdlib/@tests/test_cases/check_codecs.py @@ -7,6 +7,8 @@ assert_type(codecs.decode(b"x", "unicode-escape"), str) assert_type(codecs.decode(b"x", "utf-8"), str) +assert_type(codecs.lookup("UTF-8").decode(b"potato", errors="replace"), tuple[str, int]) +codecs.lookup("ascii").decode(b"potato", errors="replace") # type: ignore codecs.decode("x", "utf-8") # type: ignore assert_type(codecs.decode("ab", "hex"), bytes) diff --git a/stdlib/_codecs.pyi b/stdlib/_codecs.pyi index 449e9e24af9b..f3a9cee04ad5 100644 --- a/stdlib/_codecs.pyi +++ b/stdlib/_codecs.pyi @@ -70,7 +70,13 @@ def decode(obj: str, encoding: Literal["hex", "hex_codec"], errors: str = "stric @overload def decode(obj: ReadableBuffer, encoding: str = "utf-8", errors: str = "strict") -> str: ... +_UTF8Encoding: TypeAlias = Literal["UTF-8", "utf-8", "UTF_8", "utf_8", "UTF8", "utf8"] + +@overload +def lookup(encoding: _UTF8Encoding, /) -> codecs._UTF8CodecInfo: ... +@overload def lookup(encoding: str, /) -> codecs.CodecInfo: ... + def charmap_build(map: str, /) -> dict[int, int] | _EncodingMap: ... def ascii_decode(data: ReadableBuffer, errors: str | None = None, /) -> tuple[str, int]: ... def ascii_encode(str: str, errors: str | None = None, /) -> tuple[bytes, int]: ... diff --git a/stdlib/codecs.pyi b/stdlib/codecs.pyi index ee1ab25bac47..1e6fc8c94ecc 100644 --- a/stdlib/codecs.pyi +++ b/stdlib/codecs.pyi @@ -182,6 +182,15 @@ else: _is_text_encoding: bool | None = None, ) -> Self: ... +@type_check_only +class _UTF8Decoder(Protocol): + def __call__(self, input: ReadableBuffer, /, errors: str = ...) -> tuple[str, int]: ... + +@type_check_only +class _UTF8CodecInfo(CodecInfo): + @property + def decode(self) -> _UTF8Decoder: ... + def getencoder(encoding: str) -> _Encoder: ... def getdecoder(encoding: str) -> _Decoder: ... def getincrementalencoder(encoding: str) -> _IncrementalEncoder: ...