From 9f226fc47683038acd3304dd7d59be18984d3737 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Mon, 4 Mar 2024 19:38:10 -0500 Subject: [PATCH 1/3] Remove unneeded annotation on __slots__ variable The correct type is inferred, and no other __slots__ variable in the codebase has an annotation. --- git/cmd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git/cmd.py b/git/cmd.py index c7cec48d7..8550830aa 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -749,7 +749,7 @@ class CatFileContentStream: rest to ensure the underlying stream continues to work. """ - __slots__: Tuple[str, ...] = ("_stream", "_nbr", "_size") + __slots__ = ("_stream", "_nbr", "_size") def __init__(self, size: int, stream: IO[bytes]) -> None: self._stream = stream From e984bfebd9f1934b9810ac6cccb1bb803c64dd21 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Mon, 4 Mar 2024 20:02:47 -0500 Subject: [PATCH 2/3] Fix some underindented portions of docstrings A few docstrings had parts that were meant to be indented the usual four spaces beyond the surrounding indentation, but were indented only three spaces beyond it instead. --- git/cmd.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/git/cmd.py b/git/cmd.py index 8550830aa..3d5992dbd 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -850,10 +850,10 @@ def __init__(self, working_dir: Union[None, PathLike] = None): """Initialize this instance with: :param working_dir: - Git directory we should work in. If ``None``, we always work in the current - directory as returned by :func:`os.getcwd`. - This is meant to be the working tree directory if available, or the - ``.git`` directory in case of bare repositories. + Git directory we should work in. If ``None``, we always work in the current + directory as returned by :func:`os.getcwd`. + This is meant to be the working tree directory if available, or the + ``.git`` directory in case of bare repositories. """ super().__init__() self._working_dir = expand_path(working_dir) @@ -1103,8 +1103,8 @@ def execute( :raise git.exc.GitCommandError: :note: - If you add additional keyword arguments to the signature of this method, - you must update the ``execute_kwargs`` variable housed in this module. + If you add additional keyword arguments to the signature of this method, you + must update the ``execute_kwargs`` variable housed in this module. """ # Remove password for the command if present. redacted_command = remove_password_if_present(command) @@ -1438,7 +1438,7 @@ def _call_process( turns into:: - git rev-list max-count 10 --header master + git rev-list max-count 10 --header master :return: Same as :meth:`execute`. If no args are given, used :meth:`execute`'s From 5d7e55b8dd2abaacd5397860e6efd7bd4215fe12 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Mon, 4 Mar 2024 20:11:40 -0500 Subject: [PATCH 3/3] Add return-type annotation on __init__ methods Although sometimes found unintuitive, the reutrn type of a class's __init__ method is best annotated as None, as in other functions that return implicitly or by operand-less return statements. Note that this is only a minor improvement, effectively just a style fix, because mypy treats __init__ specially and, *when at least one parameter is annotated*, its return type is implicitly None rather than implicitly Any like other functions. All the __init__ methods modified here did already have one or more annotated parameters. However, having __init__ methods without the return type makes it easier to introduce a bug where an __init__ method with no parameters besides self -- which itself should almost always be unannotated and is properly inferred -- is written and the return annotation needed to get mypy to regard it as statically typed, so it checks it at all, is omitted. (It is also inelegant when one considers the meaning of __init__ and the distinction between it and __new__.) This commit does not add any return type annotations to functions in the test suite, since the test suite doesn't currently use static typing. Further reading: - https://peps.python.org/pep-0484/#the-meaning-of-annotations - https://github.com/python/mypy/issues/604 - https://github.com/gitpython-developers/GitPython/pull/1755#issuecomment-1837419098 --- git/cmd.py | 2 +- git/objects/base.py | 2 +- git/objects/submodule/root.py | 2 +- git/refs/head.py | 2 +- git/refs/log.py | 2 +- git/refs/symbolic.py | 2 +- git/util.py | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/git/cmd.py b/git/cmd.py index 3d5992dbd..731d0fab8 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -846,7 +846,7 @@ def __del__(self) -> None: self._stream.read(bytes_left + 1) # END handle incomplete read - def __init__(self, working_dir: Union[None, PathLike] = None): + def __init__(self, working_dir: Union[None, PathLike] = None) -> None: """Initialize this instance with: :param working_dir: diff --git a/git/objects/base.py b/git/objects/base.py index e78420e8a..2b8dd0ff6 100644 --- a/git/objects/base.py +++ b/git/objects/base.py @@ -55,7 +55,7 @@ class Object(LazyMixin): type: Union[Lit_commit_ish, None] = None - def __init__(self, repo: "Repo", binsha: bytes): + def __init__(self, repo: "Repo", binsha: bytes) -> None: """Initialize an object by identifying it by its binary sha. All keyword arguments will be set on demand if ``None``. diff --git a/git/objects/submodule/root.py b/git/objects/submodule/root.py index ac0f7ad94..3268d73a4 100644 --- a/git/objects/submodule/root.py +++ b/git/objects/submodule/root.py @@ -56,7 +56,7 @@ class RootModule(Submodule): k_root_name = "__ROOT__" - def __init__(self, repo: "Repo"): + def __init__(self, repo: "Repo") -> None: # repo, binsha, mode=None, path=None, name = None, parent_commit=None, url=None, ref=None) super().__init__( repo, diff --git a/git/refs/head.py b/git/refs/head.py index d546cb4ef..f6020f461 100644 --- a/git/refs/head.py +++ b/git/refs/head.py @@ -44,7 +44,7 @@ class HEAD(SymbolicReference): __slots__ = () - def __init__(self, repo: "Repo", path: PathLike = _HEAD_NAME): + def __init__(self, repo: "Repo", path: PathLike = _HEAD_NAME) -> None: if path != self._HEAD_NAME: raise ValueError("HEAD instance must point to %r, got %r" % (self._HEAD_NAME, path)) super().__init__(repo, path) diff --git a/git/refs/log.py b/git/refs/log.py index 7aefeb4e6..f98f56f11 100644 --- a/git/refs/log.py +++ b/git/refs/log.py @@ -164,7 +164,7 @@ def __new__(cls, filepath: Union[PathLike, None] = None) -> "RefLog": inst = super().__new__(cls) return inst - def __init__(self, filepath: Union[PathLike, None] = None): + def __init__(self, filepath: Union[PathLike, None] = None) -> None: """Initialize this instance with an optional filepath, from which we will initialize our data. The path is also used to write changes back using the :meth:`write` method.""" diff --git a/git/refs/symbolic.py b/git/refs/symbolic.py index 142952e06..16aada0a7 100644 --- a/git/refs/symbolic.py +++ b/git/refs/symbolic.py @@ -74,7 +74,7 @@ class SymbolicReference: _remote_common_path_default = "refs/remotes" _id_attribute_ = "name" - def __init__(self, repo: "Repo", path: PathLike, check_path: bool = False): + def __init__(self, repo: "Repo", path: PathLike, check_path: bool = False) -> None: self.repo = repo self.path = path diff --git a/git/util.py b/git/util.py index 52f9dbdad..d272dc53c 100644 --- a/git/util.py +++ b/git/util.py @@ -917,7 +917,7 @@ class Stats: __slots__ = ("total", "files") - def __init__(self, total: Total_TD, files: Dict[PathLike, Files_TD]): + def __init__(self, total: Total_TD, files: Dict[PathLike, Files_TD]) -> None: self.total = total self.files = files