2
2
# https://docs.python.org/2/library/codecs.html and https://docs.python.org/3/library/codecs.html
3
3
import sys
4
4
from typing import (
5
+ Any ,
5
6
BinaryIO ,
6
7
Callable ,
7
8
Generator ,
@@ -33,8 +34,8 @@ _encoded = bytes
33
34
34
35
# TODO: It is not possible to specify these signatures correctly, because
35
36
# they have an optional positional or keyword argument for errors=.
36
- _encode_type = Callable [[_decoded ], _encoded ] # signature of Codec().encode
37
- _decode_type = Callable [[_encoded ], _decoded ] # signature of Codec().decode
37
+ _encode_type = Callable [[_decoded ], Tuple [ _encoded , int ] ] # signature of Codec().encode
38
+ _decode_type = Callable [[_encoded ], Tuple [ _decoded , int ] ] # signature of Codec().decode
38
39
_stream_reader_type = Callable [[IO [_encoded ]], 'StreamReader' ] # signature of StreamReader __init__
39
40
_stream_writer_type = Callable [[IO [_encoded ]], 'StreamWriter' ] # signature of StreamWriter __init__
40
41
_incremental_encoder_type = Callable [[], 'IncrementalEncoder' ] # signature of IncrementalEncoder __init__
@@ -49,13 +50,19 @@ def decode(obj: _encoded, encoding: str = ..., errors: str = ...) -> _decoded:
49
50
def lookup (encoding : str ) -> 'CodecInfo' :
50
51
...
51
52
class CodecInfo (Tuple [_encode_type , _decode_type , _stream_reader_type , _stream_writer_type ]):
52
- encode = ... # type: _encode_type
53
- decode = ... # type: _decode_type
54
- streamreader = ... # type: _stream_reader_type
55
- streamwriter = ... # type: _stream_writer_type
56
- incrementalencoder = ... # type: _incremental_encoder_type
57
- incrementaldecoder = ... # type: _incremental_decoder_type
58
- name = ... # type: str
53
+ @property
54
+ def encode (self ) -> _encode_type : ...
55
+ @property
56
+ def decode (self ) -> _decode_type : ...
57
+ @property
58
+ def streamreader (self ) -> _stream_reader_type : ...
59
+ @property
60
+ def streamwriter (self ) -> _stream_writer_type : ...
61
+ @property
62
+ def incrementalencoder (self ) -> _incremental_encoder_type : ...
63
+ @property
64
+ def incrementaldecoder (self ) -> _incremental_decoder_type : ...
65
+ name : str
59
66
def __init__ (self , encode : _encode_type , decode : _decode_type , streamreader : _stream_reader_type = ..., streamwriter : _stream_writer_type = ..., incrementalencoder : _incremental_encoder_type = ..., incrementaldecoder : _incremental_decoder_type = ..., name : str = ...) -> None : ...
60
67
61
68
def getencoder (encoding : str ) -> _encode_type :
@@ -207,7 +214,10 @@ class StreamReaderWriter(TextIO):
207
214
def read (self , size : int = ...) -> _decoded : ...
208
215
def readline (self , size : Optional [int ] = ...) -> _decoded : ...
209
216
def readlines (self , sizehint : Optional [int ] = ...) -> List [_decoded ]: ...
210
- def __next__ (self ) -> _decoded : ...
217
+ if sys .version_info >= (3 ,):
218
+ def __next__ (self ) -> Text : ...
219
+ else :
220
+ def next (self ) -> Text : ...
211
221
def __iter__ (self : _T ) -> _T : ...
212
222
# This actually returns None, but that's incompatible with the supertype
213
223
def write (self , data : _decoded ) -> int : ...
@@ -217,7 +227,49 @@ class StreamReaderWriter(TextIO):
217
227
def seek (self , offset : int , whence : int = ...) -> int : ...
218
228
def __enter__ (self : _T ) -> _T : ...
219
229
def __exit__ (self , typ : Optional [Type [BaseException ]], exc : Optional [BaseException ], tb : Optional [types .TracebackType ]) -> bool : ...
230
+ def __getattr__ (self , name : str ) -> Any : ...
231
+
232
+ # These methods don't actually exist directly, but they are needed to satisfy the TextIO
233
+ # interface. At runtime, they are delegated through __getattr__.
234
+ def close (self ) -> None : ...
235
+ def fileno (self ) -> int : ...
236
+ def flush (self ) -> None : ...
237
+ def isatty (self ) -> bool : ...
238
+ def readable (self ) -> bool : ...
239
+ def truncate (self , size : Optional [int ] = ...) -> int : ...
240
+ def seekable (self ) -> bool : ...
241
+ def tell (self ) -> int : ...
242
+ def writable (self ) -> bool : ...
243
+
244
+ _SRT = TypeVar ('_SRT' , bound = StreamRecoder )
220
245
221
246
class StreamRecoder (BinaryIO ):
222
247
def __init__ (self , stream : IO [_encoded ], encode : _encode_type , decode : _decode_type , Reader : _stream_reader_type , Writer : _stream_writer_type , errors : str = ...) -> None :
223
248
...
249
+ def read (self , size : int = ...) -> bytes : ...
250
+ def readline (self , size : Optional [int ] = ...) -> bytes : ...
251
+ def readlines (self , sizehint : Optional [int ] = ...) -> List [bytes ]: ...
252
+ if sys .version_info >= (3 ,):
253
+ def __next__ (self ) -> bytes : ...
254
+ else :
255
+ def next (self ) -> bytes : ...
256
+ def __iter__ (self : _SRT ) -> _SRT : ...
257
+ def write (self , data : bytes ) -> int : ...
258
+ def writelines (self , list : Iterable [bytes ]) -> int : ... # type: ignore # it's supposed to return None
259
+ def reset (self ) -> None : ...
260
+ def __getattr__ (self , name : str ) -> Any : ...
261
+ def __enter__ (self : _SRT ) -> _SRT : ...
262
+ def __exit__ (self , type : Optional [Type [BaseException ]], value : Optional [BaseException ], tb : Optional [types .TracebackType ]) -> bool : ...
263
+
264
+ # These methods don't actually exist directly, but they are needed to satisfy the BinaryIO
265
+ # interface. At runtime, they are delegated through __getattr__.
266
+ def seek (self , offset : int , whence : int = ...) -> int : ...
267
+ def close (self ) -> None : ...
268
+ def fileno (self ) -> int : ...
269
+ def flush (self ) -> None : ...
270
+ def isatty (self ) -> bool : ...
271
+ def readable (self ) -> bool : ...
272
+ def truncate (self , size : Optional [int ] = ...) -> int : ...
273
+ def seekable (self ) -> bool : ...
274
+ def tell (self ) -> int : ...
275
+ def writable (self ) -> bool : ...
0 commit comments