3 from _typeshed import Self
4 from abc import abstractmethod
5 from typing import IO, Any, BinaryIO, Callable, Generator, Iterable, Iterator, Protocol, TextIO, Tuple, Type, TypeVar, overload
6 from typing_extensions import Literal
8 # TODO: this only satisfies the most common interface, where
9 # bytes is the raw form and str is the cooked form.
10 # In the long run, both should become template parameters maybe?
11 # There *are* bytes->bytes and str->str encodings in the standard library.
12 # They are much more common in Python 2 than in Python 3.
14 class _Encoder(Protocol):
15 def __call__(self, input: str, errors: str = ...) -> tuple[bytes, int]: ... # signature of Codec().encode
17 class _Decoder(Protocol):
18 def __call__(self, input: bytes, errors: str = ...) -> tuple[str, int]: ... # signature of Codec().decode
20 class _StreamReader(Protocol):
21 def __call__(self, stream: IO[bytes], errors: str = ...) -> StreamReader: ...
23 class _StreamWriter(Protocol):
24 def __call__(self, stream: IO[bytes], errors: str = ...) -> StreamWriter: ...
26 class _IncrementalEncoder(Protocol):
27 def __call__(self, errors: str = ...) -> IncrementalEncoder: ...
29 class _IncrementalDecoder(Protocol):
30 def __call__(self, errors: str = ...) -> IncrementalDecoder: ...
32 # The type ignore on `encode` and `decode` is to avoid issues with overlapping overloads, for more details, see #300
33 # mypy and pytype disagree about where the type ignore can and cannot go, so alias the long type
34 _BytesToBytesEncodingT = Literal[
54 def encode(obj: bytes, encoding: _BytesToBytesEncodingT, errors: str = ...) -> bytes: ...
56 def encode(obj: str, encoding: Literal["rot13", "rot_13"] = ..., errors: str = ...) -> str: ... # type: ignore
58 def encode(obj: str, encoding: str = ..., errors: str = ...) -> bytes: ...
60 def decode(obj: bytes, encoding: _BytesToBytesEncodingT, errors: str = ...) -> bytes: ... # type: ignore
62 def decode(obj: str, encoding: Literal["rot13", "rot_13"] = ..., errors: str = ...) -> str: ...
64 def decode(obj: bytes, encoding: str = ..., errors: str = ...) -> str: ...
65 def lookup(__encoding: str) -> CodecInfo: ...
66 def utf_16_be_decode(__data: bytes, __errors: str | None = ..., __final: bool = ...) -> tuple[str, int]: ... # undocumented
67 def utf_16_be_encode(__str: str, __errors: str | None = ...) -> tuple[bytes, int]: ... # undocumented
69 class CodecInfo(Tuple[_Encoder, _Decoder, _StreamReader, _StreamWriter]):
71 def encode(self) -> _Encoder: ...
73 def decode(self) -> _Decoder: ...
75 def streamreader(self) -> _StreamReader: ...
77 def streamwriter(self) -> _StreamWriter: ...
79 def incrementalencoder(self) -> _IncrementalEncoder: ...
81 def incrementaldecoder(self) -> _IncrementalDecoder: ...
87 streamreader: _StreamReader | None = ...,
88 streamwriter: _StreamWriter | None = ...,
89 incrementalencoder: _IncrementalEncoder | None = ...,
90 incrementaldecoder: _IncrementalDecoder | None = ...,
91 name: str | None = ...,
93 _is_text_encoding: bool | None = ...,
96 def getencoder(encoding: str) -> _Encoder: ...
97 def getdecoder(encoding: str) -> _Decoder: ...
98 def getincrementalencoder(encoding: str) -> _IncrementalEncoder: ...
99 def getincrementaldecoder(encoding: str) -> _IncrementalDecoder: ...
100 def getreader(encoding: str) -> _StreamReader: ...
101 def getwriter(encoding: str) -> _StreamWriter: ...
102 def register(__search_function: Callable[[str], CodecInfo | None]) -> None: ...
104 filename: str, mode: str = ..., encoding: str | None = ..., errors: str = ..., buffering: int = ...
105 ) -> StreamReaderWriter: ...
106 def EncodedFile(file: IO[bytes], data_encoding: str, file_encoding: str | None = ..., errors: str = ...) -> StreamRecoder: ...
107 def iterencode(iterator: Iterable[str], encoding: str, errors: str = ...) -> Generator[bytes, None, None]: ...
108 def iterdecode(iterator: Iterable[bytes], encoding: str, errors: str = ...) -> Generator[str, None, None]: ...
110 if sys.version_info >= (3, 10):
111 def unregister(__search_function: Callable[[str], CodecInfo | None]) -> None: ...
124 # It is expected that different actions be taken depending on which of the
125 # three subclasses of `UnicodeError` is actually ...ed. However, the Union
126 # is still needed for at least one of the cases.
127 def register_error(__errors: str, __handler: Callable[[UnicodeError], tuple[str | bytes, int]]) -> None: ...
128 def lookup_error(__name: str) -> Callable[[UnicodeError], tuple[str | bytes, int]]: ...
129 def strict_errors(exception: UnicodeError) -> tuple[str | bytes, int]: ...
130 def replace_errors(exception: UnicodeError) -> tuple[str | bytes, int]: ...
131 def ignore_errors(exception: UnicodeError) -> tuple[str | bytes, int]: ...
132 def xmlcharrefreplace_errors(exception: UnicodeError) -> tuple[str | bytes, int]: ...
133 def backslashreplace_errors(exception: UnicodeError) -> tuple[str | bytes, int]: ...
136 # These are sort of @abstractmethod but sort of not.
137 # The StreamReader and StreamWriter subclasses only implement one.
138 def encode(self, input: str, errors: str = ...) -> tuple[bytes, int]: ...
139 def decode(self, input: bytes, errors: str = ...) -> tuple[str, int]: ...
141 class IncrementalEncoder:
143 def __init__(self, errors: str = ...) -> None: ...
145 def encode(self, input: str, final: bool = ...) -> bytes: ...
146 def reset(self) -> None: ...
147 # documentation says int but str is needed for the subclass.
148 def getstate(self) -> int | str: ...
149 def setstate(self, state: int | str) -> None: ...
151 class IncrementalDecoder:
153 def __init__(self, errors: str = ...) -> None: ...
155 def decode(self, input: bytes, final: bool = ...) -> str: ...
156 def reset(self) -> None: ...
157 def getstate(self) -> tuple[bytes, int]: ...
158 def setstate(self, state: tuple[bytes, int]) -> None: ...
160 # These are not documented but used in encodings/*.py implementations.
161 class BufferedIncrementalEncoder(IncrementalEncoder):
163 def __init__(self, errors: str = ...) -> None: ...
165 def _buffer_encode(self, input: str, errors: str, final: bool) -> bytes: ...
166 def encode(self, input: str, final: bool = ...) -> bytes: ...
168 class BufferedIncrementalDecoder(IncrementalDecoder):
170 def __init__(self, errors: str = ...) -> None: ...
172 def _buffer_decode(self, input: bytes, errors: str, final: bool) -> tuple[str, int]: ...
173 def decode(self, input: bytes, final: bool = ...) -> str: ...
175 # TODO: it is not possible to specify the requirement that all other
176 # attributes and methods are passed-through from the stream.
177 class StreamWriter(Codec):
179 def __init__(self, stream: IO[bytes], errors: str = ...) -> None: ...
180 def write(self, object: str) -> None: ...
181 def writelines(self, list: Iterable[str]) -> None: ...
182 def reset(self) -> None: ...
183 def __enter__(self: Self) -> Self: ...
184 def __exit__(self, typ: Type[BaseException] | None, exc: BaseException | None, tb: types.TracebackType | None) -> None: ...
185 def __getattr__(self, name: str, getattr: Callable[[str], Any] = ...) -> Any: ...
187 class StreamReader(Codec):
189 def __init__(self, stream: IO[bytes], errors: str = ...) -> None: ...
190 def read(self, size: int = ..., chars: int = ..., firstline: bool = ...) -> str: ...
191 def readline(self, size: int | None = ..., keepends: bool = ...) -> str: ...
192 def readlines(self, sizehint: int | None = ..., keepends: bool = ...) -> list[str]: ...
193 def reset(self) -> None: ...
194 def __enter__(self: Self) -> Self: ...
195 def __exit__(self, typ: Type[BaseException] | None, exc: BaseException | None, tb: types.TracebackType | None) -> None: ...
196 def __iter__(self) -> Iterator[str]: ...
197 def __getattr__(self, name: str, getattr: Callable[[str], Any] = ...) -> Any: ...
199 _T = TypeVar("_T", bound=StreamReaderWriter)
201 # Doesn't actually inherit from TextIO, but wraps a BinaryIO to provide text reading and writing
202 # and delegates attributes to the underlying binary stream with __getattr__.
203 class StreamReaderWriter(TextIO):
204 def __init__(self, stream: IO[bytes], Reader: _StreamReader, Writer: _StreamWriter, errors: str = ...) -> None: ...
205 def read(self, size: int = ...) -> str: ...
206 def readline(self, size: int | None = ...) -> str: ...
207 def readlines(self, sizehint: int | None = ...) -> list[str]: ...
208 def __next__(self) -> str: ...
209 def __iter__(self: _T) -> _T: ...
210 # This actually returns None, but that's incompatible with the supertype
211 def write(self, data: str) -> int: ...
212 def writelines(self, list: Iterable[str]) -> None: ...
213 def reset(self) -> None: ...
215 def seek(self, offset: int, whence: int = ...) -> int: ...
216 def __enter__(self: Self) -> Self: ...
217 def __exit__(self, typ: Type[BaseException] | None, exc: BaseException | None, tb: types.TracebackType | None) -> None: ...
218 def __getattr__(self, name: str) -> Any: ...
219 # These methods don't actually exist directly, but they are needed to satisfy the TextIO
220 # interface. At runtime, they are delegated through __getattr__.
221 def close(self) -> None: ...
222 def fileno(self) -> int: ...
223 def flush(self) -> None: ...
224 def isatty(self) -> bool: ...
225 def readable(self) -> bool: ...
226 def truncate(self, size: int | None = ...) -> int: ...
227 def seekable(self) -> bool: ...
228 def tell(self) -> int: ...
229 def writable(self) -> bool: ...
231 _SRT = TypeVar("_SRT", bound=StreamRecoder)
233 class StreamRecoder(BinaryIO):
239 Reader: _StreamReader,
240 Writer: _StreamWriter,
243 def read(self, size: int = ...) -> bytes: ...
244 def readline(self, size: int | None = ...) -> bytes: ...
245 def readlines(self, sizehint: int | None = ...) -> list[bytes]: ...
246 def __next__(self) -> bytes: ...
247 def __iter__(self: _SRT) -> _SRT: ...
248 def write(self, data: bytes) -> int: ...
249 def writelines(self, list: Iterable[bytes]) -> int: ... # type: ignore # it's supposed to return None
250 def reset(self) -> None: ...
251 def __getattr__(self, name: str) -> Any: ...
252 def __enter__(self: Self) -> Self: ...
253 def __exit__(self, type: Type[BaseException] | None, value: BaseException | None, tb: types.TracebackType | None) -> None: ...
254 # These methods don't actually exist directly, but they are needed to satisfy the BinaryIO
255 # interface. At runtime, they are delegated through __getattr__.
256 def seek(self, offset: int, whence: int = ...) -> int: ...
257 def close(self) -> None: ...
258 def fileno(self) -> int: ...
259 def flush(self) -> None: ...
260 def isatty(self) -> bool: ...
261 def readable(self) -> bool: ...
262 def truncate(self, size: int | None = ...) -> int: ...
263 def seekable(self) -> bool: ...
264 def tell(self) -> int: ...
265 def writable(self) -> bool: ...