소스 검색

Some minor cleanups:

* avoid name clashes with builtins
* simplifications
Jelmer Vernooij 1 개월 전
부모
커밋
c74fa5e288
5개의 변경된 파일30개의 추가작업 그리고 40개의 파일을 삭제
  1. 3 4
      dulwich/__init__.py
  2. 8 17
      dulwich/errors.py
  3. 4 4
      dulwich/objects.py
  4. 7 7
      dulwich/pack.py
  5. 8 8
      dulwich/refs.py

+ 3 - 4
dulwich/__init__.py

@@ -4,7 +4,7 @@
 #
 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
 # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
-# General Public License as public by the Free Software Foundation; version 2.0
+# General Public License as published by the Free Software Foundation; version 2.0
 # or (at your option) any later version. You can redistribute it and/or
 # modify it under the terms of either of these two licenses.
 #
@@ -24,7 +24,7 @@
 """Python implementation of the Git file formats and protocols."""
 
 import sys
-from typing import Any, Callable, Optional, TypeVar
+from typing import Callable, Optional, TypeVar
 
 if sys.version_info >= (3, 10):
     from typing import ParamSpec
@@ -33,11 +33,10 @@ else:
 
 __version__ = (0, 23, 3)
 
-__all__ = ["replace_me"]
+__all__ = ["__version__", "replace_me"]
 
 P = ParamSpec("P")
 R = TypeVar("R")
-F = TypeVar("F", bound=Callable[..., Any])
 
 try:
     from dissolve import replace_me

+ 8 - 17
dulwich/errors.py

@@ -4,7 +4,7 @@
 #
 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
 # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
-# General Public License as public by the Free Software Foundation; version 2.0
+# General Public License as published by the Free Software Foundation; version 2.0
 # or (at your option) any later version. You can redistribute it and/or
 # modify it under the terms of either of these two licenses.
 #
@@ -40,16 +40,10 @@ class ChecksumMismatch(Exception):
         self.expected = expected
         self.got = got
         self.extra = extra
-        if self.extra is None:
-            Exception.__init__(
-                self,
-                f"Checksum mismatch: Expected {expected}, got {got}",
-            )
-        else:
-            Exception.__init__(
-                self,
-                f"Checksum mismatch: Expected {expected}, got {got}; {extra}",
-            )
+        message = f"Checksum mismatch: Expected {expected}, got {got}"
+        if self.extra is not None:
+            message += f"; {extra}"
+        Exception.__init__(self, message)
 
 
 class WrongObjectException(Exception):
@@ -141,7 +135,7 @@ class HangupException(GitProtocolError):
         if stderr_lines:
             super().__init__(
                 "\n".join(
-                    [line.decode("utf-8", "surrogateescape") for line in stderr_lines]
+                    line.decode("utf-8", "surrogateescape") for line in stderr_lines
                 )
             )
         else:
@@ -156,11 +150,8 @@ class UnexpectedCommandError(GitProtocolError):
     """Unexpected command received in a proto line."""
 
     def __init__(self, command) -> None:
-        if command is None:
-            command = "flush-pkt"
-        else:
-            command = f"command {command}"
-        super().__init__(f"Protocol got unexpected {command}")
+        command_str = "flush-pkt" if command is None else f"command {command}"
+        super().__init__(f"Protocol got unexpected {command_str}")
 
 
 class FileFormatException(Exception):

+ 4 - 4
dulwich/objects.py

@@ -4,7 +4,7 @@
 #
 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
 # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
-# General Public License as public by the Free Software Foundation; version 2.0
+# General Public License as published by the Free Software Foundation; version 2.0
 # or (at your option) any later version. You can redistribute it and/or
 # modify it under the terms of either of these two licenses.
 #
@@ -148,10 +148,10 @@ def hex_to_filename(
     # as path.
     if type(path) is not type(hex) and isinstance(path, str):
         hex = hex.decode("ascii")  # type: ignore
-    dir = hex[:2]
-    file = hex[2:]
+    dir_name = hex[:2]
+    file_name = hex[2:]
     # Check from object dir
-    return os.path.join(path, dir, file)  # type: ignore
+    return os.path.join(path, dir_name, file_name)  # type: ignore
 
 
 def filename_to_hex(filename: Union[str, bytes]) -> str:

+ 7 - 7
dulwich/pack.py

@@ -4,7 +4,7 @@
 #
 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
 # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
-# General Public License as public by the Free Software Foundation; version 2.0
+# General Public License as published by the Free Software Foundation; version 2.0
 # or (at your option) any later version. You can redistribute it and/or
 # modify it under the terms of either of these two licenses.
 #
@@ -517,7 +517,7 @@ class PackIndex:
 
     def object_sha1(self, index: int) -> bytes:
         """Return the SHA1 corresponding to the index in the pack file."""
-        for name, offset, crc32 in self.iterentries():
+        for name, offset, _crc32 in self.iterentries():
             if offset == index:
                 return name
         else:
@@ -561,7 +561,7 @@ class MemoryPackIndex(PackIndex):
         """
         self._by_sha = {}
         self._by_offset = {}
-        for name, offset, crc32 in entries:
+        for name, offset, _crc32 in entries:
             self._by_sha[name] = offset
             self._by_offset[offset] = name
         self._entries = entries
@@ -1132,7 +1132,7 @@ class PackStreamReader:
         """
         pack_version, self._num_objects = read_pack_header(self.read)
 
-        for i in range(self._num_objects):
+        for _ in range(self._num_objects):
             offset = self.offset
             unpacked, unused = unpack_object(
                 self.read,
@@ -2403,13 +2403,13 @@ def write_pack_index_v1(f, entries, pack_checksum):
     """
     f = SHA1Writer(f)
     fan_out_table = defaultdict(lambda: 0)
-    for name, offset, entry_checksum in entries:
+    for name, _offset, _entry_checksum in entries:
         fan_out_table[ord(name[:1])] += 1
     # Fan-out table
     for i in range(0x100):
         f.write(struct.pack(">L", fan_out_table[i]))
         fan_out_table[i + 1] += fan_out_table[i]
-    for name, offset, entry_checksum in entries:
+    for name, offset, _entry_checksum in entries:
         if not (offset <= 0xFFFFFFFF):
             raise TypeError("pack format 1 only supports offsets < 2Gb")
         f.write(struct.pack(">L20s", offset, name))
@@ -2987,7 +2987,7 @@ class Pack:
         # Now grab the base object (mustn't be a delta) and apply the
         # deltas all the way up the stack.
         chunks = base_obj
-        for prev_offset, delta_type, delta in reversed(delta_stack):
+        for prev_offset, _delta_type, delta in reversed(delta_stack):
             chunks = apply_delta(chunks, delta)
             # TODO(dborowitz): This can result in poor performance if
             # large base objects are separated from deltas in the pack.

+ 8 - 8
dulwich/refs.py

@@ -3,7 +3,7 @@
 #
 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
 # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
-# General Public License as public by the Free Software Foundation; version 2.0
+# General Public License as published by the Free Software Foundation; version 2.0
 # or (at your option) any later version. You can redistribute it and/or
 # modify it under the terms of either of these two licenses.
 #
@@ -669,12 +669,12 @@ class DiskRefsContainer(RefsContainer):
         subkeys = set()
         path = self.refpath(base)
         for root, unused_dirs, files in os.walk(path):
-            dir = root[len(path) :]
+            directory = root[len(path) :]
             if os.path.sep != "/":
-                dir = dir.replace(os.fsencode(os.path.sep), b"/")
-            dir = dir.strip(b"/")
+                directory = directory.replace(os.fsencode(os.path.sep), b"/")
+            directory = directory.strip(b"/")
             for filename in files:
-                refname = b"/".join(([dir] if dir else []) + [filename])
+                refname = b"/".join(([directory] if directory else []) + [filename])
                 # check_ref_format requires at least one /, so we prepend the
                 # base before calling it.
                 if check_ref_format(base + b"/" + refname):
@@ -691,11 +691,11 @@ class DiskRefsContainer(RefsContainer):
         path = self.refpath(b"")
         refspath = self.refpath(b"refs")
         for root, unused_dirs, files in os.walk(refspath):
-            dir = root[len(path) :]
+            directory = root[len(path) :]
             if os.path.sep != "/":
-                dir = dir.replace(os.fsencode(os.path.sep), b"/")
+                directory = directory.replace(os.fsencode(os.path.sep), b"/")
             for filename in files:
-                refname = b"/".join([dir, filename])
+                refname = b"/".join([directory, filename])
                 if check_ref_format(refname):
                     allkeys.add(refname)
         allkeys.update(self.get_packed_refs())