-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Python 3.14: PEP-784 compression.zstd #14129
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
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@emmatyping: if you want to give try this out, or simply comment on the use of |
I think we should type that it accepts |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Now that CPython 3.14.0 beta 2 is released, the CI is green and the PR ready for review! |
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.
Thanks, a few remark below. Also I notice that many defaults are ...
, although they have basic defaults in the implementation. In these cases, we include the actual defaults, ...
is only used for complicated cases.
stdlib/_zstd.pyi
Outdated
CONTINUE: Final[_ZstdCompressorContinue] = 0 | ||
FLUSH_BLOCK: Final[_ZstdCompressorFlushBlock] = 1 | ||
FLUSH_FRAME: Final[_ZstdCompressorFlushFrame] = 2 |
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.
The Final
implies the Literal
:
CONTINUE: Final[_ZstdCompressorContinue] = 0 | |
FLUSH_BLOCK: Final[_ZstdCompressorFlushBlock] = 1 | |
FLUSH_FRAME: Final[_ZstdCompressorFlushFrame] = 2 | |
CONTINUE: Final = 0 | |
FLUSH_BLOCK: Final = 1 | |
FLUSH_FRAME: Final = 2 |
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.
That was to address a suggestion of @emmatyping of making sure the value is only defined once.
Do you want me to revert all occurrences of _ZstdCompressorContinue
?
e.g. replace _ZstdCompressorContinue | _ZstdCompressorFlushBlock | _ZstdCompressorFlushFrame
down below with Literal[0, 1, 2]
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.
The rest is fine as is. This was just to point out the redundancy here.
stdlib/_zstd.pyi
Outdated
def get_frame_info(frame_buffer: ReadableBuffer) -> tuple[int, int]: ... | ||
def get_frame_size(frame_buffer: ReadableBuffer) -> int: ... | ||
def get_param_bounds(parameter: int, is_compress: bool) -> tuple[int, int]: ... | ||
def set_parameter_types(c_parameter_type: type[Any], d_parameter_type: type[Any]) -> None: ... |
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.
Why use Any
here? I assume that only certain types are allowed? In that case, we need a comment explaining that.
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.
Yeah these should be specifically type[CompressionParameter]
and type[DecompressionParameter]
.
def set_parameter_types(c_parameter_type: type[Any], d_parameter_type: type[Any]) -> None: ... | |
def set_parameter_types(c_parameter_type: type[CompressionParameter], d_parameter_type: type[DecompressionParameter]) -> None: ... |
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 my understanding is correct, this functions take any types, and will set valid types to be used as keys (on top of int
) for the options
parameters.
In the implementation of zstd
a single call is done explicitly with CompressionParameter
and DecompressionParameter
, but it could have been something else.
In any case, it is in a private _zstd
module, so I don't think it matters a lot.
Happy to change this with Emma's suggestion.
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.
While they can take any type, I would argue it is wrong to do so. I guess you could specify type[IntEnum]
perhaps if you didn't want to specify the specific types?
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 think you are right, and typing with type[CompressionParameter]
/type[DecompressionParameter]
better shows the intent.
I will go with that.
_PathOrFileBinary: TypeAlias = StrOrBytesPath | IO[bytes] | ||
_PathOrFileText: TypeAlias = StrOrBytesPath | IO[str] |
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.
We try to avoid the unwieldy pseudo-protocol IO
and its sub-classes, especially in argument annotations. In this case, appropriate protocols seem to be fairly to construct.
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 took this from the lzma.pyi
file so I thought it was ok. Tell me what you think.
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.
The lzma stubs probably predate the introduction of protocols and need to be updated as well at some point.
encoding: str | None = ..., | ||
errors: str | None = ..., | ||
newline: str | None = ..., | ||
) -> TextIO: ... |
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 here. The implementation seems to return TextIOWrapper
, so we should just return this concrete type.
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 took this from the lzma.pyi
file so I thought it was ok.
This one is straightforward to change though. Do you want me to change it in lzma.pyi
as well?
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.
Changing it in lzma as well would be appreciated.
encoding: str | None = ..., | ||
errors: str | None = ..., | ||
newline: str | None = ..., | ||
) -> TextIO: ... |
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 here.
I thought that was implementation details with no use for type checking, but I will change. Likewise, is it needed to define the values for the constants? For example: # currently
ZSTD_CLEVEL_DEFAULT: Final[int]
# possible change
ZSTD_CLEVEL_DEFAULT: Final = 3 |
Generally we do that nowadays. |
This comment has been minimized.
This comment has been minimized.
I think I have addressed all your points, feel free to review again, especially the protocols. I am not sure what the issue with |
Definitely looks unrelated. I searched through the codebase and there aren't any references to |
The primer errors are a mypy hiccup. I've reopened the PR to trigger another run. |
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, let's see what primer says, but I don't expect any relevant output.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Second PR about PEP-784 in stdlib (first PR):
_zstd
compression.zstd