Skip to content

Commit 5f1fd23

Browse files
authored
Rework tarfile.open/TarFile.open (#13177)
* Copy overloads of `open()` to `TarFile.open()`. * Replace remaining instances of `IO` with `_Fileobj`. * Replace `open()` with alias to `TarFile.open()` to match implementation.
1 parent 6cecf19 commit 5f1fd23

File tree

1 file changed

+181
-178
lines changed

1 file changed

+181
-178
lines changed

stdlib/tarfile.pyi

+181-178
Original file line numberDiff line numberDiff line change
@@ -103,182 +103,6 @@ PAX_NAME_FIELDS: set[str]
103103

104104
ENCODING: str
105105

106-
@overload
107-
def open(
108-
name: StrOrBytesPath | None = None,
109-
mode: Literal["r", "r:*", "r:", "r:gz", "r:bz2", "r:xz"] = "r",
110-
fileobj: IO[bytes] | None = None,
111-
bufsize: int = 10240,
112-
*,
113-
format: int | None = ...,
114-
tarinfo: type[TarInfo] | None = ...,
115-
dereference: bool | None = ...,
116-
ignore_zeros: bool | None = ...,
117-
encoding: str | None = ...,
118-
errors: str = ...,
119-
pax_headers: Mapping[str, str] | None = ...,
120-
debug: int | None = ...,
121-
errorlevel: int | None = ...,
122-
) -> TarFile: ...
123-
@overload
124-
def open(
125-
name: StrOrBytesPath | None,
126-
mode: Literal["x", "x:", "a", "a:", "w", "w:", "w:tar"],
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:", "w:tar"],
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 = ...,
209-
preset: Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | None = ...,
210-
) -> TarFile: ...
211-
@overload
212-
def open(
213-
name: StrOrBytesPath | None = None,
214-
*,
215-
mode: Literal["x:xz", "w:xz"],
216-
fileobj: _Fileobj | None = None,
217-
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-
@overload
230-
def open(
231-
name: StrOrBytesPath | ReadableBuffer | None = None,
232-
*,
233-
mode: Literal["r|*", "r|", "r|gz", "r|bz2", "r|xz"],
234-
fileobj: IO[bytes] | None = None,
235-
bufsize: int = 10240,
236-
format: int | None = ...,
237-
tarinfo: type[TarInfo] | None = ...,
238-
dereference: bool | None = ...,
239-
ignore_zeros: bool | None = ...,
240-
encoding: str | None = ...,
241-
errors: str = ...,
242-
pax_headers: Mapping[str, str] | None = ...,
243-
debug: int | None = ...,
244-
errorlevel: int | None = ...,
245-
) -> TarFile: ...
246-
@overload
247-
def open(
248-
name: StrOrBytesPath | WriteableBuffer | None = None,
249-
*,
250-
mode: Literal["w|", "w|xz"],
251-
fileobj: IO[bytes] | None = None,
252-
bufsize: int = 10240,
253-
format: int | None = ...,
254-
tarinfo: type[TarInfo] | None = ...,
255-
dereference: bool | None = ...,
256-
ignore_zeros: bool | None = ...,
257-
encoding: str | None = ...,
258-
errors: str = ...,
259-
pax_headers: Mapping[str, str] | None = ...,
260-
debug: int | None = ...,
261-
errorlevel: int | None = ...,
262-
) -> TarFile: ...
263-
@overload
264-
def open(
265-
name: StrOrBytesPath | WriteableBuffer | None = None,
266-
*,
267-
mode: Literal["w|gz", "w|bz2"],
268-
fileobj: IO[bytes] | None = None,
269-
bufsize: int = 10240,
270-
format: int | None = ...,
271-
tarinfo: type[TarInfo] | None = ...,
272-
dereference: bool | None = ...,
273-
ignore_zeros: bool | None = ...,
274-
encoding: str | None = ...,
275-
errors: str = ...,
276-
pax_headers: Mapping[str, str] | None = ...,
277-
debug: int | None = ...,
278-
errorlevel: int | None = ...,
279-
compresslevel: int = 9,
280-
) -> TarFile: ...
281-
282106
class ExFileObject(io.BufferedReader):
283107
def __init__(self, tarfile: TarFile, tarinfo: TarInfo) -> None: ...
284108

@@ -341,14 +165,152 @@ class TarFile:
341165
self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
342166
) -> None: ...
343167
def __iter__(self) -> Iterator[TarInfo]: ...
168+
@overload
169+
@classmethod
170+
def open(
171+
cls,
172+
name: StrOrBytesPath | None = None,
173+
mode: Literal["r", "r:*", "r:", "r:gz", "r:bz2", "r:xz"] = "r",
174+
fileobj: _Fileobj | None = None,
175+
bufsize: int = 10240,
176+
*,
177+
format: int | None = ...,
178+
tarinfo: type[TarInfo] | None = ...,
179+
dereference: bool | None = ...,
180+
ignore_zeros: bool | None = ...,
181+
encoding: str | None = ...,
182+
errors: str = ...,
183+
pax_headers: Mapping[str, str] | None = ...,
184+
debug: int | None = ...,
185+
errorlevel: int | None = ...,
186+
) -> Self: ...
187+
@overload
188+
@classmethod
189+
def open(
190+
cls,
191+
name: StrOrBytesPath | None,
192+
mode: Literal["x", "x:", "a", "a:", "w", "w:", "w:tar"],
193+
fileobj: _Fileobj | None = None,
194+
bufsize: int = 10240,
195+
*,
196+
format: int | None = ...,
197+
tarinfo: type[TarInfo] | None = ...,
198+
dereference: bool | None = ...,
199+
ignore_zeros: bool | None = ...,
200+
encoding: str | None = ...,
201+
errors: str = ...,
202+
pax_headers: Mapping[str, str] | None = ...,
203+
debug: int | None = ...,
204+
errorlevel: int | None = ...,
205+
) -> Self: ...
206+
@overload
207+
@classmethod
208+
def open(
209+
cls,
210+
name: StrOrBytesPath | None = None,
211+
*,
212+
mode: Literal["x", "x:", "a", "a:", "w", "w:", "w:tar"],
213+
fileobj: _Fileobj | None = None,
214+
bufsize: int = 10240,
215+
format: int | None = ...,
216+
tarinfo: type[TarInfo] | None = ...,
217+
dereference: bool | None = ...,
218+
ignore_zeros: bool | None = ...,
219+
encoding: str | None = ...,
220+
errors: str = ...,
221+
pax_headers: Mapping[str, str] | None = ...,
222+
debug: int | None = ...,
223+
errorlevel: int | None = ...,
224+
) -> Self: ...
225+
@overload
226+
@classmethod
227+
def open(
228+
cls,
229+
name: StrOrBytesPath | None,
230+
mode: Literal["x:gz", "x:bz2", "w:gz", "w:bz2"],
231+
fileobj: _Fileobj | None = None,
232+
bufsize: int = 10240,
233+
*,
234+
format: int | None = ...,
235+
tarinfo: type[TarInfo] | None = ...,
236+
dereference: bool | None = ...,
237+
ignore_zeros: bool | None = ...,
238+
encoding: str | None = ...,
239+
errors: str = ...,
240+
pax_headers: Mapping[str, str] | None = ...,
241+
debug: int | None = ...,
242+
errorlevel: int | None = ...,
243+
compresslevel: int = 9,
244+
) -> Self: ...
245+
@overload
246+
@classmethod
247+
def open(
248+
cls,
249+
name: StrOrBytesPath | None = None,
250+
*,
251+
mode: Literal["x:gz", "x:bz2", "w:gz", "w:bz2"],
252+
fileobj: _Fileobj | None = None,
253+
bufsize: int = 10240,
254+
format: int | None = ...,
255+
tarinfo: type[TarInfo] | None = ...,
256+
dereference: bool | None = ...,
257+
ignore_zeros: bool | None = ...,
258+
encoding: str | None = ...,
259+
errors: str = ...,
260+
pax_headers: Mapping[str, str] | None = ...,
261+
debug: int | None = ...,
262+
errorlevel: int | None = ...,
263+
compresslevel: int = 9,
264+
) -> Self: ...
265+
@overload
266+
@classmethod
267+
def open(
268+
cls,
269+
name: StrOrBytesPath | None,
270+
mode: Literal["x:xz", "w:xz"],
271+
fileobj: _Fileobj | None = None,
272+
bufsize: int = 10240,
273+
*,
274+
format: int | None = ...,
275+
tarinfo: type[TarInfo] | None = ...,
276+
dereference: bool | None = ...,
277+
ignore_zeros: bool | None = ...,
278+
encoding: str | None = ...,
279+
errors: str = ...,
280+
pax_headers: Mapping[str, str] | None = ...,
281+
debug: int | None = ...,
282+
errorlevel: int | None = ...,
283+
preset: Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | None = ...,
284+
) -> Self: ...
285+
@overload
344286
@classmethod
345287
def open(
346288
cls,
347289
name: StrOrBytesPath | None = None,
348-
mode: str = "r",
349-
fileobj: IO[bytes] | None = None, # depends on mode
290+
*,
291+
mode: Literal["x:xz", "w:xz"],
292+
fileobj: _Fileobj | None = None,
350293
bufsize: int = 10240,
294+
format: int | None = ...,
295+
tarinfo: type[TarInfo] | None = ...,
296+
dereference: bool | None = ...,
297+
ignore_zeros: bool | None = ...,
298+
encoding: str | None = ...,
299+
errors: str = ...,
300+
pax_headers: Mapping[str, str] | None = ...,
301+
debug: int | None = ...,
302+
errorlevel: int | None = ...,
303+
preset: Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | None = ...,
304+
) -> Self: ...
305+
@overload
306+
@classmethod
307+
def open(
308+
cls,
309+
name: StrOrBytesPath | ReadableBuffer | None = None,
351310
*,
311+
mode: Literal["r|*", "r|", "r|gz", "r|bz2", "r|xz"],
312+
fileobj: _Fileobj | None = None,
313+
bufsize: int = 10240,
352314
format: int | None = ...,
353315
tarinfo: type[TarInfo] | None = ...,
354316
dereference: bool | None = ...,
@@ -359,6 +321,45 @@ class TarFile:
359321
debug: int | None = ...,
360322
errorlevel: int | None = ...,
361323
) -> Self: ...
324+
@overload
325+
@classmethod
326+
def open(
327+
cls,
328+
name: StrOrBytesPath | WriteableBuffer | None = None,
329+
*,
330+
mode: Literal["w|", "w|xz"],
331+
fileobj: _Fileobj | None = None,
332+
bufsize: int = 10240,
333+
format: int | None = ...,
334+
tarinfo: type[TarInfo] | None = ...,
335+
dereference: bool | None = ...,
336+
ignore_zeros: bool | None = ...,
337+
encoding: str | None = ...,
338+
errors: str = ...,
339+
pax_headers: Mapping[str, str] | None = ...,
340+
debug: int | None = ...,
341+
errorlevel: int | None = ...,
342+
) -> Self: ...
343+
@overload
344+
@classmethod
345+
def open(
346+
cls,
347+
name: StrOrBytesPath | WriteableBuffer | None = None,
348+
*,
349+
mode: Literal["w|gz", "w|bz2"],
350+
fileobj: _Fileobj | None = None,
351+
bufsize: int = 10240,
352+
format: int | None = ...,
353+
tarinfo: type[TarInfo] | None = ...,
354+
dereference: bool | None = ...,
355+
ignore_zeros: bool | None = ...,
356+
encoding: str | None = ...,
357+
errors: str = ...,
358+
pax_headers: Mapping[str, str] | None = ...,
359+
debug: int | None = ...,
360+
errorlevel: int | None = ...,
361+
compresslevel: int = 9,
362+
) -> Self: ...
362363
@classmethod
363364
def taropen(
364365
cls,
@@ -517,6 +518,8 @@ class TarFile:
517518
) -> TarInfo: ...
518519
def close(self) -> None: ...
519520

521+
open = TarFile.open
522+
520523
if sys.version_info >= (3, 9):
521524
def is_tarfile(name: StrOrBytesPath | IO[bytes]) -> bool: ...
522525

0 commit comments

Comments
 (0)