Files
typeshed/stdlib/3/lzma.pyi
Bruce Merry e05fbabdeb Introduce ReadableBuffer and WriteableBuffer Union aliases (#4232)
Since typing doesn't yet have a way to express buffer protocol objects
(python/typing#593), various interfaces have ended up with a mish-mash
of options: some list just bytes (or just bytearray, when writable),
some include mmap, some include memoryview, I think none of them include
array.array even though it's explicitly mentioned as bytes-like, etc. I
ran into problems because RawIOBase.readinto didn't allow for
memoryview.

To allow for some uniformity until the fundamental issue is resolved,
I've introduced _typeshed.ReadableBuffer and _typeshed.WriteableBuffer,
and applied them in stdlib/3/io.pyi as an example. If these get rolled
out in more places, it will mean that we have only one place where they
have to get tweaked in future, or swapped out for a public protocol.

This unfortunately does have the potential to break code that inherits
from RawIOBase/BufferedIOBase and overrides these methods, because the
base method is now more general and so the override now needs to accept
these types as well (which is why I've also updated gzip and lzma).
However, it should be a reasonably easy fix, and will make the
downstream annotations more correct.
2020-06-19 12:45:12 +02:00

165 lines
4.5 KiB
Python

import io
from typing import IO, Any, Mapping, Optional, Sequence, TextIO, TypeVar, Union, overload
from _typeshed import AnyPath, ReadableBuffer
from typing_extensions import Literal
_OpenBinaryWritingMode = Literal["w", "wb", "x", "xb", "a", "ab"]
_OpenTextWritingMode = Literal["wt", "xt", "at"]
_PathOrFile = Union[AnyPath, IO[bytes]]
_FilterChain = Sequence[Mapping[str, Any]]
_T = TypeVar("_T")
FORMAT_AUTO: int
FORMAT_XZ: int
FORMAT_ALONE: int
FORMAT_RAW: int
CHECK_NONE: int
CHECK_CRC32: int
CHECK_CRC64: int
CHECK_SHA256: int
CHECK_ID_MAX: int
CHECK_UNKNOWN: int
FILTER_LZMA1: int
FILTER_LZMA2: int
FILTER_DELTA: int
FILTER_X86: int
FILTER_IA64: int
FILTER_ARM: int
FILTER_ARMTHUMB: int
FILTER_SPARC: int
FILTER_POWERPC: int
MF_HC3: int
MF_HC4: int
MF_BT2: int
MF_BT3: int
MF_BT4: int
MODE_FAST: int
MODE_NORMAL: int
PRESET_DEFAULT: int
PRESET_EXTREME: int
# from _lzma.c
class LZMADecompressor(object):
def __init__(
self, format: Optional[int] = ..., memlimit: Optional[int] = ..., filters: Optional[_FilterChain] = ...
) -> None: ...
def decompress(self, data: bytes, max_length: int = ...) -> bytes: ...
@property
def check(self) -> int: ...
@property
def eof(self) -> bool: ...
@property
def unused_data(self) -> bytes: ...
@property
def needs_input(self) -> bool: ...
# from _lzma.c
class LZMACompressor(object):
def __init__(
self, format: Optional[int] = ..., check: int = ..., preset: Optional[int] = ..., filters: Optional[_FilterChain] = ...
) -> None: ...
def compress(self, data: bytes) -> bytes: ...
def flush(self) -> bytes: ...
class LZMAError(Exception): ...
class LZMAFile(io.BufferedIOBase, IO[bytes]):
def __init__(
self,
filename: Optional[_PathOrFile] = ...,
mode: str = ...,
*,
format: Optional[int] = ...,
check: int = ...,
preset: Optional[int] = ...,
filters: Optional[_FilterChain] = ...,
) -> None: ...
def __enter__(self: _T) -> _T: ...
def close(self) -> None: ...
@property
def closed(self) -> bool: ...
def fileno(self) -> int: ...
def seekable(self) -> bool: ...
def readable(self) -> bool: ...
def writable(self) -> bool: ...
def peek(self, size: int = ...) -> bytes: ...
def read(self, size: Optional[int] = ...) -> bytes: ...
def read1(self, size: int = ...) -> bytes: ...
def readline(self, size: Optional[int] = ...) -> bytes: ...
def write(self, data: ReadableBuffer) -> int: ...
def seek(self, offset: int, whence: int = ...) -> int: ...
def tell(self) -> int: ...
@overload
def open(
filename: _PathOrFile,
mode: Literal["r", "rb"] = ...,
*,
format: Optional[int] = ...,
check: Literal[-1] = ...,
preset: None = ...,
filters: Optional[_FilterChain] = ...,
encoding: None = ...,
errors: None = ...,
newline: None = ...,
) -> LZMAFile: ...
@overload
def open(
filename: _PathOrFile,
mode: _OpenBinaryWritingMode,
*,
format: Optional[int] = ...,
check: int = ...,
preset: Optional[int] = ...,
filters: Optional[_FilterChain] = ...,
encoding: None = ...,
errors: None = ...,
newline: None = ...,
) -> LZMAFile: ...
@overload
def open(
filename: AnyPath,
mode: Literal["rt"],
*,
format: Optional[int] = ...,
check: Literal[-1] = ...,
preset: None = ...,
filters: Optional[_FilterChain] = ...,
encoding: Optional[str] = ...,
errors: Optional[str] = ...,
newline: Optional[str] = ...,
) -> TextIO: ...
@overload
def open(
filename: AnyPath,
mode: _OpenTextWritingMode,
*,
format: Optional[int] = ...,
check: int = ...,
preset: Optional[int] = ...,
filters: Optional[_FilterChain] = ...,
encoding: Optional[str] = ...,
errors: Optional[str] = ...,
newline: Optional[str] = ...,
) -> TextIO: ...
@overload
def open(
filename: _PathOrFile,
mode: str,
*,
format: Optional[int] = ...,
check: int = ...,
preset: Optional[int] = ...,
filters: Optional[_FilterChain] = ...,
encoding: Optional[str] = ...,
errors: Optional[str] = ...,
newline: Optional[str] = ...,
) -> Union[LZMAFile, TextIO]: ...
def compress(
data: bytes, format: int = ..., check: int = ..., preset: Optional[int] = ..., filters: Optional[_FilterChain] = ...
) -> bytes: ...
def decompress(data: bytes, format: int = ..., memlimit: Optional[int] = ..., filters: Optional[_FilterChain] = ...) -> bytes: ...
def is_check_supported(check: int) -> bool: ...