feat: added stubs for type checkers #223#295
feat: added stubs for type checkers #223#295Anshuman-37 wants to merge 4 commits intopython-lz4:masterfrom
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 |
jmg-duarte
left a comment
There was a problem hiding this comment.
Good job! As Jonathan said, only the block left to go!
lz4/frame/__init__.py
Outdated
|
|
||
| if self._started is False: | ||
| self._context = create_compression_context() | ||
| self._context: Any = create_compression_context() |
There was a problem hiding this comment.
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
| _MODE_READ: int = 1 | ||
| # Value 2 no longer used | ||
| _MODE_WRITE = 3 | ||
| _MODE_WRITE: int = 3 |
There was a problem hiding this comment.
Same applies to the others
| _MODE_WRITE: int = 3 | |
| _MODE_WRITE: Literal[3] = 3 |
lz4/frame/__init__.py
Outdated
| 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.
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.
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"], ...]| 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 |
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.
| 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 |
|
Conflicts to resolve. |
Hi, just added types. Please point out mistakes if any. Closes #223