-
Notifications
You must be signed in to change notification settings - Fork 76
feat: added stubs for type checkers #223 #295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -21,6 +21,7 @@ | |||||
BLOCKSIZE_MAX4MB as _BLOCKSIZE_MAX4MB, | ||||||
__doc__ as _doc | ||||||
) | ||||||
from typing import Union, IO, Any | ||||||
|
||||||
__doc__ = _doc | ||||||
|
||||||
|
@@ -153,13 +154,13 @@ class LZ4FrameCompressor(object): | |||||
""" | ||||||
|
||||||
def __init__(self, | ||||||
block_size=BLOCKSIZE_DEFAULT, | ||||||
block_linked=True, | ||||||
compression_level=COMPRESSIONLEVEL_MIN, | ||||||
content_checksum=False, | ||||||
block_checksum=False, | ||||||
auto_flush=False, | ||||||
return_bytearray=False): | ||||||
block_size: int = BLOCKSIZE_DEFAULT, | ||||||
block_linked: bool = True, | ||||||
compression_level: int = COMPRESSIONLEVEL_MIN, | ||||||
content_checksum: bool = False, | ||||||
block_checksum: bool = False, | ||||||
auto_flush: bool = False, | ||||||
return_bytearray: bool = False): | ||||||
self.block_size = block_size | ||||||
self.block_linked = block_linked | ||||||
self.compression_level = compression_level | ||||||
|
@@ -190,7 +191,7 @@ def __exit__(self, exception_type, exception, traceback): | |||||
self._context = None | ||||||
self._started = False | ||||||
|
||||||
def begin(self, source_size=0): | ||||||
def begin(self, source_size: int = 0) -> Union[bytes, bytearray]: | ||||||
"""Begin a compression frame. | ||||||
|
||||||
The returned data contains frame header information. The data returned | ||||||
|
@@ -209,7 +210,7 @@ def begin(self, source_size=0): | |||||
""" | ||||||
|
||||||
if self._started is False: | ||||||
self._context = create_compression_context() | ||||||
self._context: Any = create_compression_context() | ||||||
result = compress_begin( | ||||||
self._context, | ||||||
block_size=self.block_size, | ||||||
|
@@ -228,7 +229,7 @@ def begin(self, source_size=0): | |||||
"LZ4FrameCompressor.begin() called after already initialized" | ||||||
) | ||||||
|
||||||
def compress(self, data): # noqa: F811 | ||||||
def compress(self, data: Union[str, bytes, memoryview]) -> Union[bytes, bytearray]: # noqa: F811 | ||||||
"""Compresses data and returns it. | ||||||
|
||||||
This compresses ``data`` (a ``bytes`` object), returning a bytes or | ||||||
|
@@ -262,7 +263,7 @@ def compress(self, data): # noqa: F811 | |||||
|
||||||
return result | ||||||
|
||||||
def flush(self): | ||||||
def flush(self) -> Union[bytes, bytearray]: | ||||||
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong. |
||||||
"""Finish the compression process. | ||||||
|
||||||
This returns a ``bytes`` or ``bytearray`` object con 8000 taining any data | ||||||
|
@@ -294,7 +295,7 @@ def reset(self): | |||||
self._context = None | ||||||
self._started = False | ||||||
|
||||||
def has_context(self): | ||||||
def has_context(self) -> bool: | ||||||
"""Return whether the compression context exists. | ||||||
|
||||||
Returns: | ||||||
|
@@ -303,7 +304,7 @@ def has_context(self): | |||||
""" | ||||||
return self._context is not None | ||||||
|
||||||
def started(self): | ||||||
def started(self) -> bool: | ||||||
"""Return whether the compression frame has been started. | ||||||
|
||||||
Returns: | ||||||
|
@@ -337,13 +338,13 @@ class LZ4FrameDecompressor(object): | |||||
|
||||||
""" | ||||||
|
||||||
def __init__(self, return_bytearray=False): | ||||||
def __init__(self, return_bytearray: bool = False): | ||||||
self._context = create_decompression_context() | ||||||
self.eof = False | ||||||
self.needs_input = True | ||||||
self.unused_data = None | ||||||
self._unconsumed_data = b'' | ||||||
self._return_bytearray = return_bytearray | ||||||
self.eof: bool = False | ||||||
self.needs_input: bool = True | ||||||
self.unused_data: bytes = None | ||||||
self._unconsumed_data: bytes = b'' | ||||||
self._return_bytearray: bool = return_bytearray | ||||||
|
||||||
def __enter__(self): | ||||||
# All necessary initialization is done in __init__ | ||||||
|
@@ -369,7 +370,7 @@ def reset(self): | |||||
self.unused_data = None | ||||||
self._unconsumed_data = b'' | ||||||
|
||||||
def decompress(self, data, max_length=-1): # noqa: F811 | ||||||
def decompress(self, data: Union[str, bytes, memoryview], max_length: int = -1) -> bytes: # noqa: F811 | ||||||
"""Decompresses part or all of an LZ4 frame of compressed data. | ||||||
|
||||||
The returned data should be concatenated with the output of any | ||||||
|
@@ -434,10 +435,10 @@ def decompress(self, data, max_length=-1): # noqa: F811 | |||||
return decompressed | ||||||
|
||||||
|
||||||
_MODE_CLOSED = 0 | ||||||
_MODE_READ = 1 | ||||||
_MODE_CLOSED: int = 0 | ||||||
_MODE_READ: int = 1 | ||||||
# Value 2 no longer used | ||||||
_MODE_WRITE = 3 | ||||||
_MODE_WRITE: int = 3 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same applies to the others
Suggested change
|
||||||
|
||||||
|
||||||
class LZ4FrameFile(_compression.BaseStream): | ||||||
|
@@ -488,15 +489,15 @@ class LZ4FrameFile(_compression.BaseStream): | |||||
|
||||||
""" | ||||||
|
||||||
def __init__(self, filename=None, mode='r', | ||||||
block_size=BLOCKSIZE_DEFAULT, | ||||||
block_linked=True, | ||||||
compression_level=COMPRESSIONLEVEL_MIN, | ||||||
content_checksum=False, | ||||||
block_checksum=False, | ||||||
auto_flush=False, | ||||||
return_bytearray=False, | ||||||
source_size=0): | ||||||
def __init__(self, filename: Union[str, bytes, os.PathLike, IO[Any]] = None, mode: str = 'r', | ||||||
block_size: int = BLOCKSIZE_DEFAULT, | ||||||
block_linked: bool = True, | ||||||
compression_level: int = COMPRESSIONLEVEL_MIN, | ||||||
content_checksum: bool = False, | ||||||
block_checksum: bool = False, | ||||||
auto_flush: bool = False, | ||||||
return_bytearray: bool = False, | ||||||
source_size: int = 0): | ||||||
|
||||||
self._fp = None | ||||||
self._closefp = False | ||||||
|
@@ -571,7 +572,7 @@ def close(self): | |||||
self._mode = _MODE_CLOSED | ||||||
|
||||||
@property | ||||||
def closed(self): | ||||||
def closed(self) -> bool: | ||||||
"""Returns ``True`` if this file is closed. | ||||||
|
||||||
Returns: | ||||||
|
@@ -580,7 +581,7 @@ def closed(self): | |||||
""" | ||||||
return self._mode == _MODE_CLOSED | ||||||
|
||||||
def fileno(self): | ||||||
def fileno(self) -> int: | ||||||
"""Return the file descriptor for the underlying file. | ||||||
|
||||||
Returns: | ||||||
|
@@ -590,7 +591,7 @@ def fileno(self): | |||||
self._check_not_closed() | ||||||
return self._fp.fileno() | ||||||
|
||||||
def seekable(self): | ||||||
def seekable(self) -> bool: | ||||||
"""Return whether the file supports seeking. | ||||||
|
||||||
Returns: | ||||||
|
@@ -599,7 +600,7 @@ def seekable(self): | |||||
""" | ||||||
return self.readable() and self._buffer.seekable() | ||||||
|
||||||
def readable(self): | ||||||
def readable(self) -> bool: | ||||||
"""Return whether the file was opened for reading. | ||||||
|
||||||
Returns: | ||||||
|
@@ -610,7 +611,7 @@ def readable(self): | |||||
self._check_not_closed() | ||||||
return self._mode == _MODE_READ | ||||||
|
||||||
def writable(self): | ||||||
def writable(self) -> bool: | ||||||
"""Return whether the file was opened for writing. | ||||||
|
||||||
Returns: | ||||||
|
@@ -621,7 +622,7 @@ def writable(self): | |||||
self._check_not_closed() | ||||||
return self._mode == _MODE_WRITE | ||||||
|
||||||
def peek(self, size=-1): | ||||||
def peek(self, size: int = -1) -> bytes: | ||||||
"""Return buffered data without advancing the file position. | ||||||
9E88 |
|
|||||
Always returns at least one byte of data, unless at EOF. The exact | ||||||
|
@@ -636,7 +637,7 @@ def peek(self, size=-1): | |||||
# returns at least one byte (except at EOF) | ||||||
return self._buffer.peek(size) | ||||||
|
||||||
def readall(self): | ||||||
def readall(self) -> bytes: | ||||||
chunks = bytearray() | ||||||
|
||||||
while True: | ||||||
|
@@ -647,7 +648,7 @@ def readall(self): | |||||
|
||||||
return bytes(chunks) | ||||||
|
||||||
def read(self, size=-1): | ||||||
def read(self, size: int = -1) -> bytes: | ||||||
"""Read up to ``size`` uncompressed bytes from the file. | ||||||
|
||||||
If ``size`` is negative or omitted, read until ``EOF`` is reached. | ||||||
|
@@ -667,7 +668,7 @@ def read(self, size=-1): | |||||
return self.readall() | ||||||
return self._buffer.read(size) | ||||||
|
||||||
def read1(self, size=-1): | ||||||
def read1(self, size: int = -1) -> bytes: | ||||||
"""Read up to ``size`` uncompressed bytes. | ||||||
|
||||||
This method tries to avoid making multiple reads from the underlying | ||||||
|
@@ -691,7 +692,7 @@ def read1(self, size=-1): | |||||
size = io.DEFAULT_BUFFER_SIZE | ||||||
return self._buffer.read1(size) | ||||||
|
||||||
def readline(self, size=-1): | ||||||
def readline(self, size: int = -1) -> bytes: | ||||||
"""Read a line of uncompressed bytes from the file. | ||||||
|
||||||
The terminating newline (if present) is retained. If size is | ||||||
|
@@ -709,7 +710,7 @@ def readline(self, size=-1): | |||||
self._check_can_read() | ||||||
return self._buffer.readline(size) | ||||||
|
||||||
def write(self, data): | ||||||
def write(self, data: bytes) -> int: | ||||||
"""Write a bytes object to the file. | ||||||
|
||||||
Returns the number of uncompressed bytes written, which is | ||||||
|
@@ -752,7 +753,7 @@ def flush(self): | |||||
self._fp.write(self._compressor.flush()) | ||||||
self._fp.flush() | ||||||
|
||||||
def seek(self, offset, whence=io.SEEK_SET): | ||||||
def seek(self, offset: int, whence: int = io.SEEK_SET) -> int: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||||||
"""Change the file position. | ||||||
|
||||||
The new position is specified by ``offset``, relative to the position | ||||||
|
@@ -780,7 +781,7 @@ def seek(self, offset, whence=io.SEEK_SET): | |||||
self._check_can_seek() | ||||||
return self._buffer.seek(offset, whence) | ||||||
|
||||||
def tell(self): | ||||||
def tell(self) -> int: | ||||||
"""Return the current file position. | ||||||
|
||||||
Args: | ||||||
|
@@ -796,18 +797,18 @@ def tell(self): | |||||
return self._pos | ||||||
|
||||||
|
||||||
def open(filename, mode="rb", | ||||||
encoding=None, | ||||||
errors=None, | ||||||
newline=None, | ||||||
block_size=BLOCKSIZE_DEFAULT, | ||||||
block_linked=True, | ||||||
compression_level=COMPRESSIONLEVEL_MIN, | ||||||
content_checksum=False, | ||||||
block_checksum=False, | ||||||
auto_flush=False, | ||||||
return_bytearray=False, | ||||||
source_size=0): | ||||||
def open(filename, mode: str = "rb", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly to others, the right type for open would be: When adding this, don't forget to make your life easier by doing: OpenModes = Union[Literal["w"], ...] |
||||||
encoding: str = None, | ||||||
errors: str = None, | ||||||
newline: str = None, | ||||||
block_size: int = BLOCKSIZE_DEFAULT, | ||||||
block_linked: bool = True, | ||||||
compression_level: int = COMPRESSIONLEVEL_MIN, | ||||||
content_checksum: bool = False, | ||||||
block_checksum: bool = False, | ||||||
auto_flush: bool = False, | ||||||
return_bytearray: bool = False, | ||||||
source_size: int = 0) -> Union[io.TextIOWrapper, LZ4FrameFile]: | ||||||
"""Open an LZ4Frame-compressed file in binary or text mode. | ||||||
|
||||||
``filename`` can be either an actual file name (given as a str, bytes, or | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,6 @@ | ||||||
from ._stream import _create_context, _compress, _decompress, _get_block | ||||||
from ._stream import LZ4StreamError, _compress_bound, _input_bound, LZ4_MAX_INPUT_SIZE # noqa: F401 | ||||||
|
||||||
from typing import Union | ||||||
|
||||||
__doc__ = """\ | ||||||
A Python wrapper for the LZ4 stream protocol. | ||||||
|
@@ -12,7 +12,9 @@ class LZ4StreamDecompressor: | |||||
""" LZ4 stream decompression context. | ||||||
|
||||||
""" | ||||||
def __init__(self, strategy, buffer_size, return_bytearray=False, store_comp_size=4, dictionary=""): | ||||||
|
||||||
def __init__(self, strategy: str, buffer_size: int, return_bytearray: bool = False, store_comp_size: int = 4, | ||||||
dictionary: Union[str, bytes, memoryview] = ""): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
And up in the file:
Apply to the others too, this makes it easier to not f-up when writing the same union types |
||||||
""" Instantiates and initializes a LZ4 stream decompression context. | ||||||
|
||||||
Args: | ||||||
|
@@ -60,7 +62,7 @@ def __exit__(self, exc_type, exc, exc_tb): | |||||
""" | ||||||
pass | ||||||
|
||||||
def decompress(self, chunk): | ||||||
def decompress(self, chunk: Union[str, bytes, memoryview]) -> Union[bytes, bytearray]: | ||||||
""" Decompress streamed compressed data. | ||||||
|
||||||
Decompress the given ``chunk``, using the given LZ4 stream context, | ||||||
|
@@ -86,7 +88,7 @@ def decompress(self, chunk): | |||||
""" | ||||||
return _decompress(self._context, chunk) | ||||||
|
||||||
def get_block(self, stream): | ||||||
def get_block(self, stream: Union[bytes, bytearray, memoryview]) -> Union[bytes, bytearray]: | ||||||
""" Return the first LZ4 compressed block from ``stream``. | ||||||
|
||||||
Args: | ||||||
|
@@ -115,8 +117,11 @@ class LZ4StreamCompressor: | |||||
""" LZ4 stream compressing context. | ||||||
|
||||||
""" | ||||||
def __init__(self, strategy, buffer_size, mode="default", acceleration=True, compression_level=9, | ||||||
return_bytearray=False, store_comp_size=4, dictionary=""): | ||||||
|
||||||
def __init__(self, strategy: str, buffer_size: int, mode: str = "default", acceleration: int = True, | ||||||
compression_level: int = 9, | ||||||
return_bytearray: bool = False, store_comp_size: int = 4, | ||||||
dictionary: Union[str, bytes, memoryview] = ""): | ||||||
""" Instantiates and initializes a LZ4 stream compression context. | ||||||
|
||||||
Args: | ||||||
|
@@ -181,7 +186,7 @@ def __exit__(self, exc_type, exc, exc_tb): | |||||
""" | ||||||
pass | ||||||
|
||||||
def compress(self, chunk): | ||||||
def compress(self, chunk: Union[str, bytes, memoryview]) -> Union[bytes, bytearray]: | ||||||
""" Stream compress given ``chunk`` of data. | ||||||
|
||||||
Compress the given ``chunk``, using the given LZ4 stream context, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should be a "regular"
Any
, in Python this represents literally ANY object. This means that astr
could be "compatible" with this.A safer approach would be to use
NewType