From dad65e412140f565102f6340c2b817a63e82d38c Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 27 Apr 2017 08:47:42 -0700 Subject: [PATCH] improve stubs for _codecs (#1219) * improve stubs for _codecs I noticed that many of these used AnyStr in a context where it doesn't make sense. Found a few more things after reviewing _codecsmodule.c: - Some Windows-only functions were omitted - A few argument counts and names and return types were wrong The Python 2 _codecs stub has many of the same issues, but the module is also pretty similar between Python 2 and 3. I'm planning to send another PR after this one to merge the two and move the stub into 2and3. * correct platform name --- stdlib/3/_codecs.pyi | 86 ++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/stdlib/3/_codecs.pyi b/stdlib/3/_codecs.pyi index 8d4fb3c18..485ca493a 100644 --- a/stdlib/3/_codecs.pyi +++ b/stdlib/3/_codecs.pyi @@ -1,51 +1,61 @@ """Stub file for the '_codecs' module.""" -from typing import Any, AnyStr, Callable, Tuple, Optional, Dict +import sys +from typing import Any, Callable, Tuple, Optional, Dict, Union import codecs # For convenience: _Handler = Callable[[Exception], Tuple[str, int]] +_String = Union[bytes, str] def register(search_function: Callable[[str], Any]) -> None: ... def register_error(errors: str, handler: _Handler) -> None: ... -def lookup(a: str) -> codecs.CodecInfo: ... -def lookup_error(a: str) -> _Handler: ... +def lookup(encoding: str) -> codecs.CodecInfo: ... +def lookup_error(name: str) -> _Handler: ... def decode(obj: Any, encoding: str = ..., errors: str = ...) -> Any: ... def encode(obj: Any, encoding: str = ..., errors: str = ...) -> Any: ... -def charmap_build(a: str) -> Dict[int, int]: ... +def charmap_build(map: str) -> Dict[int, int]: ... -def ascii_decode(data: AnyStr, errors: str = ...) -> Tuple[str, int]: ... -def ascii_encode(data: AnyStr, errors: str = ...) -> Tuple[bytes, int]: ... -def charbuffer_encode(data: AnyStr, errors: str = ...) -> Tuple[bytes, int]: ... -def charmap_decode(data: AnyStr, errors: str = ..., mapping: Optional[Dict[int, int]] = ...) -> Tuple[str, int]: ... -def charmap_encode(data: AnyStr, errors: str, mapping: Optional[Dict[int, int]] = ...) -> Tuple[bytes, int]: ... -def escape_decode(data: AnyStr, errors: str = ...) -> Tuple[str, int]: ... -def escape_encode(data: AnyStr, errors: str = ...) -> Tuple[bytes, int]: ... -def latin_1_decode(data: AnyStr, errors: str = ...) -> Tuple[str, int]: ... -def latin_1_encode(data: AnyStr, errors: str = ...) -> Tuple[bytes, int]: ... -def raw_unicode_escape_decode(data: AnyStr, errors: str = ...) -> Tuple[str, int]: ... -def raw_unicode_escape_encode(data: AnyStr, errors: str = ...) -> Tuple[bytes, int]: ... -def readbuffer_encode(data: AnyStr, errors: str = ...) -> Tuple[bytes, int]: ... -def unicode_escape_decode(data: AnyStr, errors: str = ...) -> Tuple[str, int]: ... -def unicode_escape_encode(data: AnyStr, errors: str = ...) -> Tuple[bytes, int]: ... -def unicode_internal_decode(data: AnyStr, errors: str = ...) -> Tuple[str, int]: ... -def unicode_internal_encode(data: AnyStr, errors: str = ...) -> Tuple[bytes, int]: ... -def utf_16_be_decode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... -def utf_16_be_encode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[bytes, int]: ... -def utf_16_decode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... -def utf_16_encode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[bytes, int]: ... -def utf_16_ex_decode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... -def utf_16_le_decode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... -def utf_16_le_encode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[bytes, int]: ... -def utf_32_be_decode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... -def utf_32_be_encode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[bytes, int]: ... -def utf_32_decode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... -def utf_32_encode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[bytes, int]: ... -def utf_32_ex_decode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... -def utf_32_le_decode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... -def utf_32_le_encode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[bytes, int]: ... -def utf_7_decode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... -def utf_7_encode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[bytes, int]: ... -def utf_8_decode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... -def utf_8_encode(data: AnyStr, errors: str = ..., final: int = ...) -> Tuple[bytes, int]: ... +def ascii_decode(data: bytes, errors: str = ...) -> Tuple[str, int]: ... +def ascii_encode(data: str, errors: str = ...) -> Tuple[bytes, int]: ... +def charbuffer_encode(data: str, errors: str = ...) -> Tuple[bytes, int]: ... +def charmap_decode(data: bytes, errors: str = ..., mapping: Optional[Dict[int, int]] = ...) -> Tuple[str, int]: ... +def charmap_encode(data: str, errors: str, mapping: Optional[Dict[int, int]] = ...) -> Tuple[bytes, int]: ... +def escape_decode(data: _String, errors: str = ...) -> Tuple[str, int]: ... +def escape_encode(data: bytes, errors: str = ...) -> Tuple[bytes, int]: ... +def latin_1_decode(data: bytes, errors: str = ...) -> Tuple[str, int]: ... +def latin_1_encode(data: str, errors: str = ...) -> Tuple[bytes, int]: ... +def raw_unicode_escape_decode(data: _String, errors: str = ...) -> Tuple[str, int]: ... +def raw_unicode_escape_encode(data: str, errors: str = ...) -> Tuple[bytes, int]: ... +def readbuffer_encode(data: _String, errors: str = ...) -> Tuple[bytes, int]: ... +def unicode_escape_decode(data: _String, errors: str = ...) -> Tuple[str, int]: ... +def unicode_escape_encode(data: str, errors: str = ...) -> Tuple[bytes, int]: ... +def unicode_internal_decode(data: _String, errors: str = ...) -> Tuple[str, int]: ... +def unicode_internal_encode(data: _String, errors: str = ...) -> Tuple[bytes, int]: ... +def utf_16_be_decode(data: bytes, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... +def utf_16_be_encode(data: str, errors: str = ...) -> Tuple[bytes, int]: ... +def utf_16_decode(data: bytes, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... +def utf_16_encode(data: str, errors: str = ..., byteorder: int = ...) -> Tuple[bytes, int]: ... +def utf_16_ex_decode(data: bytes, errors: str = ..., final: int = ...) -> Tuple[str, int, int]: ... +def utf_16_le_decode(data: bytes, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... +def utf_16_le_encode(data: str, errors: str = ...) -> Tuple[bytes, int]: ... +def utf_32_be_decode(data: bytes, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... +def utf_32_be_encode(data: str, errors: str = ...) -> Tuple[bytes, int]: ... +def utf_32_decode(data: bytes, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... +def utf_32_encode(data: str, errors: str = ..., byteorder: int = ...) -> Tuple[bytes, int]: ... +def utf_32_ex_decode(data: bytes, errors: str = ..., final: int = ...) -> Tuple[str, int, int]: ... +def utf_32_le_decode(data: bytes, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... +def utf_32_le_encode(data: str, errors: str = ...) -> Tuple[bytes, int]: ... +def utf_7_decode(data: bytes, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... +def utf_7_encode(data: str, errors: str = ...) -> Tuple[bytes, int]: ... +def utf_8_decode(data: bytes, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... +def utf_8_encode(data: str, errors: str = ...) -> Tuple[bytes, int]: ... + +if sys.platform == 'win32': + def mbcs_decode(data: bytes, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... + def oem_decode(data: bytes, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... + def code_page_decode(codepage: int, data: bytes, errors: str = ..., final: int = ...) -> Tuple[str, int]: ... + def mbcs_encode(str: str, errors: str = ...) -> Tuple[bytes, int]: ... + def oem_encode(str: str, errors: str = ...) -> Tuple[bytes, int]: ... + def code_page_encode(code_page: int, str: str, errors: str = ...) -> Tuple[bytes, int]: ...