Skip to content

Commit

Permalink
Add return-type annotation on __init__ methods
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
EliahKagan committed Mar 5, 2024
1 parent e984bfe commit 5d7e55b
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion git/objects/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``.
Expand Down
2 changes: 1 addition & 1 deletion git/objects/submodule/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion git/refs/head.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion git/refs/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion git/refs/symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 5d7e55b

Please sign in to comment.