Skip to content

Commit 5d7e55b

Browse files
committed
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 - python/mypy#604 - gitpython-developers#1755 (comment)
1 parent e984bfe commit 5d7e55b

File tree

7 files changed

+7
-7
lines changed

7 files changed

+7
-7
lines changed

Diff for: git/cmd.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ def __del__(self) -> None:
846846
self._stream.read(bytes_left + 1)
847847
# END handle incomplete read
848848

849-
def __init__(self, working_dir: Union[None, PathLike] = None):
849+
def __init__(self, working_dir: Union[None, PathLike] = None) -> None:
850850
"""Initialize this instance with:
851851
852852
:param working_dir:

Diff for: git/objects/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Object(LazyMixin):
5555

5656
type: Union[Lit_commit_ish, None] = None
5757

58-
def __init__(self, repo: "Repo", binsha: bytes):
58+
def __init__(self, repo: "Repo", binsha: bytes) -> None:
5959
"""Initialize an object by identifying it by its binary sha.
6060
6161
All keyword arguments will be set on demand if ``None``.

Diff for: git/objects/submodule/root.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class RootModule(Submodule):
5656

5757
k_root_name = "__ROOT__"
5858

59-
def __init__(self, repo: "Repo"):
59+
def __init__(self, repo: "Repo") -> None:
6060
# repo, binsha, mode=None, path=None, name = None, parent_commit=None, url=None, ref=None)
6161
super().__init__(
6262
repo,

Diff for: git/refs/head.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class HEAD(SymbolicReference):
4444

4545
__slots__ = ()
4646

47-
def __init__(self, repo: "Repo", path: PathLike = _HEAD_NAME):
47+
def __init__(self, repo: "Repo", path: PathLike = _HEAD_NAME) -> None:
4848
if path != self._HEAD_NAME:
4949
raise ValueError("HEAD instance must point to %r, got %r" % (self._HEAD_NAME, path))
5050
super().__init__(repo, path)

Diff for: git/refs/log.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def __new__(cls, filepath: Union[PathLike, None] = None) -> "RefLog":
164164
inst = super().__new__(cls)
165165
return inst
166166

167-
def __init__(self, filepath: Union[PathLike, None] = None):
167+
def __init__(self, filepath: Union[PathLike, None] = None) -> None:
168168
"""Initialize this instance with an optional filepath, from which we will
169169
initialize our data. The path is also used to write changes back using the
170170
:meth:`write` method."""

Diff for: git/refs/symbolic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class SymbolicReference:
7474
_remote_common_path_default = "refs/remotes"
7575
_id_attribute_ = "name"
7676

77-
def __init__(self, repo: "Repo", path: PathLike, check_path: bool = False):
77+
def __init__(self, repo: "Repo", path: PathLike, check_path: bool = False) -> None:
7878
self.repo = repo
7979
self.path = path
8080

Diff for: git/util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ class Stats:
917917

918918
__slots__ = ("total", "files")
919919

920-
def __init__(self, total: Total_TD, files: Dict[PathLike, Files_TD]):
920+
def __init__(self, total: Total_TD, files: Dict[PathLike, Files_TD]) -> None:
921921
self.total = total
922922
self.files = files
923923

0 commit comments

Comments
 (0)