-
Notifications
You must be signed in to change notification settings - Fork 73
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?
Conversation
Thanks for this. At present this only annotates the function/method return types, and doesn't cover the block bindings, so isn't really complete. |
Do you mean about the types for the parameters in the functions for example |
@Anshuman-37 you forgot to cover this folder https://github.com/python-lz4/python-lz4/tree/master/lz4/block |
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.
Good job! As Jonathan said, only the block
left to go!
lz4/frame/__init__.py
Outdated
@@ -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() |
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 a str
could be "compatible" with this.
A safer approach would be to use NewType
CompressionContext = NewType("CompressionContext", Any)
lz4/frame/__init__.py
Outdated
# 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Same applies to the others
_MODE_WRITE: int = 3 | |
_MODE_WRITE: Literal[3] = 3 |
lz4/frame/__init__.py
Outdated
@@ -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 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
lz4/frame/__init__.py
Outdated
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 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"], ...]
@@ -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.
Sorry, something went wrong.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
Ah, don't forget to add |
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.
lgtm, just a small improvement
lz4/stream/__init__.py
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
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
Okay I have updated the code please have a look @jmg-duarte and @jonathanunderwood |
Hi, just added types. Please point out mistakes if any. Closes #223