Thanks to visit codestin.com
Credit goes to github.com

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

Conversation

Anshuman-37
Copy link

@Anshuman-37 Anshuman-37 commented Jan 17, 2025

Hi, just added types. Please point out mistakes if any. Closes #223

@jonathanunderwood
Copy link
Member

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.

@Anshuman-37
Copy link
Author

Do you mean about the types for the parameters in the functions for example
def get_block(self, stream: Union[str, bytes, memoryview]) -> Union[bytes, bytearray]:

@jmg-duarte
Copy link
Contributor

jmg-duarte commented Jan 23, 2025

Copy link
Contributor

@jmg-duarte jmg-duarte left a 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!

@@ -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)

# 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

@@ -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

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"], ...]

@@ -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.

@jmg-duarte
Copy link
Contributor

Ah, don't forget to add mypy to the CI!

Copy link
Contributor

@jmg-duarte jmg-duarte left a 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

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

@Anshuman-37
Copy link
Author

Okay I have updated the code please have a look @jmg-duarte and @jonathanunderwood

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add stubs for type checkers (mypy, pytype, etc)
3 participants