Replace 'Text' with 'str' in py3 stdlib (#5466)

This commit is contained in:
Sebastian Rittau
2021-05-16 16:10:48 +02:00
committed by GitHub
parent dbe77b6ae9
commit 6a9c89e928
49 changed files with 328 additions and 349 deletions

View File

@@ -11,7 +11,6 @@ from typing import (
List,
Optional,
Protocol,
Text,
TextIO,
Tuple,
Type,
@@ -22,25 +21,22 @@ from typing import (
from typing_extensions import Literal
# TODO: this only satisfies the most common interface, where
# bytes (py2 str) is the raw form and str (py2 unicode) is the cooked form.
# bytes is the raw form and str is the cooked form.
# In the long run, both should become template parameters maybe?
# There *are* bytes->bytes and str->str encodings in the standard library.
# They are much more common in Python 2 than in Python 3.
_Decoded = Text
_Encoded = bytes
class _Encoder(Protocol):
def __call__(self, input: _Decoded, errors: str = ...) -> Tuple[_Encoded, int]: ... # signature of Codec().encode
def __call__(self, input: str, errors: str = ...) -> Tuple[bytes, int]: ... # signature of Codec().encode
class _Decoder(Protocol):
def __call__(self, input: _Encoded, errors: str = ...) -> Tuple[_Decoded, int]: ... # signature of Codec().decode
def __call__(self, input: bytes, errors: str = ...) -> Tuple[str, int]: ... # signature of Codec().decode
class _StreamReader(Protocol):
def __call__(self, stream: IO[_Encoded], errors: str = ...) -> StreamReader: ...
def __call__(self, stream: IO[bytes], errors: str = ...) -> StreamReader: ...
class _StreamWriter(Protocol):
def __call__(self, stream: IO[_Encoded], errors: str = ...) -> StreamWriter: ...
def __call__(self, stream: IO[bytes], errors: str = ...) -> StreamWriter: ...
class _IncrementalEncoder(Protocol):
def __call__(self, errors: str = ...) -> IncrementalEncoder: ...
@@ -74,18 +70,16 @@ def encode(obj: bytes, encoding: _BytesToBytesEncodingT, errors: str = ...) -> b
@overload
def encode(obj: str, encoding: Literal["rot13", "rot_13"] = ..., errors: str = ...) -> str: ... # type: ignore
@overload
def encode(obj: _Decoded, encoding: str = ..., errors: str = ...) -> _Encoded: ...
def encode(obj: str, encoding: str = ..., errors: str = ...) -> bytes: ...
@overload
def decode(obj: bytes, encoding: _BytesToBytesEncodingT, errors: str = ...) -> bytes: ... # type: ignore
@overload
def decode(obj: str, encoding: Literal["rot13", "rot_13"] = ..., errors: str = ...) -> Text: ...
def decode(obj: str, encoding: Literal["rot13", "rot_13"] = ..., errors: str = ...) -> str: ...
@overload
def decode(obj: _Encoded, encoding: str = ..., errors: str = ...) -> _Decoded: ...
def decode(obj: bytes, encoding: str = ..., errors: str = ...) -> str: ...
def lookup(__encoding: str) -> CodecInfo: ...
def utf_16_be_decode(
__data: _Encoded, __errors: Optional[str] = ..., __final: bool = ...
) -> Tuple[_Decoded, int]: ... # undocumented
def utf_16_be_encode(__str: _Decoded, __errors: Optional[str] = ...) -> Tuple[_Encoded, int]: ... # undocumented
def utf_16_be_decode(__data: bytes, __errors: Optional[str] = ..., __final: bool = ...) -> Tuple[str, int]: ... # undocumented
def utf_16_be_encode(__str: str, __errors: Optional[str] = ...) -> Tuple[bytes, int]: ... # undocumented
class CodecInfo(Tuple[_Encoder, _Decoder, _StreamReader, _StreamWriter]):
@property
@@ -124,11 +118,9 @@ def register(__search_function: Callable[[str], Optional[CodecInfo]]) -> None: .
def open(
filename: str, mode: str = ..., encoding: Optional[str] = ..., errors: str = ..., buffering: int = ...
) -> StreamReaderWriter: ...
def EncodedFile(
file: IO[_Encoded], data_encoding: str, file_encoding: Optional[str] = ..., errors: str = ...
) -> StreamRecoder: ...
def iterencode(iterator: Iterable[_Decoded], encoding: str, errors: str = ...) -> Generator[_Encoded, None, None]: ...
def iterdecode(iterator: Iterable[_Encoded], encoding: str, errors: str = ...) -> Generator[_Decoded, None, None]: ...
def EncodedFile(file: IO[bytes], data_encoding: str, file_encoding: Optional[str] = ..., errors: str = ...) -> StreamRecoder: ...
def iterencode(iterator: Iterable[str], encoding: str, errors: str = ...) -> Generator[bytes, None, None]: ...
def iterdecode(iterator: Iterable[bytes], encoding: str, errors: str = ...) -> Generator[str, None, None]: ...
BOM: bytes
BOM_BE: bytes
@@ -155,42 +147,42 @@ def backslashreplace_errors(exception: UnicodeError) -> Tuple[Union[str, bytes],
class Codec:
# These are sort of @abstractmethod but sort of not.
# The StreamReader and StreamWriter subclasses only implement one.
def encode(self, input: _Decoded, errors: str = ...) -> Tuple[_Encoded, int]: ...
def decode(self, input: _Encoded, errors: str = ...) -> Tuple[_Decoded, int]: ...
def encode(self, input: str, errors: str = ...) -> Tuple[bytes, int]: ...
def decode(self, input: bytes, errors: str = ...) -> Tuple[str, int]: ...
class IncrementalEncoder:
errors: str
def __init__(self, errors: str = ...) -> None: ...
@abstractmethod
def encode(self, input: _Decoded, final: bool = ...) -> _Encoded: ...
def encode(self, input: str, final: bool = ...) -> bytes: ...
def reset(self) -> None: ...
# documentation says int but str is needed for the subclass.
def getstate(self) -> Union[int, _Decoded]: ...
def setstate(self, state: Union[int, _Decoded]) -> None: ...
def getstate(self) -> Union[int, str]: ...
def setstate(self, state: Union[int, str]) -> None: ...
class IncrementalDecoder:
errors: str
def __init__(self, errors: str = ...) -> None: ...
@abstractmethod
def decode(self, input: _Encoded, final: bool = ...) -> _Decoded: ...
def decode(self, input: bytes, final: bool = ...) -> str: ...
def reset(self) -> None: ...
def getstate(self) -> Tuple[_Encoded, int]: ...
def setstate(self, state: Tuple[_Encoded, int]) -> None: ...
def getstate(self) -> Tuple[bytes, int]: ...
def setstate(self, state: Tuple[bytes, int]) -> None: ...
# These are not documented but used in encodings/*.py implementations.
class BufferedIncrementalEncoder(IncrementalEncoder):
buffer: str
def __init__(self, errors: str = ...) -> None: ...
@abstractmethod
def _buffer_encode(self, input: _Decoded, errors: str, final: bool) -> _Encoded: ...
def encode(self, input: _Decoded, final: bool = ...) -> _Encoded: ...
def _buffer_encode(self, input: str, errors: str, final: bool) -> bytes: ...
def encode(self, input: str, final: bool = ...) -> bytes: ...
class BufferedIncrementalDecoder(IncrementalDecoder):
buffer: bytes
def __init__(self, errors: str = ...) -> None: ...
@abstractmethod
def _buffer_decode(self, input: _Encoded, errors: str, final: bool) -> Tuple[_Decoded, int]: ...
def decode(self, input: _Encoded, final: bool = ...) -> _Decoded: ...
def _buffer_decode(self, input: bytes, errors: str, final: bool) -> Tuple[str, int]: ...
def decode(self, input: bytes, final: bool = ...) -> str: ...
_SW = TypeVar("_SW", bound=StreamWriter)
@@ -198,9 +190,9 @@ _SW = TypeVar("_SW", bound=StreamWriter)
# attributes and methods are passed-through from the stream.
class StreamWriter(Codec):
errors: str
def __init__(self, stream: IO[_Encoded], errors: str = ...) -> None: ...
def write(self, object: _Decoded) -> None: ...
def writelines(self, list: Iterable[_Decoded]) -> None: ...
def __init__(self, stream: IO[bytes], errors: str = ...) -> None: ...
def write(self, object: str) -> None: ...
def writelines(self, list: Iterable[str]) -> None: ...
def reset(self) -> None: ...
def __enter__(self: _SW) -> _SW: ...
def __exit__(
@@ -212,16 +204,16 @@ _SR = TypeVar("_SR", bound=StreamReader)
class StreamReader(Codec):
errors: str
def __init__(self, stream: IO[_Encoded], errors: str = ...) -> None: ...
def read(self, size: int = ..., chars: int = ..., firstline: bool = ...) -> _Decoded: ...
def readline(self, size: Optional[int] = ..., keepends: bool = ...) -> _Decoded: ...
def readlines(self, sizehint: Optional[int] = ..., keepends: bool = ...) -> List[_Decoded]: ...
def __init__(self, stream: IO[bytes], errors: str = ...) -> None: ...
def read(self, size: int = ..., chars: int = ..., firstline: bool = ...) -> str: ...
def readline(self, size: Optional[int] = ..., keepends: bool = ...) -> str: ...
def readlines(self, sizehint: Optional[int] = ..., keepends: bool = ...) -> List[str]: ...
def reset(self) -> None: ...
def __enter__(self: _SR) -> _SR: ...
def __exit__(
self, typ: Optional[Type[BaseException]], exc: Optional[BaseException], tb: Optional[types.TracebackType]
) -> None: ...
def __iter__(self) -> Iterator[_Decoded]: ...
def __iter__(self) -> Iterator[str]: ...
def __getattr__(self, name: str, getattr: Callable[[str], Any] = ...) -> Any: ...
_T = TypeVar("_T", bound=StreamReaderWriter)
@@ -229,15 +221,15 @@ _T = TypeVar("_T", bound=StreamReaderWriter)
# Doesn't actually inherit from TextIO, but wraps a BinaryIO to provide text reading and writing
# and delegates attributes to the underlying binary stream with __getattr__.
class StreamReaderWriter(TextIO):
def __init__(self, stream: IO[_Encoded], Reader: _StreamReader, Writer: _StreamWriter, errors: str = ...) -> None: ...
def read(self, size: int = ...) -> _Decoded: ...
def readline(self, size: Optional[int] = ...) -> _Decoded: ...
def readlines(self, sizehint: Optional[int] = ...) -> List[_Decoded]: ...
def __next__(self) -> Text: ...
def __init__(self, stream: IO[bytes], Reader: _StreamReader, Writer: _StreamWriter, errors: str = ...) -> None: ...
def read(self, size: int = ...) -> str: ...
def readline(self, size: Optional[int] = ...) -> str: ...
def readlines(self, sizehint: Optional[int] = ...) -> List[str]: ...
def __next__(self) -> str: ...
def __iter__(self: _T) -> _T: ...
# This actually returns None, but that's incompatible with the supertype
def write(self, data: _Decoded) -> int: ...
def writelines(self, list: Iterable[_Decoded]) -> None: ...
def write(self, data: str) -> int: ...
def writelines(self, list: Iterable[str]) -> None: ...
def reset(self) -> None: ...
# Same as write()
def seek(self, offset: int, whence: int = ...) -> int: ...
@@ -263,7 +255,7 @@ _SRT = TypeVar("_SRT", bound=StreamRecoder)
class StreamRecoder(BinaryIO):
def __init__(
self,
stream: IO[_Encoded],
stream: IO[bytes],
encode: _Encoder,
decode: _Decoder,
Reader: _StreamReader,