8000 feat: added stubs for type checkers #223 by Anshuman-37 · Pull Request #295 · python-lz4/python-lz4 · GitHub
[go: up one dir, main page]

Skip to content

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 58 additions & 57 deletions lz4/frame/__init__.py
9E88
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
BLOCKSIZE_MAX4MB as _BLOCKSIZE_MAX4MB,
__doc__ as _doc
)
from typing import Union, IO, Any

__doc__ = _doc

Expand Down 8000 Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
Copy link
Contributor

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 a str could be "compatible" with this.

A safer approach would be to use NewType

CompressionContext = NewType("CompressionContext", Any)

result = compress_begin(
self._context,
block_size=self.block_size,
Expand All @@ -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
Expand Down Expand Up @@ -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.

This comment was marked as resolved.

"""Finish the compression process.

This returns a ``bytes`` or ``bytearray`` object con 8000 taining any data
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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__
Expand All @@ -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
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same applies to the others

Suggested change
_MODE_WRITE: int = 3
_MODE_WRITE: Literal[3] = 3



class LZ4FrameFile(_compression.BaseStream):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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.

Always returns at least one byte of data, unless at EOF. The exact
Expand All @@ -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:
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If whence is only SEEK values, then the right type is: Union[Literal[0], Literal[1], Literal[2]] as per https://github.com/python/cpython/blob/d674792ba773d78d4ed71698b7d47fafb8842d89/Lib/io.py#L65-L67

"""Change the file position.

The new position is specified by ``offset``, relative to the position
Expand Down Expand Up @@ -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:
Expand All @@ -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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly to others, the right type for open would be: Union[Literal["w"], ...] based on https://github.com/python-lz4/python-lz4/pull/295/files#diff-c82af95b2ce244d45d0ba30e51ab0da71df329e418ae1cb3870e024ce4ebb99eR818-R820

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
Expand Down
19 changes: 12 additions & 7 deletions lz4/stream/__init__.py
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.
Expand All @@ -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] = ""):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dictionary: Union[str, bytes, memoryview] = ""):
dictionary: BytesLike = ""):

And up in the file:

BytesLike = Union[str, bytes, memoryview]

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:
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down
0