improve codecs stubs (#2114)

Started out as progress towards #1476, but I ended up fixing a few more things:
- fixed the signature of _encode_type, which actually returns a pair, not a string
- made some attributes into properties in order to prevent the descriptor protocol from turning them into methods
- found a bug in CPython in the process (python/cpython#6779)

I used the following test file to make sure these classes are now instantiable:

```python
import codecs
import io
from typing import IO

bio = io.BytesIO()
cod = codecs.lookup('utf-8')

codecs.StreamReaderWriter(bio, codecs.StreamReader, codecs.StreamWriter)
codecs.StreamRecoder(bio, cod.encode, cod.decode, codecs.StreamReader, codecs.StreamWriter)
```
This commit is contained in:
Jelle Zijlstra
2018-06-11 15:53:15 -07:00
committed by Guido van Rossum
parent d2469c0e89
commit bdb06b5b81

View File

@@ -2,6 +2,7 @@
# https://docs.python.org/2/library/codecs.html and https://docs.python.org/3/library/codecs.html
import sys
from typing import (
Any,
BinaryIO,
Callable,
Generator,
@@ -33,8 +34,8 @@ _encoded = bytes
# TODO: It is not possible to specify these signatures correctly, because
# they have an optional positional or keyword argument for errors=.
_encode_type = Callable[[_decoded], _encoded] # signature of Codec().encode
_decode_type = Callable[[_encoded], _decoded] # signature of Codec().decode
_encode_type = Callable[[_decoded], Tuple[_encoded, int]] # signature of Codec().encode
_decode_type = Callable[[_encoded], Tuple[_decoded, int]] # signature of Codec().decode
_stream_reader_type = Callable[[IO[_encoded]], 'StreamReader'] # signature of StreamReader __init__
_stream_writer_type = Callable[[IO[_encoded]], 'StreamWriter'] # signature of StreamWriter __init__
_incremental_encoder_type = Callable[[], 'IncrementalEncoder'] # signature of IncrementalEncoder __init__
@@ -49,13 +50,19 @@ def decode(obj: _encoded, encoding: str = ..., errors: str = ...) -> _decoded:
def lookup(encoding: str) -> 'CodecInfo':
...
class CodecInfo(Tuple[_encode_type, _decode_type, _stream_reader_type, _stream_writer_type]):
encode = ... # type: _encode_type
decode = ... # type: _decode_type
streamreader = ... # type: _stream_reader_type
streamwriter = ... # type: _stream_writer_type
incrementalencoder = ... # type: _incremental_encoder_type
incrementaldecoder = ... # type: _incremental_decoder_type
name = ... # type: str
@property
def encode(self) -> _encode_type: ...
@property
def decode(self) -> _decode_type: ...
@property
def streamreader(self) -> _stream_reader_type: ...
@property
def streamwriter(self) -> _stream_writer_type: ...
@property
def incrementalencoder(self) -> _incremental_encoder_type: ...
@property
def incrementaldecoder(self) -> _incremental_decoder_type: ...
name: str
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: ...
def getencoder(encoding: str) -> _encode_type:
@@ -207,7 +214,10 @@ class StreamReaderWriter(TextIO):
def read(self, size: int= ...) -> _decoded: ...
def readline(self, size: Optional[int] = ...) -> _decoded: ...
def readlines(self, sizehint: Optional[int] = ...) -> List[_decoded]: ...
def __next__(self) -> _decoded: ...
if sys.version_info >= (3,):
def __next__(self) -> Text: ...
else:
def next(self) -> Text: ...
def __iter__(self: _T) -> _T: ...
# This actually returns None, but that's incompatible with the supertype
def write(self, data: _decoded) -> int: ...
@@ -217,7 +227,49 @@ class StreamReaderWriter(TextIO):
def seek(self, offset: int, whence: int = ...) -> int: ...
def __enter__(self: _T) -> _T: ...
def __exit__(self, typ: Optional[Type[BaseException]], exc: Optional[BaseException], tb: Optional[types.TracebackType]) -> bool: ...
def __getattr__(self, name: str) -> Any: ...
# These methods don't actually exist directly, but they are needed to satisfy the TextIO
# interface. At runtime, they are delegated through __getattr__.
def close(self) -> None: ...
def fileno(self) -> int: ...
def flush(self) -> None: ...
def isatty(self) -> bool: ...
def readable(self) -> bool: ...
def truncate(self, size: Optional[int] = ...) -> int: ...
def seekable(self) -> bool: ...
def tell(self) -> int: ...
def writable(self) -> bool: ...
_SRT = TypeVar('_SRT', bound=StreamRecoder)
class StreamRecoder(BinaryIO):
def __init__(self, stream: IO[_encoded], encode: _encode_type, decode: _decode_type, Reader: _stream_reader_type, Writer: _stream_writer_type, errors: str = ...) -> None:
...
def read(self, size: int = ...) -> bytes: ...
def readline(self, size: Optional[int] = ...) -> bytes: ...
def readlines(self, sizehint: Optional[int] = ...) -> List[bytes]: ...
if sys.version_info >= (3,):
def __next__(self) -> bytes: ...
else:
def next(self) -> bytes: ...
def __iter__(self: _SRT) -> _SRT: ...
def write(self, data: bytes) -> int: ...
def writelines(self, list: Iterable[bytes]) -> int: ... # type: ignore # it's supposed to return None
def reset(self) -> None: ...
def __getattr__(self, name: str) -> Any: ...
def __enter__(self: _SRT) -> _SRT: ...
def __exit__(self, type: Optional[Type[BaseException]], value: Optional[BaseException], tb: Optional[types.TracebackType]) -> bool: ...
# These methods don't actually exist directly, but they are needed to satisfy the BinaryIO
# interface. At runtime, they are delegated through __getattr__.
def seek(self, offset: int, whence: int = ...) -> int: ...
def close(self) -> None: ...
def fileno(self) -> int: ...
def flush(self) -> None: ...
def isatty(self) -> bool: ...
def readable(self) -> bool: ...
def truncate(self, size: Optional[int] = ...) -> int: ...
def seekable(self) -> bool: ...
def tell(self) -> int: ...
def writable(self) -> bool: ...