Bläddra i källkod

Add type annotations to PackIndex classes and read_pack_header function

Jelmer Vernooij 5 månader sedan
förälder
incheckning
0db5ad0406
1 ändrade filer med 16 tillägg och 27 borttagningar
  1. 16 27
      dulwich/pack.py

+ 16 - 27
dulwich/pack.py

@@ -924,7 +924,7 @@ class PackIndex1(FilePackIndex):
     """Version 1 Pack Index file."""
 
     def __init__(
-        self, filename: Union[str, os.PathLike], file=None, contents=None, size=None
+        self, filename: Union[str, os.PathLike], file: Optional[BinaryIO] = None, contents: Optional[bytes] = None, size: Optional[int] = None
     ) -> None:
         """Initialize a version 1 pack index.
 
@@ -938,23 +938,19 @@ class PackIndex1(FilePackIndex):
         self.version = 1
         self._fan_out_table = self._read_fan_out_table(0)
 
-    def _unpack_entry(self, i):
-        """Unpack the i-th entry from the v1 index."""
+    def _unpack_entry(self, i: int) -> tuple[bytes, int, None]:
         (offset, name) = unpack_from(">L20s", self._contents, (0x100 * 4) + (i * 24))
         return (name, offset, None)
 
-    def _unpack_name(self, i):
-        """Unpack the i-th SHA1 from the v1 index."""
+    def _unpack_name(self, i: int) -> bytes:
         offset = (0x100 * 4) + (i * 24) + 4
         return self._contents[offset : offset + 20]
 
-    def _unpack_offset(self, i):
-        """Unpack the i-th offset from the v1 index."""
+    def _unpack_offset(self, i: int) -> int:
         offset = (0x100 * 4) + (i * 24)
         return unpack_from(">L", self._contents, offset)[0]
 
-    def _unpack_crc32_checksum(self, i) -> None:
-        """Return None as v1 indexes don't store CRC32 checksums."""
+    def _unpack_crc32_checksum(self, i: int) -> None:
         # Not stored in v1 index files
         return None
 
@@ -963,7 +959,7 @@ class PackIndex2(FilePackIndex):
     """Version 2 Pack Index file."""
 
     def __init__(
-        self, filename: Union[str, os.PathLike], file=None, contents=None, size=None
+        self, filename: Union[str, os.PathLike], file: Optional[BinaryIO] = None, contents: Optional[bytes] = None, size: Optional[int] = None
     ) -> None:
         """Initialize a version 2 pack index.
 
@@ -987,24 +983,18 @@ class PackIndex2(FilePackIndex):
             self
         )
 
-    def _unpack_entry(self, i):
-        """Unpack the i-th entry from the v2 index."""
+    def _unpack_entry(self, i: int) -> tuple[bytes, int, int]:
         return (
             self._unpack_name(i),
             self._unpack_offset(i),
             self._unpack_crc32_checksum(i),
         )
 
-    def _unpack_name(self, i):
-        """Unpack the i-th SHA1 from the v2 index."""
+    def _unpack_name(self, i: int) -> bytes:
         offset = self._name_table_offset + i * 20
         return self._contents[offset : offset + 20]
 
-    def _unpack_offset(self, i):
-        """Unpack the i-th offset from the v2 index.
-
-        Handles large offsets (>2GB) by reading from the large offset table.
-        """
+    def _unpack_offset(self, i: int) -> int:
         offset = self._pack_offset_table_offset + i * 4
         offset = unpack_from(">L", self._contents, offset)[0]
         if offset & (2**31):
@@ -1012,8 +1002,7 @@ class PackIndex2(FilePackIndex):
             offset = unpack_from(">Q", self._contents, offset)[0]
         return offset
 
-    def _unpack_crc32_checksum(self, i):
-        """Unpack the i-th CRC32 checksum from the v2 index."""
+    def _unpack_crc32_checksum(self, i: int) -> int:
         return unpack_from(">L", self._contents, self._crc32_table_offset + i * 4)[0]
 
 
@@ -1024,7 +1013,7 @@ class PackIndex3(FilePackIndex):
     """
 
     def __init__(
-        self, filename: Union[str, os.PathLike], file=None, contents=None, size=None
+        self, filename: Union[str, os.PathLike], file: Optional[BinaryIO] = None, contents: Optional[bytes] = None, size: Optional[int] = None
     ) -> None:
         """Initialize a version 3 pack index.
 
@@ -1064,18 +1053,18 @@ class PackIndex3(FilePackIndex):
             self
         )
 
-    def _unpack_entry(self, i):
+    def _unpack_entry(self, i: int) -> tuple[bytes, int, int]:
         return (
             self._unpack_name(i),
             self._unpack_offset(i),
             self._unpack_crc32_checksum(i),
         )
 
-    def _unpack_name(self, i):
+    def _unpack_name(self, i: int) -> bytes:
         offset = self._name_table_offset + i * self.hash_size
         return self._contents[offset : offset + self.hash_size]
 
-    def _unpack_offset(self, i):
+    def _unpack_offset(self, i: int) -> int:
         offset = self._pack_offset_table_offset + i * 4
         offset = unpack_from(">L", self._contents, offset)[0]
         if offset & (2**31):
@@ -1083,11 +1072,11 @@ class PackIndex3(FilePackIndex):
             offset = unpack_from(">Q", self._contents, offset)[0]
         return offset
 
-    def _unpack_crc32_checksum(self, i):
+    def _unpack_crc32_checksum(self, i: int) -> int:
         return unpack_from(">L", self._contents, self._crc32_table_offset + i * 4)[0]
 
 
-def read_pack_header(read) -> tuple[int, int]:
+def read_pack_header(read: Callable[[int], bytes]) -> tuple[int, int]:
     """Read the header of a pack file.
 
     Args: