Skip to content

Commit

Permalink
Fix how HEAD annotates inherited commit property
Browse files Browse the repository at this point in the history
HEAD inherits its commit attribute from SymbolicReference, which
defines it as a property using an explicit function call to
`property`, rather than using it as a decorator. Due at least to
python/mypy#16827, mypy has trouble with
this. Currently that assignment in SymbolicReference is marked as
type[ignore], which suppresses a type error (or something mypy
regards as one) in the call itself. Even without that type comment,
the type of the SymboilicReference instance attribute produced by
the property is inferred as Any. This commit does not change that.

This commit replaces the HEAD class's partially successful attempt
to annotate `commit` as `self.commit` in `__init__` with a fully
recognized annotation for `commit` in the class scope (which is
interpreted as an instance attribute).

Merely removing the annotation in `__init__` is sufficient to make
the mypy error for it go away, but this causes the inferred type of
`x.commit` when `x` is a `HEAD` instance to be inferred as Any
rather than the desired specific type Commit. That is why this also
adds an annotation to the class to achieve that without causing its
own mypy error as the old one did.

Once the SymbolicReference.commit property has a more specific
static type, however that is to be achieved, there will be no need
for the annotation added in the HEAD class body here. (This differs
from the situation in 3aeef46 before this, where Diffable does not
inherit any `repo` attribute--and is intended not to--and therefore
had to introduce an annotation for it, for mypy to accept it.)
  • Loading branch information
EliahKagan committed Mar 8, 2024
1 parent 3aeef46 commit f1cc1fe
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion git/refs/head.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ class HEAD(SymbolicReference):

__slots__ = ()

# TODO: This can be removed once SymbolicReference.commit has static type hints.
commit: "Commit"

def __init__(self, repo: "Repo", path: PathLike = _HEAD_NAME):
if path != self._HEAD_NAME:
raise ValueError("HEAD instance must point to %r, got %r" % (self._HEAD_NAME, path))
super().__init__(repo, path)
self.commit: "Commit"

def orig_head(self) -> SymbolicReference:
"""
Expand Down

0 comments on commit f1cc1fe

Please sign in to comment.