Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[codecs] Limit keyword errors to UTF-8
  • Loading branch information
jw9829 committed Sep 27, 2026
commit fce814fa03953a8ed752d3356bffaebf42ea50b9
1 change: 1 addition & 0 deletions stdlib/@tests/test_cases/check_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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)
Expand Down
5 changes: 5 additions & 0 deletions stdlib/_codecs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ 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]: ...
Expand Down
11 changes: 10 additions & 1 deletion stdlib/codecs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class _Encoder(Protocol):

@type_check_only
class _Decoder(Protocol):
def __call__(self, input: ReadableBuffer, /, errors: str = ...) -> tuple[str, int]: ... # signature of Codec().decode
def __call__(self, input: ReadableBuffer, errors: str = ..., /) -> tuple[str, int]: ... # signature of Codec().decode
Comment thread
JelleZijlstra marked this conversation as resolved.

@type_check_only
class _StreamReader(Protocol):
Expand Down Expand Up @@ -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: ...
Expand Down
Loading