Skip to content

Commit 094b25f

Browse files
committed
Fix codecs.register_error callback type
The callback receives one of the three concrete Unicode codec error types. Model that union so contextual typing exposes their shared attributes.
1 parent 4bca622 commit 094b25f

2 files changed

Lines changed: 4 additions & 1 deletion

File tree

‎stdlib/@tests/test_cases/check_codecs.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@
1111

1212
assert_type(codecs.decode("ab", "hex"), bytes)
1313
assert_type(codecs.decode(b"ab", "hex"), bytes)
14+
15+
codecs.register_error("test", lambda error: (assert_type(error.object, str | bytes), assert_type(error.end, int)))

‎stdlib/_codecs.pyi‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class _EncodingMap:
1313

1414
_DecodeCharMap: TypeAlias = SupportsGetItem[int, str | int | None]
1515
_EncodeCharMap: TypeAlias = SupportsGetItem[int, bytes | int | None] | _EncodingMap
16-
_Handler: TypeAlias = Callable[[UnicodeError], tuple[str | bytes, int]]
16+
_CodecError: TypeAlias = UnicodeEncodeError | UnicodeDecodeError | UnicodeTranslateError
17+
_Handler: TypeAlias = Callable[[_CodecError], tuple[str | bytes, int]]
1718
_SearchFunction: TypeAlias = Callable[[str], codecs.CodecInfo | None]
1819

1920
def register(search_function: _SearchFunction, /) -> None: ...

0 commit comments

Comments
 (0)