@@ -35,8 +35,8 @@ class _IncrementalDecoder(Protocol):
35
35
def __call__ (self , errors : str = ...) -> IncrementalDecoder : ...
36
36
37
37
# The type ignore on `encode` and `decode` is to avoid issues with overlapping overloads, for more details, see #300
38
- # mypy and pytype disagree about where the type ignore can and cannot go, so alias the long type
39
- _BytesToBytesEncodingT = Literal [
38
+ # https://docs.python.org/3/library/codecs.html#binary-transforms
39
+ _BytesToBytesEncoding = Literal [
40
40
"base64" ,
41
41
"base_64" ,
42
42
"base64_codec" ,
@@ -54,17 +54,23 @@ _BytesToBytesEncodingT = Literal[
54
54
"zlib" ,
55
55
"zlib_codec" ,
56
56
]
57
+ # https://docs.python.org/3/library/codecs.html#text-transforms
58
+ _StrToStrEncoding = Literal ["rot13" , "rot_13" ]
57
59
58
60
@overload
59
- def encode (obj : bytes , encoding : _BytesToBytesEncodingT , errors : str = ...) -> bytes : ...
61
+ def encode (obj : bytes , encoding : _BytesToBytesEncoding , errors : str = ...) -> bytes : ...
60
62
@overload
61
- def encode (obj : str , encoding : Literal [ "rot13" , "rot_13" , "hex" ] = ... , errors : str = ...) -> str : ... # type: ignore[misc]
63
+ def encode (obj : str , encoding : _StrToStrEncoding , errors : str = ...) -> str : ... # type: ignore[misc]
62
64
@overload
63
65
def encode (obj : str , encoding : str = ..., errors : str = ...) -> bytes : ...
64
66
@overload
65
- def decode (obj : bytes , encoding : _BytesToBytesEncodingT , errors : str = ...) -> bytes : ... # type: ignore[misc]
67
+ def decode (obj : bytes , encoding : _BytesToBytesEncoding , errors : str = ...) -> bytes : ... # type: ignore[misc]
66
68
@overload
67
- def decode (obj : str , encoding : Literal ["rot13" , "rot_13" , "hex" ] = ..., errors : str = ...) -> str : ...
69
+ def decode (obj : str , encoding : _StrToStrEncoding , errors : str = ...) -> str : ...
70
+
71
+ # hex is officially documented as a bytes to bytes encoding, but it appears to also work with str
72
+ @overload
73
+ def decode (obj : str , encoding : Literal ["hex" , "hex_codec" ], errors : str = ...) -> bytes : ...
68
74
@overload
69
75
def decode (obj : bytes , encoding : str = ..., errors : str = ...) -> str : ...
70
76
def lookup (__encoding : str ) -> CodecInfo : ...
0 commit comments