mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-06 20:24:30 +08:00
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.
93 lines
2.8 KiB
Python
93 lines
2.8 KiB
Python
import sys
|
|
import zlib
|
|
from typing import IO, Optional, TextIO, Union, overload
|
|
import _compression
|
|
from _typeshed import AnyPath, ReadableBuffer
|
|
from typing_extensions import Literal
|
|
|
|
_OpenBinaryMode = Literal["r", "rb", "a", "ab", "w", "wb", "x", "xb"]
|
|
_OpenTextMode = Literal["rt", "at", "wt", "xt"]
|
|
|
|
@overload
|
|
def open(
|
|
filename: Union[AnyPath, IO[bytes]],
|
|
mode: _OpenBinaryMode = ...,
|
|
compresslevel: int = ...,
|
|
encoding: None = ...,
|
|
errors: None = ...,
|
|
newline: None = ...,
|
|
) -> GzipFile: ...
|
|
@overload
|
|
def open(
|
|
filename: AnyPath,
|
|
mode: _OpenTextMode,
|
|
compresslevel: int = ...,
|
|
encoding: Optional[str] = ...,
|
|
errors: Optional[str] = ...,
|
|
newline: Optional[str] = ...,
|
|
) -> TextIO: ...
|
|
@overload
|
|
def open(
|
|
filename: Union[AnyPath, IO[bytes]],
|
|
mode: str,
|
|
compresslevel: int = ...,
|
|
encoding: Optional[str] = ...,
|
|
errors: Optional[str] = ...,
|
|
newline: Optional[str] = ...,
|
|
) -> Union[GzipFile, TextIO]: ...
|
|
|
|
class _PaddedFile:
|
|
file: IO[bytes]
|
|
def __init__(self, f: IO[bytes], prepend: bytes = ...) -> None: ...
|
|
def read(self, size: int) -> bytes: ...
|
|
def prepend(self, prepend: bytes = ...) -> None: ...
|
|
def seek(self, off: int) -> int: ...
|
|
def seekable(self) -> bool: ...
|
|
|
|
class GzipFile(_compression.BaseStream):
|
|
myfileobj: Optional[IO[bytes]]
|
|
mode: str
|
|
name: str
|
|
compress: zlib._Compress
|
|
fileobj: IO[bytes]
|
|
def __init__(
|
|
self,
|
|
filename: Optional[AnyPath] = ...,
|
|
mode: Optional[str] = ...,
|
|
compresslevel: int = ...,
|
|
fileobj: Optional[IO[bytes]] = ...,
|
|
mtime: Optional[float] = ...,
|
|
) -> None: ...
|
|
@property
|
|
def filename(self) -> str: ...
|
|
@property
|
|
def mtime(self) -> Optional[int]: ...
|
|
crc: int
|
|
def write(self, data: ReadableBuffer) -> int: ...
|
|
def read(self, size: Optional[int] = ...) -> bytes: ...
|
|
def read1(self, size: int = ...) -> bytes: ...
|
|
def peek(self, n: int) -> bytes: ...
|
|
@property
|
|
def closed(self) -> bool: ...
|
|
def close(self) -> None: ...
|
|
def flush(self, zlib_mode: int = ...) -> None: ...
|
|
def fileno(self) -> int: ...
|
|
def rewind(self) -> None: ...
|
|
def readable(self) -> bool: ...
|
|
def writable(self) -> bool: ...
|
|
def seekable(self) -> bool: ...
|
|
def seek(self, offset: int, whence: int = ...) -> int: ...
|
|
def readline(self, size: Optional[int] = ...) -> bytes: ...
|
|
|
|
class _GzipReader(_compression.DecompressReader):
|
|
def __init__(self, fp: IO[bytes]) -> None: ...
|
|
def read(self, size: int = ...) -> bytes: ...
|
|
|
|
if sys.version_info >= (3, 8):
|
|
def compress(data, compresslevel: int = ..., *, mtime: Optional[float] = ...) -> bytes: ...
|
|
|
|
else:
|
|
def compress(data, compresslevel: int = ...) -> bytes: ...
|
|
|
|
def decompress(data: bytes) -> bytes: ...
|