@@ -6,7 +6,7 @@ from builtins import list as _list # aliases to avoid name clashes with fields
6
6
from collections .abc import Callable , Iterable , Iterator , Mapping
7
7
from gzip import _ReadableFileobj as _GzipReadableFileobj , _WritableFileobj as _GzipWritableFileobj
8
8
from types import TracebackType
9
- from typing import IO , ClassVar , Literal , Protocol , overload
9
+ from typing import IO , ClassVar , Final , Literal , Protocol , overload , type_check_only
10
10
from typing_extensions import Self , TypeAlias , deprecated
11
11
12
12
if sys .version_info >= (3 , 14 ):
@@ -47,6 +47,7 @@ if sys.version_info >= (3, 13):
47
47
_FilterFunction : TypeAlias = Callable [[TarInfo , str ], TarInfo | None ]
48
48
_TarfileFilter : TypeAlias = Literal ["fully_trusted" , "tar" , "data" ] | _FilterFunction
49
49
50
+ @type_check_only
50
51
class _Fileobj (Protocol ):
51
52
def read (self , size : int , / ) -> bytes : ...
52
53
def write (self , b : bytes , / ) -> object : ...
@@ -57,72 +58,75 @@ class _Fileobj(Protocol):
57
58
# name: str | bytes
58
59
# mode: Literal["rb", "r+b", "wb", "xb"]
59
60
61
+ @type_check_only
60
62
class _Bz2ReadableFileobj (bz2 ._ReadableFileobj ):
61
63
def close (self ) -> object : ...
62
64
65
+ @type_check_only
63
66
class _Bz2WritableFileobj (bz2 ._WritableFileobj ):
64
67
def close (self ) -> object : ...
65
68
66
69
# tar constants
67
- NUL : bytes
68
- BLOCKSIZE : int
69
- RECORDSIZE : int
70
- GNU_MAGIC : bytes
71
- POSIX_MAGIC : bytes
72
-
73
- LENGTH_NAME : int
74
- LENGTH_LINK : int
75
- LENGTH_PREFIX : int
76
-
77
- REGTYPE : bytes
78
- AREGTYPE : bytes
79
- LNKTYPE : bytes
80
- SYMTYPE : bytes
81
- CONTTYPE : bytes
82
- BLKTYPE : bytes
83
- DIRTYPE : bytes
84
- FIFOTYPE : bytes
85
- CHRTYPE : bytes
86
-
87
- GNUTYPE_LONGNAME : bytes
88
- GNUTYPE_LONGLINK : bytes
89
- GNUTYPE_SPARSE : bytes
90
-
91
- XHDTYPE : bytes
92
- XGLTYPE : bytes
93
- SOLARIS_XHDTYPE : bytes
94
-
95
- USTAR_FORMAT : int
96
- GNU_FORMAT : int
97
- PAX_FORMAT : int
98
- DEFAULT_FORMAT : int
70
+ NUL : Final = b"\0 "
71
+ BLOCKSIZE : Final = 512
72
+ RECORDSIZE : Final = 10240
73
+ GNU_MAGIC : Final = b"ustar \0 "
74
+ POSIX_MAGIC : Final = b"ustar\x00 00"
75
+
76
+ LENGTH_NAME : Final = 100
77
+ LENGTH_LINK : Final = 100
78
+ LENGTH_PREFIX : Final = 155
79
+
80
+ REGTYPE : Final = b"0"
81
+ AREGTYPE : Final = b"\0 "
82
+ LNKTYPE : Final = b"1"
83
+ SYMTYPE : Final = b"2"
84
+ CHRTYPE : Final = b"3"
85
+ BLKTYPE : Final = b"4"
86
+ DIRTYPE : Final = b"5"
87
+ FIFOTYPE : Final = b"6"
88
+ CONTTYPE : Final = b"7"
89
+
90
+ GNUTYPE_LONGNAME : Final = b"L"
91
+ GNUTYPE_LONGLINK : Final = b"K"
92
+ GNUTYPE_SPARSE : Final = b"S"
93
+
94
+ XHDTYPE : Final = b"x"
95
+ XGLTYPE : Final = b"g"
96
+ SOLARIS_XHDTYPE : Final = b"X"
97
+
98
+ _TarFormat : TypeAlias = Literal [0 , 1 , 2 ] # does not exist at runtime
99
+ USTAR_FORMAT : Final = 0
100
+ GNU_FORMAT : Final = 1
101
+ PAX_FORMAT : Final = 2
102
+ DEFAULT_FORMAT : Final = PAX_FORMAT
99
103
100
104
# tarfile constants
101
105
102
- SUPPORTED_TYPES : tuple [bytes , ...]
103
- REGULAR_TYPES : tuple [bytes , ...]
104
- GNU_TYPES : tuple [bytes , ...]
105
- PAX_FIELDS : tuple [str , ...]
106
- PAX_NUMBER_FIELDS : dict [str , type ]
107
- PAX_NAME_FIELDS : set [str ]
106
+ SUPPORTED_TYPES : Final [ tuple [bytes , ...] ]
107
+ REGULAR_TYPES : Final [ tuple [bytes , ...] ]
108
+ GNU_TYPES : Final [ tuple [bytes , ...] ]
109
+ PAX_FIELDS : Final [ tuple [str , ...] ]
110
+ PAX_NUMBER_FIELDS : Final [ dict [str , type ] ]
111
+ PAX_NAME_FIELDS : Final [ set [str ] ]
108
112
109
- ENCODING : str
113
+ ENCODING : Final [ str ]
110
114
111
- class ExFileObject (io .BufferedReader ):
115
+ class ExFileObject (io .BufferedReader ): # undocumented
112
116
def __init__ (self , tarfile : TarFile , tarinfo : TarInfo ) -> None : ...
113
117
114
118
class TarFile :
115
119
OPEN_METH : ClassVar [Mapping [str , str ]]
116
120
name : StrOrBytesPath | None
117
121
mode : Literal ["r" , "a" , "w" , "x" ]
118
122
fileobj : _Fileobj | None
119
- format : int | None
123
+ format : _TarFormat | None
120
124
tarinfo : type [TarInfo ]
121
125
dereference : bool | None
122
126
ignore_zeros : bool | None
123
127
encoding : str | None
124
128
errors : str
125
- fileobject : type [ExFileObject ]
129
+ fileobject : type [ExFileObject ] # undocumented
126
130
pax_headers : Mapping [str , str ] | None
127
131
debug : int | None
128
132
errorlevel : int | None
@@ -751,7 +755,7 @@ class TarInfo:
751
755
offset_data : int
752
756
sparse : bytes | None
753
757
mode : int
754
- type : bytes
758
+ type : bytes # usually one of the TYPE constants, but could be an arbitrary byte
755
759
linkname : str
756
760
uid : int
757
761
gid : int
@@ -791,7 +795,7 @@ class TarInfo:
791
795
deep : bool = True ,
792
796
) -> Self : ...
793
797
def get_info (self ) -> Mapping [str , str | int | bytes | Mapping [str , str ]]: ...
794
- def tobuf (self , format : int | None = 2 , encoding : str | None = "utf-8" , errors : str = "surrogateescape" ) -> bytes : ...
798
+ def tobuf (self , format : _TarFormat | None = 2 , encoding : str | None = "utf-8" , errors : str = "surrogateescape" ) -> bytes : ...
795
799
def create_ustar_header (
796
800
self , info : Mapping [str , str | int | bytes | Mapping [str , str ]], encoding : str , errors : str
797
801
) -> bytes : ...
0 commit comments