8000 Revert "Use `Literal` for `compression` in `zipfile` (#9346)" (#9367) · python/typeshed@2b20b70 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2b20b70

Browse files
authored
Revert "Use Literal for compression in zipfile (#9346)" (#9367)
This reverts commit 034cfab. The commit exposed the value of the compression constants, which seem to be implementation details, in the public API. I don't see anything in the documentation about the values of the constants such as `ZIP_STORED`: https://docs.python.org/3/library/zipfile.html?highlight=zipfile#zipfile.ZipFile Example where this makes a difference: ``` from typing import Literal import zipfile def f1(p: str, compression: int) -> None: """Error: compression should have the type Literal[0, 8, 12, 14]""" zipfile.ZipFile(p, compression=compression) def f2(p: str, compression: Literal[0, 8, 12, 14]) -> None: """Works, but cryptic and exposes internal implementation details""" zipfile.ZipFile(p, compression=compression) ``` The values are of constants need to be explicitly specified if somebody wants to wrap `zipfipe.ZipFile`, which arguably exposes implementation details in a problematic fashion. Here is a real-world example where this caused a regression: https://github.com/pytorch/vision/blob/main/torchvision/datasets/utils.py#L301
1 parent 119cc62 commit 2b20b70

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

stdlib/zipfile.pyi

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ _DateTuple: TypeAlias = tuple[int, int, int, int, int, int]
2929
_ReadWriteMode: TypeAlias = Literal["r", "w"]
3030
_ReadWriteBinaryMode: TypeAlias = Literal["r", "w", "rb", "wb"]
3131
_ZipFileMode: TypeAlias = Literal["r", "w", "x", "a"]
32-
_CompressionMode: TypeAlias = Literal[0, 8, 12, 14]
3332

3433
class BadZipFile(Exception): ...
3534

@@ -101,7 +100,7 @@ class ZipFile:
101100
fp: IO[bytes] | None
102101
NameToInfo: dict[str, ZipInfo]
103102
start_dir: int # undocumented
104-
compression: _CompressionMode # undocumented
103+
compression: int # undocumented
105104
compresslevel: int | None # undocumented
106105
mode: _ZipFileMode # undocumented
107106
pwd: bytes | None # undocumented
@@ -111,7 +110,7 @@ class ZipFile:
111110
self,
112111
file: StrPath | IO[bytes],
113112
mode: Literal["r"] = ...,
114-
compression: _CompressionMode = ...,
113+
compression: int = ...,
115114
allowZip64: bool = ...,
116115
compresslevel: int | None = ...,
117116
*,
@@ -123,7 +122,7 @@ class ZipFile:
123122
self,
124123
file: StrPath | IO[bytes],
125124
mode: _ZipFileMode = ...,
126-
compression: _CompressionMode = ...,
125+
compression: int = ...,
127126
allowZip64: bool = ...,
128127
compresslevel: int | None = ...,
129128
*,
@@ -135,7 +134,7 @@ class ZipFile:
135134
self,
136135
file: StrPath | IO[bytes],
137136
mode: _ZipFileMode = ...,
138-
compression: _CompressionMode = ...,
137+
compression: int = ...,
139138
allowZip64: bool = ...,
140139
compresslevel: int | None = ...,
141140
*,
@@ -146,7 +145,7 @@ class ZipFile:
146145
self,
147146
file: StrPath | IO[bytes],
148147
mode: _ZipFileMode = ...,
149-
compression: _CompressionMode = ...,
148+
compression: int = ...,
150149
allowZip64: bool = ...,
151150
compresslevel: int | None = ...,
152151
) -> None: ...
@@ -185,19 +184,14 @@ class ZipFile:
185184

186185
class PyZipFile(ZipFile):
187186
def __init__(
188-
self,
189-
file: str | IO[bytes],
190-
mode: _ZipFileMode = ...,
191-
compression: _CompressionMode = ...,
192-
allowZip64: bool = ...,
193-
optimize: int = ...,
187+
self, file: str | IO[bytes], mode: _ZipFileMode = ..., compression: int = ..., allowZip64: bool = ..., optimize: int = ...
194188
) -> None: ...
195189
def writepy(self, pathname: str, basename: str = ..., filterfunc: Callable[[str], bool] | None = ...) -> None: ...
196190

197191
class ZipInfo:
198192
filename: str
199193
date_time: _DateTuple
200-
compress_type: _CompressionMode
194+
compress_type: int
201195
comment: bytes
202196
extra: bytes
203197
create_system: int
@@ -275,10 +269,10 @@ if sys.version_info >= (3, 8):
275269

276270
def is_zipfile(filename: StrOrBytesPath | _SupportsReadSeekTell) -> bool: ...
277271

278-
ZIP_STORED: Literal[0]
279-
ZIP_DEFLATED: Literal[8]
272+
ZIP_STORED: int
273+
ZIP_DEFLATED: int
280274
ZIP64_LIMIT: int
281275
ZIP_FILECOUNT_LIMIT: int
282276
ZIP_MAX_COMMENT: int
283-
ZIP_BZIP2: Literal[12]
284-
ZIP_LZMA: Literal[14]
277+
ZIP_BZIP2: int
278+
ZIP_LZMA: int

0 commit comments

Comments
 (0)
0