2 from typing import Any, BinaryIO, Callable, Generator, IO, Iterable, List, Optional, Text, TextIO, Tuple, Type, TypeVar, Union
\r
4 from abc import abstractmethod
\r
7 # TODO: this only satisfies the most common interface, where
\r
8 # bytes (py2 str) is the raw form and str (py2 unicode) is the cooked form.
\r
9 # In the long run, both should become template parameters maybe?
\r
10 # There *are* bytes->bytes and str->str encodings in the standard library.
\r
11 # They are much more common in Python 2 than in Python 3.
\r
16 # TODO: It is not possible to specify these signatures correctly, because
\r
17 # they have an optional positional or keyword argument for errors=.
\r
18 _Encoder = Callable[[_Decoded], Tuple[_Encoded, int]] # signature of Codec().encode
\r
19 _Decoder = Callable[[_Encoded], Tuple[_Decoded, int]] # signature of Codec().decode
\r
20 _StreamReader = Callable[[IO[_Encoded]], StreamReader] # signature of StreamReader __init__
\r
21 _StreamWriter = Callable[[IO[_Encoded]], StreamWriter] # signature of StreamWriter __init__
\r
22 _IncrementalEncoder = Callable[[], IncrementalEncoder] # signature of IncrementalEncoder __init__
\r
23 _IncrementalDecoder = Callable[[], IncrementalDecoder] # signature of IncrementalDecoder __init__
\r
24 def encode(obj: _Decoded, encoding: str = ..., errors: str = ...) -> _Encoded: ...
\r
25 def decode(obj: _Encoded, encoding: str = ..., errors: str = ...) -> _Decoded: ...
\r
26 def lookup(encoding: str) -> CodecInfo: ...
\r
28 class CodecInfo(Tuple[_Encoder, _Decoder, _StreamReader, _StreamWriter]):
\r
30 def encode(self) -> _Encoder: ...
\r
32 def decode(self) -> _Decoder: ...
\r
34 def streamreader(self) -> _StreamReader: ...
\r
36 def streamwriter(self) -> _StreamWriter: ...
\r
38 def incrementalencoder(self) -> _IncrementalEncoder: ...
\r
40 def incrementaldecoder(self) -> _IncrementalDecoder: ...
\r
46 streamreader: _StreamReader = ...,
\r
47 streamwriter: _StreamWriter = ...,
\r
48 incrementalencoder: _IncrementalEncoder = ...,
\r
49 incrementaldecoder: _IncrementalDecoder = ...,
\r
53 def getencoder(encoding: str) -> _Encoder: ...
\r
54 def getdecoder(encoding: str) -> _Decoder: ...
\r
55 def getincrementalencoder(encoding: str) -> _IncrementalEncoder: ...
\r
56 def getincrementaldecoder(encoding: str) -> _IncrementalDecoder: ...
\r
57 def getreader(encoding: str) -> _StreamReader: ...
\r
58 def getwriter(encoding: str) -> _StreamWriter: ...
\r
59 def register(search_function: Callable[[str], CodecInfo]) -> None: ...
\r
60 def open(filename: str, mode: str = ..., encoding: str = ..., errors: str = ..., buffering: int = ...) -> StreamReaderWriter: ...
\r
61 def EncodedFile(file: IO[_Encoded], data_encoding: str, file_encoding: str = ..., errors: str = ...) -> StreamRecoder: ...
\r
62 def iterencode(iterator: Iterable[_Decoded], encoding: str, errors: str = ...) -> Generator[_Encoded, None, None]: ...
\r
63 def iterdecode(iterator: Iterable[_Encoded], encoding: str, errors: str = ...) -> Generator[_Decoded, None, None]: ...
\r
76 # It is expected that different actions be taken depending on which of the
\r
77 # three subclasses of `UnicodeError` is actually ...ed. However, the Union
\r
78 # is still needed for at least one of the cases.
\r
79 def register_error(name: str, error_handler: Callable[[UnicodeError], Tuple[Union[str, bytes], int]]) -> None: ...
\r
80 def lookup_error(name: str) -> Callable[[UnicodeError], Tuple[Union[str, bytes], int]]: ...
\r
81 def strict_errors(exception: UnicodeError) -> Tuple[Union[str, bytes], int]: ...
\r
82 def replace_errors(exception: UnicodeError) -> Tuple[Union[str, bytes], int]: ...
\r
83 def ignore_errors(exception: UnicodeError) -> Tuple[Union[str, bytes], int]: ...
\r
84 def xmlcharrefreplace_errors(exception: UnicodeError) -> Tuple[Union[str, bytes], int]: ...
\r
85 def backslashreplace_errors(exception: UnicodeError) -> Tuple[Union[str, bytes], int]: ...
\r
88 # These are sort of @abstractmethod but sort of not.
\r
89 # The StreamReader and StreamWriter subclasses only implement one.
\r
90 def encode(self, input: _Decoded, errors: str = ...) -> Tuple[_Encoded, int]: ...
\r
91 def decode(self, input: _Encoded, errors: str = ...) -> Tuple[_Decoded, int]: ...
\r
93 class IncrementalEncoder:
\r
95 def __init__(self, errors: str = ...) -> None: ...
\r
97 def encode(self, object: _Decoded, final: bool = ...) -> _Encoded: ...
\r
98 def reset(self) -> None: ...
\r
99 # documentation says int but str is needed for the subclass.
\r
100 def getstate(self) -> Union[int, _Decoded]: ...
\r
101 def setstate(self, state: Union[int, _Decoded]) -> None: ...
\r
103 class IncrementalDecoder:
\r
105 def __init__(self, errors: str = ...) -> None: ...
\r
107 def decode(self, object: _Encoded, final: bool = ...) -> _Decoded: ...
\r
108 def reset(self) -> None: ...
\r
109 def getstate(self) -> Tuple[_Encoded, int]: ...
\r
110 def setstate(self, state: Tuple[_Encoded, int]) -> None: ...
\r
112 # These are not documented but used in encodings/*.py implementations.
\r
113 class BufferedIncrementalEncoder(IncrementalEncoder):
\r
115 def __init__(self, errors: str = ...) -> None: ...
\r
117 def _buffer_encode(self, input: _Decoded, errors: str, final: bool) -> _Encoded: ...
\r
118 def encode(self, input: _Decoded, final: bool = ...) -> _Encoded: ...
\r
120 class BufferedIncrementalDecoder(IncrementalDecoder):
\r
122 def __init__(self, errors: str = ...) -> None: ...
\r
124 def _buffer_decode(self, input: _Encoded, errors: str, final: bool) -> Tuple[_Decoded, int]: ...
\r
125 def decode(self, object: _Encoded, final: bool = ...) -> _Decoded: ...
\r
127 # TODO: it is not possible to specify the requirement that all other
\r
128 # attributes and methods are passed-through from the stream.
\r
129 class StreamWriter(Codec):
\r
131 def __init__(self, stream: IO[_Encoded], errors: str = ...) -> None: ...
\r
132 def write(self, obj: _Decoded) -> None: ...
\r
133 def writelines(self, list: Iterable[_Decoded]) -> None: ...
\r
134 def reset(self) -> None: ...
\r
136 class StreamReader(Codec):
\r
138 def __init__(self, stream: IO[_Encoded], errors: str = ...) -> None: ...
\r
139 def read(self, size: int = ..., chars: int = ..., firstline: bool = ...) -> _Decoded: ...
\r
140 def readline(self, size: int = ..., keepends: bool = ...) -> _Decoded: ...
\r
141 def readlines(self, sizehint: int = ..., keepends: bool = ...) -> List[_Decoded]: ...
\r
142 def reset(self) -> None: ...
\r
144 _T = TypeVar("_T", bound=StreamReaderWriter)
\r
146 # Doesn't actually inherit from TextIO, but wraps a BinaryIO to provide text reading and writing
\r
147 # and delegates attributes to the underlying binary stream with __getattr__.
\r
148 class StreamReaderWriter(TextIO):
\r
149 def __init__(self, stream: IO[_Encoded], Reader: _StreamReader, Writer: _StreamWriter, errors: str = ...) -> None: ...
\r
150 def read(self, size: int = ...) -> _Decoded: ...
\r
151 def readline(self, size: Optional[int] = ...) -> _Decoded: ...
\r
152 def readlines(self, sizehint: Optional[int] = ...) -> List[_Decoded]: ...
\r
153 if sys.version_info >= (3,):
\r
154 def __next__(self) -> Text: ...
\r
156 def next(self) -> Text: ...
\r
157 def __iter__(self: _T) -> _T: ...
\r
158 # This actually returns None, but that's incompatible with the supertype
\r
159 def write(self, data: _Decoded) -> int: ...
\r
160 def writelines(self, list: Iterable[_Decoded]) -> None: ...
\r
161 def reset(self) -> None: ...
\r
163 def seek(self, offset: int, whence: int = ...) -> int: ...
\r
164 def __enter__(self: _T) -> _T: ...
\r
166 self, typ: Optional[Type[BaseException]], exc: Optional[BaseException], tb: Optional[types.TracebackType]
\r
168 def __getattr__(self, name: str) -> Any: ...
\r
169 # These methods don't actually exist directly, but they are needed to satisfy the TextIO
\r
170 # interface. At runtime, they are delegated through __getattr__.
\r
171 def close(self) -> None: ...
\r
172 def fileno(self) -> int: ...
\r
173 def flush(self) -> None: ...
\r
174 def isatty(self) -> bool: ...
\r
175 def readable(self) -> bool: ...
\r
176 def truncate(self, size: Optional[int] = ...) -> int: ...
\r
177 def seekable(self) -> bool: ...
\r
178 def tell(self) -> int: ...
\r
179 def writable(self) -> bool: ...
\r
181 _SRT = TypeVar("_SRT", bound=StreamRecoder)
\r
183 class StreamRecoder(BinaryIO):
\r
186 stream: IO[_Encoded],
\r
189 Reader: _StreamReader,
\r
190 Writer: _StreamWriter,
\r
193 def read(self, size: int = ...) -> bytes: ...
\r
194 def readline(self, size: Optional[int] = ...) -> bytes: ...
\r
195 def readlines(self, sizehint: Optional[int] = ...) -> List[bytes]: ...
\r
196 if sys.version_info >= (3,):
\r
197 def __next__(self) -> bytes: ...
\r
199 def next(self) -> bytes: ...
\r
200 def __iter__(self: _SRT) -> _SRT: ...
\r
201 def write(self, data: bytes) -> int: ...
\r
202 def writelines(self, list: Iterable[bytes]) -> int: ... # type: ignore # it's supposed to return None
\r
203 def reset(self) -> None: ...
\r
204 def __getattr__(self, name: str) -> Any: ...
\r
205 def __enter__(self: _SRT) -> _SRT: ...
\r
207 self, type: Optional[Type[BaseException]], value: Optional[BaseException], tb: Optional[types.TracebackType]
\r
209 # These methods don't actually exist directly, but they are needed to satisfy the BinaryIO
\r
210 # interface. At runtime, they are delegated through __getattr__.
\r
211 def seek(self, offset: int, whence: int = ...) -> int: ...
\r
212 def close(self) -> None: ...
\r
213 def fileno(self) -> int: ...
\r
214 def flush(self) -> None: ...
\r
215 def isatty(self) -> bool: ...
\r
216 def readable(self) -> bool: ...
\r
217 def truncate(self, size: Optional[int] = ...) -> int: ...
\r
218 def seekable(self) -> bool: ...
\r
219 def tell(self) -> int: ...
\r
220 def writable(self) -> bool: ...
\r