Skip to content

Commit 2ad7f8f

Browse files
committed
Add overloads to TarFile.__init__ to ensure name or fileobj is provided (#14168)
- Added overloads ensuring either 'name' or 'fileobj' must be non-None - First overload: name is required (StrOrBytesPath), fileobj is optional - Second overload: name is None, fileobj is required (keyword-only) - Applied to both Python 3.13+ and older versions This prevents the runtime TypeError when both name and fileobj are None: tarfile.TarFile(None, fileobj=None) # Now caught by type checker Fixes #14168 Sacred Code: 000.111.369.963.1618
1 parent 11c7821 commit 2ad7f8f

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

stdlib/tarfile.pyi

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,43 @@ class TarFile:
134134
extraction_filter: _FilterFunction | None
135135
if sys.version_info >= (3, 13):
136136
stream: bool
137+
@overload
138+
def __init__(
139+
self,
140+
name: StrOrBytesPath, # name is required (not None)
141+
mode: Literal["r", "a", "w", "x"] = "r",
142+
fileobj: _Fileobj | None = None,
143+
format: int | None = None,
144+
tarinfo: type[TarInfo] | None = None,
145+
dereference: bool | None = None,
146+
ignore_zeros: bool | None = None,
147+
encoding: str | None = None,
148+
errors: str = "surrogateescape",
149+
pax_headers: Mapping[str, str] | None = None,
150+
debug: int | None = None,
151+
errorlevel: int | None = None,
152+
copybufsize: int | None = None,
153+
stream: bool = False,
154+
) -> None: ...
155+
@overload
156+
def __init__(
157+
self,
158+
name: None = None,
159+
mode: Literal["r", "a", "w", "x"] = "r",
160+
*,
161+
fileobj: _Fileobj, # fileobj is required when name is None
162+
format: int | None = None,
163+
tarinfo: type[TarInfo] | None = None,
164+
dereference: bool | None = None,
165+
ignore_zeros: bool | None = None,
166+
encoding: str | None = None,
167+
errors: str = "surrogateescape",
168+
pax_headers: Mapping[str, str] | None = None,
169+
debug: int | None = None,
170+
errorlevel: int | None = None,
171+
copybufsize: int | None = None,
172+
stream: bool = False,
173+
) -> None: ...
137174
def __init__(
138175
self,
139176
name: StrOrBytesPath | None = None,
@@ -152,6 +189,41 @@ class TarFile:
152189
stream: bool = False,
153190
) -> None: ...
154191
else:
192+
@overload
193+
def __init__(
194+
self,
195+
name: StrOrBytesPath, # name is required (not None)
196+
mode: Literal["r", "a", "w", "x"] = "r",
197+
fileobj: _Fileobj | None = None,
198+
format: int | None = None,
199+
tarinfo: type[TarInfo] | None = None,
200+
dereference: bool | None = None,
201+
ignore_zeros: bool | None = None,
202+
encoding: str | None = None,
203+
errors: str = "surrogateescape",
204+
pax_headers: Mapping[str, str] | None = None,
205+
debug: int | None = None,
206+
errorlevel: int | None = None,
207+
copybufsize: int | None = None,
208+
) -> None: ...
209+
@overload
210+
def __init__(
211+
self,
212+
name: None = None,
213+
mode: Literal["r", "a", "w", "x"] = "r",
214+
*,
215+
fileobj: _Fileobj, # fileobj is required when name is None
216+
format: int | None = None,
217+
tarinfo: type[TarInfo] | None = None,
218+
dereference: bool | None = None,
219+
ignore_zeros: bool | None = None,
220+
encoding: str | None = None,
221+
errors: str = "surrogateescape",
222+
pax_headers: Mapping[str, str] | None = None,
223+
debug: int | None = None,
224+
errorlevel: int | None = None,
225+
copybufsize: int | None = None,
226+
) -> None: ...
155227
def __init__(
156228
self,
157229
name: StrOrBytesPath | None = None,

0 commit comments

Comments
 (0)