Skip to content

Commit 6b8df6b

Browse files
Mark tarfile constants as Final (#14456)
1 parent 3e76afe commit 6b8df6b

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

stdlib/tarfile.pyi

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from builtins import list as _list # aliases to avoid name clashes with fields
66
from collections.abc import Callable, Iterable, Iterator, Mapping
77
from gzip import _ReadableFileobj as _GzipReadableFileobj, _WritableFileobj as _GzipWritableFileobj
88
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
1010
from typing_extensions import Self, TypeAlias, deprecated
1111

1212
if sys.version_info >= (3, 14):
@@ -47,6 +47,7 @@ if sys.version_info >= (3, 13):
4747
_FilterFunction: TypeAlias = Callable[[TarInfo, str], TarInfo | None]
4848
_TarfileFilter: TypeAlias = Literal["fully_trusted", "tar", "data"] | _FilterFunction
4949

50+
@type_check_only
5051
class _Fileobj(Protocol):
5152
def read(self, size: int, /) -> bytes: ...
5253
def write(self, b: bytes, /) -> object: ...
@@ -57,72 +58,75 @@ class _Fileobj(Protocol):
5758
# name: str | bytes
5859
# mode: Literal["rb", "r+b", "wb", "xb"]
5960

61+
@type_check_only
6062
class _Bz2ReadableFileobj(bz2._ReadableFileobj):
6163
def close(self) -> object: ...
6264

65+
@type_check_only
6366
class _Bz2WritableFileobj(bz2._WritableFileobj):
6467
def close(self) -> object: ...
6568

6669
# 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\x0000"
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
99103

100104
# tarfile constants
101105

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]]
108112

109-
ENCODING: str
113+
ENCODING: Final[str]
110114

111-
class ExFileObject(io.BufferedReader):
115+
class ExFileObject(io.BufferedReader): # undocumented
112116
def __init__(self, tarfile: TarFile, tarinfo: TarInfo) -> None: ...
113117

114118
class TarFile:
115119
OPEN_METH: ClassVar[Mapping[str, str]]
116120
name: StrOrBytesPath | None
117121
mode: Literal["r", "a", "w", "x"]
118122
fileobj: _Fileobj | None
119-
format: int | None
123+
format: _TarFormat | None
120124
tarinfo: type[TarInfo]
121125
dereference: bool | None
122126
ignore_zeros: bool | None
123127
encoding: str | None
124128
errors: str
125-
fileobject: type[ExFileObject]
129+
fileobject: type[ExFileObject] # undocumented
126130
pax_headers: Mapping[str, str] | None
127131
debug: int | None
128132
errorlevel: int | None
@@ -751,7 +755,7 @@ class TarInfo:
751755
offset_data: int
752756
sparse: bytes | None
753757
mode: int
754-
type: bytes
758+
type: bytes # usually one of the TYPE constants, but could be an arbitrary byte
755759
linkname: str
756760
uid: int
757761
gid: int
@@ -791,7 +795,7 @@ class TarInfo:
791795
deep: bool = True,
792796
) -> Self: ...
793797
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: ...
795799
def create_ustar_header(
796800
self, info: Mapping[str, str | int | bytes | Mapping[str, str]], encoding: str, errors: str
797801
) -> bytes: ...

0 commit comments

Comments
 (0)