Forráskód Böngészése

Add more type annotations to functions in dulwich/porcelain.py

Jelmer Vernooij 5 hónapja
szülő
commit
ee2e6532c1
1 módosított fájl, 30 hozzáadás és 48 törlés
  1. 30 48
      dulwich/porcelain.py

+ 30 - 48
dulwich/porcelain.py

@@ -91,7 +91,17 @@ from contextlib import AbstractContextManager, closing, contextmanager
 from dataclasses import dataclass
 from dataclasses import dataclass
 from io import BytesIO, RawIOBase
 from io import BytesIO, RawIOBase
 from pathlib import Path
 from pathlib import Path
-from typing import BinaryIO, Callable, Optional, TypeVar, Union, cast, overload
+from typing import (
+    Any,
+    BinaryIO,
+    Callable,
+    Optional,
+    TextIO,
+    TypeVar,
+    Union,
+    cast,
+    overload,
+)
 
 
 from . import replace_me
 from . import replace_me
 from .archive import tar_stream
 from .archive import tar_stream
@@ -205,15 +215,7 @@ class CountObjectsResult:
 class NoneStream(RawIOBase):
 class NoneStream(RawIOBase):
     """Fallback if stdout or stderr are unavailable, does nothing."""
     """Fallback if stdout or stderr are unavailable, does nothing."""
 
 
-    def read(self, size=-1) -> None:
-        """Read bytes (no-op for NoneStream).
-
-        Args:
-          size: Number of bytes to read
-
-        Returns:
-          None
-        """
+    def read(self, size: int = -1) -> None:
         return None
         return None
 
 
     def readall(self) -> bytes:
     def readall(self) -> bytes:
@@ -645,8 +647,8 @@ def commit_tree(
 
 
 
 
 def init(
 def init(
-    path: Union[str, os.PathLike] = ".", *, bare=False, symlinks: Optional[bool] = None
-):
+    path: Union[str, os.PathLike] = ".", *, bare: bool = False, symlinks: Optional[bool] = None
+) -> Repo:
     """Create a new git repository.
     """Create a new git repository.
 
 
     Args:
     Args:
@@ -665,21 +667,21 @@ def init(
 
 
 
 
 def clone(
 def clone(
-    source,
+    source: Union[str, bytes, BaseRepo],
     target: Optional[Union[str, os.PathLike]] = None,
     target: Optional[Union[str, os.PathLike]] = None,
-    bare=False,
-    checkout=None,
-    errstream=default_bytes_err_stream,
-    outstream=None,
+    bare: bool = False,
+    checkout: Optional[bool] = None,
+    errstream: BinaryIO = default_bytes_err_stream,
+    outstream: Optional[BinaryIO] = None,
     origin: Optional[str] = "origin",
     origin: Optional[str] = "origin",
     depth: Optional[int] = None,
     depth: Optional[int] = None,
     branch: Optional[Union[str, bytes]] = None,
     branch: Optional[Union[str, bytes]] = None,
     config: Optional[Config] = None,
     config: Optional[Config] = None,
-    filter_spec=None,
+    filter_spec: Optional[bytes] = None,
     protocol_version: Optional[int] = None,
     protocol_version: Optional[int] = None,
     recurse_submodules: bool = False,
     recurse_submodules: bool = False,
-    **kwargs,
-):
+    **kwargs: Any,
+) -> Repo:
     """Clone a local or remote git repository.
     """Clone a local or remote git repository.
 
 
     Args:
     Args:
@@ -769,7 +771,7 @@ def clone(
     return repo
     return repo
 
 
 
 
-def add(repo: Union[str, os.PathLike, Repo] = ".", paths=None):
+def add(repo: Union[str, os.PathLike, Repo] = ".", paths: Optional[Union[list[Union[str, bytes, os.PathLike]], str, bytes, os.PathLike]] = None) -> tuple[set[str], set[str]]:
     """Add files to the staging area.
     """Add files to the staging area.
 
 
     Args:
     Args:
@@ -882,7 +884,7 @@ def add(repo: Union[str, os.PathLike, Repo] = ".", paths=None):
     return (relpaths, ignored)
     return (relpaths, ignored)
 
 
 
 
-def _is_subdir(subdir, parentdir):
+def _is_subdir(subdir: Union[str, os.PathLike], parentdir: Union[str, os.PathLike]) -> bool:
     """Check whether subdir is parentdir or a subdir of parentdir.
     """Check whether subdir is parentdir or a subdir of parentdir.
 
 
     If parentdir or subdir is a relative path, it will be disamgibuated
     If parentdir or subdir is a relative path, it will be disamgibuated
@@ -894,7 +896,7 @@ def _is_subdir(subdir, parentdir):
 
 
 
 
 # TODO: option to remove ignored files also, in line with `git clean -fdx`
 # TODO: option to remove ignored files also, in line with `git clean -fdx`
-def clean(repo: Union[str, os.PathLike, Repo] = ".", target_dir=None) -> None:
+def clean(repo: Union[str, os.PathLike, Repo] = ".", target_dir: Optional[Union[str, os.PathLike]] = None) -> None:
     """Remove any untracked files from the target directory recursively.
     """Remove any untracked files from the target directory recursively.
 
 
     Equivalent to running ``git clean -fd`` in target_dir.
     Equivalent to running ``git clean -fd`` in target_dir.
@@ -940,7 +942,7 @@ def clean(repo: Union[str, os.PathLike, Repo] = ".", target_dir=None) -> None:
                     os.remove(ap)
                     os.remove(ap)
 
 
 
 
-def remove(repo: Union[str, os.PathLike, Repo] = ".", paths=None, cached=False) -> None:
+def remove(repo: Union[str, os.PathLike, Repo] = ".", paths: Optional[list[Union[str, bytes, os.PathLike]]] = None, cached: bool = False) -> None:
     """Remove files from the staging area.
     """Remove files from the staging area.
 
 
     Args:
     Args:
@@ -1109,17 +1111,7 @@ def mv(
 move = mv
 move = mv
 
 
 
 
-def commit_decode(commit, contents, default_encoding=DEFAULT_ENCODING):
-    """Decode commit message contents to unicode.
-
-    Args:
-      commit: Commit object
-      contents: Raw commit message bytes
-      default_encoding: Encoding to use if none specified in commit
-
-    Returns:
-      Decoded commit message as unicode string
-    """
+def commit_decode(commit: Commit, contents: bytes, default_encoding: str = DEFAULT_ENCODING) -> str:
     if commit.encoding:
     if commit.encoding:
         encoding = commit.encoding.decode("ascii")
         encoding = commit.encoding.decode("ascii")
     else:
     else:
@@ -1127,17 +1119,7 @@ def commit_decode(commit, contents, default_encoding=DEFAULT_ENCODING):
     return contents.decode(encoding, "replace")
     return contents.decode(encoding, "replace")
 
 
 
 
-def commit_encode(commit, contents, default_encoding=DEFAULT_ENCODING):
-    """Encode commit message contents to bytes.
-
-    Args:
-      commit: Commit object
-      contents: Commit message as unicode string
-      default_encoding: Encoding to use if none specified in commit
-
-    Returns:
-      Encoded commit message as bytes
-    """
+def commit_encode(commit: Commit, contents: str, default_encoding: str = DEFAULT_ENCODING) -> bytes:
     if commit.encoding:
     if commit.encoding:
         encoding = commit.encoding.decode("ascii")
         encoding = commit.encoding.decode("ascii")
     else:
     else:
@@ -1145,7 +1127,7 @@ def commit_encode(commit, contents, default_encoding=DEFAULT_ENCODING):
     return contents.encode(encoding)
     return contents.encode(encoding)
 
 
 
 
-def print_commit(commit, decode, outstream=sys.stdout) -> None:
+def print_commit(commit: Commit, decode: Callable[[Commit, bytes], str], outstream: TextIO = sys.stdout) -> None:
     """Write a human-readable commit log entry.
     """Write a human-readable commit log entry.
 
 
     Args:
     Args:
@@ -1175,7 +1157,7 @@ def print_commit(commit, decode, outstream=sys.stdout) -> None:
         outstream.write("\n")
         outstream.write("\n")
 
 
 
 
-def print_tag(tag, decode, outstream=sys.stdout) -> None:
+def print_tag(tag: Tag, decode: Callable[[bytes], str], outstream: TextIO = sys.stdout) -> None:
     """Write a human-readable tag.
     """Write a human-readable tag.
 
 
     Args:
     Args: