-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Fix tarfile.open overloads #13441
Fix tarfile.open overloads #13441
Conversation
* Allow `compresslevel` argument for modes `w|gz` and `w|bz2`. * Remove `preset` argument from modes where it's not allowed. Closes: python#13440
Diff from mypy_primer, showing the effect of this PR on open source code: vision (https://github.com/pytorch/vision)
- torchvision/datasets/utils.py:215: note: def open(name: str | bytes | PathLike[str] | PathLike[bytes] | Buffer | None = ..., *, mode: Literal['r|*', 'r|', 'r|gz', 'r|bz2', 'r|xz'], fileobj: IO[bytes] | None = ..., bufsize: int = ..., format: int | None = ..., tarinfo: type[TarInfo] | None = ..., dereference: bool | None = ..., ignore_zeros: bool | None = ..., encoding: str | None = ..., errors: str = ..., pax_headers: Mapping[str, str] | None = ..., debug: int | None = ..., errorlevel: int | None = ..., preset: int | None = ...) -> TarFile
+ torchvision/datasets/utils.py:215: note: def open(name: str | bytes | PathLike[str] | PathLike[bytes] | Buffer | None = ..., *, mode: Literal['r|*', 'r|', 'r|gz', 'r|bz2', 'r|xz'], fileobj: IO[bytes] | None = ..., bufsize: int = ..., format: int | None = ..., tarinfo: type[TarInfo] | None = ..., dereference: bool | None = ..., ignore_zeros: bool | None = ..., encoding: str | None = ..., errors: str = ..., pax_headers: Mapping[str, str] | None = ..., debug: int | None = ..., errorlevel: int | None = ...) -> TarFile
- torchvision/datasets/utils.py:215: note: def open(name: str | bytes | PathLike[str] | PathLike[bytes] | Buffer | None = ..., *, mode: Literal['w|', 'w|gz', 'w|bz2', 'w|xz'], fileobj: IO[bytes] | None = ..., bufsize: int = ..., format: int | None = ..., tarinfo: type[TarInfo] | None = ..., dereference: bool | None = ..., ignore_zeros: bool | None = ..., encoding: str | None = ..., errors: str = ..., pax_headers: Mapping[str, str] | None = ..., debug: int | None = ..., errorlevel: int | None = ..., preset: int | None = ...) -> TarFile
+ torchvision/datasets/utils.py:215: note: def open(name: str | bytes | PathLike[str] | PathLike[bytes] | Buffer | None = ..., *, mode: Literal['w|', 'w|xz'], fileobj: IO[bytes] | None = ..., bufsize: int = ..., format: int | None = ..., tarinfo: type[TarInfo] | None = ..., dereference: bool | None = ..., ignore_zeros: bool | None = ..., encoding: str | None = ..., errors: str = ..., pax_headers: Mapping[str, str] | None = ..., debug: int | None = ..., errorlevel: int | None = ...) -> TarFile
+ torchvision/datasets/utils.py:215: note: def open(name: str | bytes | PathLike[str] | PathLike[bytes] | Buffer | None = ..., *, mode: Literal['w|gz', 'w|bz2'], fileobj: IO[bytes] | None = ..., bufsize: int = ..., format: int | None = ..., tarinfo: type[TarInfo] | None = ..., dereference: bool | None = ..., ignore_zeros: bool | None = ..., encoding: str | None = ..., errors: str = ..., pax_headers: Mapping[str, str] | None = ..., debug: int | None = ..., errorlevel: int | None = ..., compresslevel: int = ...) -> TarFile
|
There is still something to be considered here:
However, typeshed annotates these cases as My proposal is to use I’m happy to move this to a separate issue if necessary but I figured this counts as “fixing tarfile.open overloads“ |
|
I just noticed that I have a second tarfile.open PR open: #13177. Could someone review and merge this so I can rework the other PR, which now has merge conflicts? |
+1 for this PR and the related #13177 |
@@ -260,7 +259,24 @@ def open( | |||
pax_headers: Mapping[str, str] | None = ..., | |||
debug: int | None = ..., | |||
errorlevel: int | None = ..., | |||
preset: int | None = ..., | |||
) -> TarFile: ... |
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 get why modes w|
or w|xz
couldn't pass a compresslevel
kwarg (avoiding an extra overload), it just goes unused doesn't it ?
Or are we being so restrictive with this function as to not allow unused parameters with certain modes? If that's the case, then changes LGTM. Otherwise I'd avoid the extra overload and add compresslevel: int = 9,
to mode: Literal["w|", "w|gz", "w|bz2", "w|xz"]
.
Source: https://github.com/python/cpython/blob/5e73ece95e8aa92d0695acb039ef54e2103ce66b/Lib/tarfile.py#L1889
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.
Yes, it's intended to prevent a misunderstanding of how this works (I.e. a subtle if harmless bug).
compresslevel
argument for modesw|gz
andw|bz2
.preset
argument from modes where it's not allowed.Closes: #13440