Skip to content

Commit 9757602

Browse files
authored
tarfile.open(): Handle all modes (#12181)
1 parent 8228faa commit 9757602

File tree

1 file changed

+111
-6
lines changed

1 file changed

+111
-6
lines changed

stdlib/tarfile.pyi

Lines changed: 111 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,10 @@ PAX_NAME_FIELDS: set[str]
103103

104104
ENCODING: str
105105

106-
_FileCreationModes: TypeAlias = Literal["a", "w", "x"]
107-
108106
@overload
109107
def open(
110108
name: StrOrBytesPath | None = None,
111-
mode: str = "r",
109+
mode: Literal["r", "r:*", "r:", "r:gz", "r:bz2", "r:xz"] = "r",
112110
fileobj: IO[bytes] | None = None,
113111
bufsize: int = 10240,
114112
*,
@@ -121,16 +119,124 @@ def open(
121119
pax_headers: Mapping[str, str] | None = ...,
122120
debug: int | None = ...,
123121
errorlevel: int | None = ...,
124-
compresslevel: int | None = ...,
122+
) -> TarFile: ...
123+
@overload
124+
def open(
125+
name: StrOrBytesPath | None,
126+
mode: Literal["x", "x:", "a", "a:", "w", "w:"],
127+
fileobj: _Fileobj | None = None,
128+
bufsize: int = 10240,
129+
*,
130+
format: int | None = ...,
131+
tarinfo: type[TarInfo] | None = ...,
132+
dereference: bool | None = ...,
133+
ignore_zeros: bool | None = ...,
134+
encoding: str | None = ...,
135+
errors: str = ...,
136+
pax_headers: Mapping[str, str] | None = ...,
137+
debug: int | None = ...,
138+
errorlevel: int | None = ...,
139+
) -> TarFile: ...
140+
@overload
141+
def open(
142+
name: StrOrBytesPath | None = None,
143+
*,
144+
mode: Literal["x", "x:", "a", "a:", "w", "w:"],
145+
fileobj: _Fileobj | None = None,
146+
bufsize: int = 10240,
147+
format: int | None = ...,
148+
tarinfo: type[TarInfo] | None = ...,
149+
dereference: bool | None = ...,
150+
ignore_zeros: bool | None = ...,
151+
encoding: str | None = ...,
152+
errors: str = ...,
153+
pax_headers: Mapping[str, str] | None = ...,
154+
debug: int | None = ...,
155+
errorlevel: int | None = ...,
156+
) -> TarFile: ...
157+
@overload
158+
def open(
159+
name: StrOrBytesPath | None,
160+
mode: Literal["x:gz", "x:bz2", "w:gz", "w:bz2"],
161+
fileobj: _Fileobj | None = None,
162+
bufsize: int = 10240,
163+
*,
164+
format: int | None = ...,
165+
tarinfo: type[TarInfo] | None = ...,
166+
dereference: bool | None = ...,
167+
ignore_zeros: bool | None = ...,
168+
encoding: str | None = ...,
169+
errors: str = ...,
170+
pax_headers: Mapping[str, str] | None = ...,
171+
debug: int | None = ...,
172+
errorlevel: int | None = ...,
173+
compresslevel: int = 9,
174+
) -> TarFile: ...
175+
@overload
176+
def open(
177+
name: StrOrBytesPath | None = None,
178+
*,
179+
mode: Literal["x:gz", "x:bz2", "w:gz", "w:bz2"],
180+
fileobj: _Fileobj | None = None,
181+
bufsize: int = 10240,
182+
format: int | None = ...,
183+
tarinfo: type[TarInfo] | None = ...,
184+
dereference: bool | None = ...,
185+
ignore_zeros: bool | None = ...,
186+
encoding: str | None = ...,
187+
errors: str = ...,
188+
pax_headers: Mapping[str, str] | None = ...,
189+
debug: int | None = ...,
190+
errorlevel: int | None = ...,
191+
compresslevel: int = 9,
192+
) -> TarFile: ...
193+
@overload
194+
def open(
195+
name: StrOrBytesPath | None,
196+
mode: Literal["x:xz", "w:xz"],
197+
fileobj: _Fileobj | None = None,
198+
bufsize: int = 10240,
199+
*,
200+
format: int | None = ...,
201+
tarinfo: type[TarInfo] | None = ...,
202+
dereference: bool | None = ...,
203+
ignore_zeros: bool | None = ...,
204+
encoding: str | None = ...,
205+
errors: str = ...,
206+
pax_headers: Mapping[str, str] | None = ...,
207+
debug: int | None = ...,
208+
errorlevel: int | None = ...,
125209
preset: Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | None = ...,
126210
) -> TarFile: ...
127211
@overload
128212
def open(
129213
name: StrOrBytesPath | None = None,
130-
mode: _FileCreationModes = ...,
214+
*,
215+
mode: Literal["x:xz", "w:xz"],
131216
fileobj: _Fileobj | None = None,
132217
bufsize: int = 10240,
218+
format: int | None = ...,
219+
tarinfo: type[TarInfo] | None = ...,
220+
dereference: bool | None = ...,
221+
ignore_zeros: bool | None = ...,
222+
encoding: str | None = ...,
223+
errors: str = ...,
224+
pax_headers: Mapping[str, str] | None = ...,
225+
debug: int | None = ...,
226+
errorlevel: int | None = ...,
227+
preset: Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | None = ...,
228+
) -> TarFile: ...
229+
230+
# TODO: Temporary fallback for modes containing pipe characters. These don't
231+
# work with mypy 1.10, but this should be fixed with mypy 1.11.
232+
# https://github.com/python/typeshed/issues/12182
233+
@overload
234+
def open(
235+
name: StrOrBytesPath | None = None,
133236
*,
237+
mode: str,
238+
fileobj: IO[bytes] | None = None,
239+
bufsize: int = 10240,
134240
format: int | None = ...,
135241
tarinfo: type[TarInfo] | None = ...,
136242
dereference: bool | None = ...,
@@ -140,7 +246,6 @@ def open(
140246
pax_headers: Mapping[str, str] | None = ...,
141247
debug: int | None = ...,
142248
errorlevel: int | None = ...,
143-
compresslevel: int | None = ...,
144249
preset: int | None = ...,
145250
) -> TarFile: ...
146251

0 commit comments

Comments
 (0)