Debian Codegraph @

jedi/third_party/typeshed/stdlib/codecs.pyi

raw symbols SCIP
1 import sys
2 import types
3 from _codecs import *
4 from _typeshed import ReadableBuffer
5 from abc import abstractmethod
6 from collections.abc import Callable, Generator, Iterable
7 from typing import Any, BinaryIO, ClassVar, Final, Literal, Protocol, TextIO, overload, type_check_only
8 from typing_extensions import Self, TypeAlias, deprecated, disjoint_base
9
10 __all__ = [
11 "register",
12 "lookup",
13 "open",
14 "EncodedFile",
15 "BOM",
16 "BOM_BE",
17 "BOM_LE",
18 "BOM32_BE",
19 "BOM32_LE",
20 "BOM64_BE",
21 "BOM64_LE",
22 "BOM_UTF8",
23 "BOM_UTF16",
24 "BOM_UTF16_LE",
25 "BOM_UTF16_BE",
26 "BOM_UTF32",
27 "BOM_UTF32_LE",
28 "BOM_UTF32_BE",
29 "CodecInfo",
30 "Codec",
31 "IncrementalEncoder",
32 "IncrementalDecoder",
33 "StreamReader",
34 "StreamWriter",
35 "StreamReaderWriter",
36 "StreamRecoder",
37 "getencoder",
38 "getdecoder",
39 "getincrementalencoder",
40 "getincrementaldecoder",
41 "getreader",
42 "getwriter",
43 "encode",
44 "decode",
45 "iterencode",
46 "iterdecode",
47 "strict_errors",
48 "ignore_errors",
49 "replace_errors",
50 "xmlcharrefreplace_errors",
51 "backslashreplace_errors",
52 "namereplace_errors",
53 "register_error",
54 "lookup_error",
55 ]
56
57 BOM32_BE: Final = b"\xfe\xff"
58 BOM32_LE: Final = b"\xff\xfe"
59 BOM64_BE: Final = b"\x00\x00\xfe\xff"
60 BOM64_LE: Final = b"\xff\xfe\x00\x00"
61
62 _BufferedEncoding: TypeAlias = Literal[
63 "idna",
64 "raw-unicode-escape",
65 "unicode-escape",
66 "utf-16",
67 "utf-16-be",
68 "utf-16-le",
69 "utf-32",
70 "utf-32-be",
71 "utf-32-le",
72 "utf-7",
73 "utf-8",
74 "utf-8-sig",
75 ]
76
77 @type_check_only
78 class _WritableStream(Protocol):
79 def write(self, data: bytes, /) -> object: ...
80 def seek(self, offset: int, whence: int, /) -> object: ...
81 def close(self) -> object: ...
82
83 @type_check_only
84 class _ReadableStream(Protocol):
85 def read(self, size: int = ..., /) -> bytes: ...
86 def seek(self, offset: int, whence: int, /) -> object: ...
87 def close(self) -> object: ...
88
89 @type_check_only
90 class _Stream(_WritableStream, _ReadableStream, Protocol): ...
91
92 # TODO: this only satisfies the most common interface, where
93 # bytes is the raw form and str is the cooked form.
94 # In the long run, both should become template parameters maybe?
95 # There *are* bytes->bytes and str->str encodings in the standard library.
96 # They were much more common in Python 2 than in Python 3.
97
98 @type_check_only
99 class _Encoder(Protocol):
100 def __call__(self, input: str, errors: str = ..., /) -> tuple[bytes, int]: ... # signature of Codec().encode
101
102 @type_check_only
103 class _Decoder(Protocol):
104 def __call__(self, input: ReadableBuffer, errors: str = ..., /) -> tuple[str, int]: ... # signature of Codec().decode
105
106 @type_check_only
107 class _StreamReader(Protocol):
108 def __call__(self, stream: _ReadableStream, errors: str = ..., /) -> StreamReader: ...
109
110 @type_check_only
111 class _StreamWriter(Protocol):
112 def __call__(self, stream: _WritableStream, errors: str = ..., /) -> StreamWriter: ...
113
114 @type_check_only
115 class _IncrementalEncoder(Protocol):
116 def __call__(self, errors: str = ...) -> IncrementalEncoder: ...
117
118 @type_check_only
119 class _IncrementalDecoder(Protocol):
120 def __call__(self, errors: str = ...) -> IncrementalDecoder: ...
121
122 @type_check_only
123 class _BufferedIncrementalDecoder(Protocol):
124 def __call__(self, errors: str = ...) -> BufferedIncrementalDecoder: ...
125
126 if sys.version_info >= (3, 12):
127 class CodecInfo(tuple[_Encoder, _Decoder, _StreamReader, _StreamWriter]):
128 _is_text_encoding: bool
129 @property
130 def encode(self) -> _Encoder: ...
131 @property
132 def decode(self) -> _Decoder: ...
133 @property
134 def streamreader(self) -> _StreamReader: ...
135 @property
136 def streamwriter(self) -> _StreamWriter: ...
137 @property
138 def incrementalencoder(self) -> _IncrementalEncoder: ...
139 @property
140 def incrementaldecoder(self) -> _IncrementalDecoder: ...
141 name: str
142 def __new__(
143 cls,
144 encode: _Encoder,
145 decode: _Decoder,
146 streamreader: _StreamReader | None = None,
147 streamwriter: _StreamWriter | None = None,
148 incrementalencoder: _IncrementalEncoder | None = None,
149 incrementaldecoder: _IncrementalDecoder | None = None,
150 name: str | None = None,
151 *,
152 _is_text_encoding: bool | None = None,
153 ) -> Self: ...
154
155 else:
156 @disjoint_base
157 class CodecInfo(tuple[_Encoder, _Decoder, _StreamReader, _StreamWriter]):
158 _is_text_encoding: bool
159 @property
160 def encode(self) -> _Encoder: ...
161 @property
162 def decode(self) -> _Decoder: ...
163 @property
164 def streamreader(self) -> _StreamReader: ...
165 @property
166 def streamwriter(self) -> _StreamWriter: ...
167 @property
168 def incrementalencoder(self) -> _IncrementalEncoder: ...
169 @property
170 def incrementaldecoder(self) -> _IncrementalDecoder: ...
171 name: str
172 def __new__(
173 cls,
174 encode: _Encoder,
175 decode: _Decoder,
176 streamreader: _StreamReader | None = None,
177 streamwriter: _StreamWriter | None = None,
178 incrementalencoder: _IncrementalEncoder | None = None,
179 incrementaldecoder: _IncrementalDecoder | None = None,
180 name: str | None = None,
181 *,
182 _is_text_encoding: bool | None = None,
183 ) -> Self: ...
184
185 def getencoder(encoding: str) -> _Encoder: ...
186 def getdecoder(encoding: str) -> _Decoder: ...
187 def getincrementalencoder(encoding: str) -> _IncrementalEncoder: ...
188 @overload
189 def getincrementaldecoder(encoding: _BufferedEncoding) -> _BufferedIncrementalDecoder: ...
190 @overload
191 def getincrementaldecoder(encoding: str) -> _IncrementalDecoder: ...
192 def getreader(encoding: str) -> _StreamReader: ...
193 def getwriter(encoding: str) -> _StreamWriter: ...
194 @deprecated("Deprecated since Python 3.14. Use `open()` instead.")
195 def open(
196 filename: str, mode: str = "r", encoding: str | None = None, errors: str = "strict", buffering: int = -1
197 ) -> StreamReaderWriter: ...
198 def EncodedFile(file: _Stream, data_encoding: str, file_encoding: str | None = None, errors: str = "strict") -> StreamRecoder: ...
199 def iterencode(iterator: Iterable[str], encoding: str, errors: str = "strict") -> Generator[bytes]: ...
200 def iterdecode(iterator: Iterable[bytes], encoding: str, errors: str = "strict") -> Generator[str]: ...
201
202 BOM: Final[Literal[b"\xff\xfe", b"\xfe\xff"]] # depends on `sys.byteorder`
203 BOM_BE: Final = b"\xfe\xff"
204 BOM_LE: Final = b"\xff\xfe"
205 BOM_UTF8: Final = b"\xef\xbb\xbf"
206 BOM_UTF16: Final[Literal[b"\xff\xfe", b"\xfe\xff"]] # depends on `sys.byteorder`
207 BOM_UTF16_BE: Final = b"\xfe\xff"
208 BOM_UTF16_LE: Final = b"\xff\xfe"
209 BOM_UTF32: Final[Literal[b"\xff\xfe\x00\x00", b"\x00\x00\xfe\xff"]] # depends on `sys.byteorder`
210 BOM_UTF32_BE: Final = b"\x00\x00\xfe\xff"
211 BOM_UTF32_LE: Final = b"\xff\xfe\x00\x00"
212
213 def strict_errors(exception: UnicodeError, /) -> tuple[str | bytes, int]: ...
214 def replace_errors(exception: UnicodeError, /) -> tuple[str | bytes, int]: ...
215 def ignore_errors(exception: UnicodeError, /) -> tuple[str | bytes, int]: ...
216 def xmlcharrefreplace_errors(exception: UnicodeError, /) -> tuple[str | bytes, int]: ...
217 def backslashreplace_errors(exception: UnicodeError, /) -> tuple[str | bytes, int]: ...
218 def namereplace_errors(exception: UnicodeError, /) -> tuple[str | bytes, int]: ...
219
220 class Codec:
221 # These are sort of @abstractmethod but sort of not.
222 # The StreamReader and StreamWriter subclasses only implement one.
223 def encode(self, input: str, errors: str = "strict") -> tuple[bytes, int]: ...
224 def decode(self, input: bytes, errors: str = "strict") -> tuple[str, int]: ...
225
226 class IncrementalEncoder:
227 errors: str
228 def __init__(self, errors: str = "strict") -> None: ...
229 @abstractmethod
230 def encode(self, input: str, final: bool = False) -> bytes: ...
231 def reset(self) -> None: ...
232 # documentation says int but str is needed for the subclass.
233 def getstate(self) -> int | str: ...
234 def setstate(self, state: int | str) -> None: ...
235
236 class IncrementalDecoder:
237 errors: str
238 def __init__(self, errors: str = "strict") -> None: ...
239 @abstractmethod
240 def decode(self, input: ReadableBuffer, final: bool = False) -> str: ...
241 def reset(self) -> None: ...
242 def getstate(self) -> tuple[bytes, int]: ...
243 def setstate(self, state: tuple[bytes, int]) -> None: ...
244
245 # These are not documented but used in encodings/*.py implementations.
246 class BufferedIncrementalEncoder(IncrementalEncoder):
247 buffer: str
248 def __init__(self, errors: str = "strict") -> None: ...
249 @abstractmethod
250 def _buffer_encode(self, input: str, errors: str, final: bool) -> tuple[bytes, int]: ...
251 def encode(self, input: str, final: bool = False) -> bytes: ...
252
253 class BufferedIncrementalDecoder(IncrementalDecoder):
254 buffer: bytes
255 def __init__(self, errors: str = "strict") -> None: ...
256 @abstractmethod
257 def _buffer_decode(self, input: ReadableBuffer, errors: str, final: bool) -> tuple[str, int]: ...
258 def decode(self, input: ReadableBuffer, final: bool = False) -> str: ...
259
260 # TODO: it is not possible to specify the requirement that all other
261 # attributes and methods are passed-through from the stream.
262 class StreamWriter(Codec):
263 stream: _WritableStream
264 errors: str
265 def __init__(self, stream: _WritableStream, errors: str = "strict") -> None: ...
266 def write(self, object: str) -> None: ...
267 def writelines(self, list: Iterable[str]) -> None: ...
268 def reset(self) -> None: ...
269 def seek(self, offset: int, whence: int = 0) -> None: ...
270 def __enter__(self) -> Self: ...
271 def __exit__(self, type: type[BaseException] | None, value: BaseException | None, tb: types.TracebackType | None) -> None: ...
272 def __getattr__(self, name: str, getattr: Callable[[Any, str], Any] = ...) -> Any: ...
273
274 class StreamReader(Codec):
275 stream: _ReadableStream
276 errors: str
277 # This is set to str, but some subclasses set to bytes instead.
278 charbuffertype: ClassVar[type] = ...
279 def __init__(self, stream: _ReadableStream, errors: str = "strict") -> None: ...
280 def read(self, size: int = -1, chars: int = -1, firstline: bool = False) -> str: ...
281 def readline(self, size: int | None = None, keepends: bool = True) -> str: ...
282 def readlines(self, sizehint: int | None = None, keepends: bool = True) -> list[str]: ...
283 def reset(self) -> None: ...
284 def seek(self, offset: int, whence: int = 0) -> None: ...
285 def __enter__(self) -> Self: ...
286 def __exit__(self, type: type[BaseException] | None, value: BaseException | None, tb: types.TracebackType | None) -> None: ...
287 def __iter__(self) -> Self: ...
288 def __next__(self) -> str: ...
289 def __getattr__(self, name: str, getattr: Callable[[Any, str], Any] = ...) -> Any: ...
290
291 # Doesn't actually inherit from TextIO, but wraps a BinaryIO to provide text reading and writing
292 # and delegates attributes to the underlying binary stream with __getattr__.
293 class StreamReaderWriter(TextIO):
294 stream: _Stream
295 def __init__(self, stream: _Stream, Reader: _StreamReader, Writer: _StreamWriter, errors: str = "strict") -> None: ...
296 def read(self, size: int = -1) -> str: ...
297 def readline(self, size: int | None = None) -> str: ...
298 def readlines(self, sizehint: int | None = None) -> list[str]: ...
299 def __next__(self) -> str: ...
300 def __iter__(self) -> Self: ...
301 def write(self, data: str) -> None: ... # type: ignore[override]
302 def writelines(self, list: Iterable[str]) -> None: ...
303 def reset(self) -> None: ...
304 def seek(self, offset: int, whence: int = 0) -> None: ... # type: ignore[override]
305 def __enter__(self) -> Self: ...
306 def __exit__(self, type: type[BaseException] | None, value: BaseException | None, tb: types.TracebackType | None) -> None: ...
307 def __getattr__(self, name: str) -> Any: ...
308 # These methods don't actually exist directly, but they are needed to satisfy the TextIO
309 # interface. At runtime, they are delegated through __getattr__.
310 def close(self) -> None: ...
311 def fileno(self) -> int: ...
312 def flush(self) -> None: ...
313 def isatty(self) -> bool: ...
314 def readable(self) -> bool: ...
315 def truncate(self, size: int | None = ...) -> int: ...
316 def seekable(self) -> bool: ...
317 def tell(self) -> int: ...
318 def writable(self) -> bool: ...
319
320 class StreamRecoder(BinaryIO):
321 data_encoding: str
322 file_encoding: str
323 def __init__(
324 self,
325 stream: _Stream,
326 encode: _Encoder,
327 decode: _Decoder,
328 Reader: _StreamReader,
329 Writer: _StreamWriter,
330 errors: str = "strict",
331 ) -> None: ...
332 def read(self, size: int = -1) -> bytes: ...
333 def readline(self, size: int | None = None) -> bytes: ...
334 def readlines(self, sizehint: int | None = None) -> list[bytes]: ...
335 def __next__(self) -> bytes: ...
336 def __iter__(self) -> Self: ...
337 # Base class accepts more types than just bytes
338 def write(self, data: bytes) -> None: ... # type: ignore[override]
339 def writelines(self, list: Iterable[bytes]) -> None: ... # type: ignore[override]
340 def reset(self) -> None: ...
341 def __getattr__(self, name: str) -> Any: ...
342 def __enter__(self) -> Self: ...
343 def __exit__(self, type: type[BaseException] | None, value: BaseException | None, tb: types.TracebackType | None) -> None: ...
344 def seek(self, offset: int, whence: int = 0) -> None: ... # type: ignore[override]
345 # These methods don't actually exist directly, but they are needed to satisfy the BinaryIO
346 # interface. At runtime, they are delegated through __getattr__.
347 def close(self) -> None: ...
348 def fileno(self) -> int: ...
349 def flush(self) -> None: ...
350 def isatty(self) -> bool: ...
351 def readable(self) -> bool: ...
352 def truncate(self, size: int | None = ...) -> int: ...
353 def seekable(self) -> bool: ...
354 def tell(self) -> int: ...
355 def writable(self) -> bool: ...