Skip to content

More types for symbolic.py #1307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3347c00
change ordereddict guard, add type: ignore
Yobmod Jul 28, 2021
77a7769
Merge branch 'main' of https://github.com/Yobmod/GitPython
Yobmod Jul 28, 2021
28fdd30
Fix SymbolicReference reference typing
Yobmod Jul 28, 2021
3abc837
Add another type ignore for Ordereddict
Yobmod Jul 28, 2021
6791424
Rmv py 3.10 check for typing.Ordereddict - its deprecated then, but w…
Yobmod Jul 28, 2021
c464e33
Fix some SymbolicReference types
Yobmod Jul 28, 2021
7cf30c1
Fix forwardref
Yobmod Jul 28, 2021
5b880c0
Fix more missing types in Symbolic.py
Yobmod Jul 28, 2021
07d71e5
Fix more missing types in Symbolic.py
Yobmod Jul 28, 2021
b8b07b9
Fix more missing types in Symbolic.py.
Yobmod Jul 28, 2021
390efbf
Fix more missing types in Symbolic.py, cos GuthubActions pytest stuck
Yobmod Jul 28, 2021
070f5c0
Rmv test file
Yobmod Jul 28, 2021
adc00dd
Fix more missing types in Symbolic.py, cos GuthubActions pytest stuck
Yobmod Jul 28, 2021
28251c3
Try downgrading pip
Yobmod Jul 28, 2021
dbb689b
its not pip...
Yobmod Jul 28, 2021
cf29514
try https://github.com/actions/virtual-environments/issues/709 workar…
Yobmod Jul 28, 2021
f1e6e8d
Merge branch 'main' of https://github.com/Yobmod/GitPython
Yobmod Jul 31, 2021
15d1c01
Add type to symbolicreference.name()
Yobmod Jul 31, 2021
34e9850
Add type to symbolicreference.iter_items()
Yobmod Jul 31, 2021
265d40b
Add type to symbolicreference.rename()
Yobmod Jul 31, 2021
bdd6a43
Add type to symbolicreference.__repr__()
Yobmod Jul 31, 2021
1f92267
Add type to symbolicreference._get_ref_info()
Yobmod Jul 31, 2021
ad4517f
Add type to symbolicreference._get_packed_refs_path()
Yobmod Jul 31, 2021
6b0faba
Add type to symbolicreference.dereference_recursive()
Yobmod Jul 31, 2021
7e972b9
Add type to symbolicreference.dereference_recursive()
Yobmod Jul 31, 2021
24c1242
Add type to symbolicreference()
Yobmod Jul 31, 2021
8eedc9d
Add type to symbolicreference.get_()
Yobmod Jul 31, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix more missing types in Symbolic.py, cos GuthubActions pytest stuck
  • Loading branch information
Yobmod committed Jul 28, 2021
commit adc00dd1773ee1b532803b2272cc989f11e09f8a
1 change: 1 addition & 0 deletions git/objects/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def __init__(self, repo: 'Repo', binsha: bytes, tree: Union[Tree, None] = None,
as what time.altzone returns. The sign is inverted compared to git's
UTC timezone."""
super(Commit, self).__init__(repo, binsha)
self.binsha = binsha
if tree is not None:
assert isinstance(tree, Tree), "Tree needs to be a Tree instance, was %s" % type(tree)
if tree is not None:
Expand Down
5 changes: 5 additions & 0 deletions git/objects/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,11 @@ def list_traverse(self: T_TIobj, *args: Any, **kwargs: Any) -> IterableList[T_TI
return super(TraversableIterableObj, self)._list_traverse(* args, **kwargs)

@ overload # type: ignore
def traverse(self: T_TIobj
) -> Iterator[T_TIobj]:
...

@ overload
def traverse(self: T_TIobj,
predicate: Callable[[Union[T_TIobj, Tuple[Union[T_TIobj, None], T_TIobj]], int], bool],
prune: Callable[[Union[T_TIobj, Tuple[Union[T_TIobj, None], T_TIobj]], int], bool],
Expand Down
13 changes: 7 additions & 6 deletions git/refs/head.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from git.config import SectionConstraint
from git.config import GitConfigParser, SectionConstraint
from git.util import join_path
from git.exc import GitCommandError

Expand Down Expand Up @@ -203,7 +203,7 @@ def rename(self, new_path: PathLike, force: bool = False) -> 'Head':
self.path = "%s/%s" % (self._common_path_default, new_path)
return self

def checkout(self, force: bool = False, **kwargs: Any):
def checkout(self, force: bool = False, **kwargs: Any) -> Union['HEAD', 'Head']:
"""Checkout this head by setting the HEAD to this reference, by updating the index
to reflect the tree we point to and by updating the working tree to reflect
the latest index.
Expand Down Expand Up @@ -235,10 +235,11 @@ def checkout(self, force: bool = False, **kwargs: Any):
self.repo.git.checkout(self, **kwargs)
if self.repo.head.is_detached:
return self.repo.head
return self.repo.active_branch
else:
return self.repo.active_branch

#{ Configuration
def _config_parser(self, read_only: bool) -> SectionConstraint:
def _config_parser(self, read_only: bool) -> SectionConstraint[GitConfigParser]:
if read_only:
parser = self.repo.config_reader()
else:
Expand All @@ -247,13 +248,13 @@ def _config_parser(self, read_only: bool) -> SectionConstraint:

return SectionConstraint(parser, 'branch "%s"' % self.name)

def config_reader(self) -> SectionConstraint:
def config_reader(self) -> SectionConstraint[GitConfigParser]:
"""
:return: A configuration parser instance constrained to only read
this instance's values"""
return self._config_parser(read_only=True)

def config_writer(self) -> SectionConstraint:
def config_writer(self) -> SectionConstraint[GitConfigParser]:
"""
:return: A configuration writer instance with read-and write access
to options of this head"""
Expand Down
4 changes: 3 additions & 1 deletion git/refs/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def __str__(self) -> str:

#{ Interface

def set_object(self, object: Commit_ish, logmsg: Union[str, None] = None) -> 'Reference': # @ReservedAssignment
# @ReservedAssignment
def set_object(self, object: Union[Commit_ish, 'SymbolicReference'], logmsg: Union[str, None] = None
) -> 'SymbolicReference':
"""Special version which checks if the head-log needs an update as well
:return: self"""
oldbinsha = None
Expand Down
47 changes: 29 additions & 18 deletions git/refs/symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
BadName
)

from .log import RefLog
from .log import RefLog, RefLogEntry

# typing ------------------------------------------------------------------

Expand All @@ -26,6 +26,8 @@
if TYPE_CHECKING:
from git.repo import Repo
from git.refs import Reference, Head, TagReference, RemoteReference
from git.config import GitConfigParser
from git.objects.commit import Actor

T_References = TypeVar('T_References', bound='SymbolicReference')

Expand Down Expand Up @@ -229,11 +231,13 @@ def set_commit(self, commit: Union[Commit, 'SymbolicReference', str],
invalid_type = False
if isinstance(commit, Object):
invalid_type = commit.type != Commit.type
commit = cast('Commit', commit)
elif isinstance(commit, SymbolicReference):
invalid_type = commit.object.type != Commit.type
else:
try:
invalid_type = self.repo.rev_parse(commit).type != Commit.type
commit = self.repo.rev_parse(commit)
invalid_type = commit.type != Commit.type
except (BadObject, BadName) as e:
raise ValueError("Invalid object: %s" % commit) from e
# END handle exception
Expand All @@ -249,7 +253,9 @@ def set_commit(self, commit: Union[Commit, 'SymbolicReference', str],
# return self
return None

def set_object(self, object, logmsg=None): # @ReservedAssignment
def set_object(self, object: Union[Commit_ish, 'SymbolicReference'],
logmsg: Union[str, None] = None
) -> 'SymbolicReference': # @ReservedAssignment
"""Set the object we point to, possibly dereference our symbolic reference first.
If the reference does not exist, it will be created

Expand All @@ -276,8 +282,8 @@ def set_object(self, object, logmsg=None): # @ReservedAssignment
# set the commit on our reference
return self._get_reference().set_object(object, logmsg)

commit = property(_get_commit, set_commit, doc="Query or set commits directly")
object = property(_get_object, set_object, doc="Return the object our ref currently refers to")
commit = cast('Commit', property(_get_commit, set_commit, doc="Query or set commits directly"))
object = property(_get_object, set_object, doc="Return the object our ref currently refers to") # type: ignore

def _get_reference(self
) -> Union['Head', 'RemoteReference', 'TagReference', 'Reference']:
Expand All @@ -290,7 +296,7 @@ def _get_reference(self
return self.from_path(self.repo, target_ref_path)

def set_reference(self, ref: Union[str, Commit_ish, 'SymbolicReference'], logmsg: Union[str, None] = None
) -> None:
) -> 'SymbolicReference':
"""Set ourselves to the given ref. It will stay a symbol if the ref is a Reference.
Otherwise an Object, given as Object instance or refspec, is assumed and if valid,
will be set which effectively detaches the refererence if it was a purely
Expand Down Expand Up @@ -331,7 +337,7 @@ def set_reference(self, ref: Union[str, Commit_ish, 'SymbolicReference'], logmsg
raise TypeError("Require commit, got %r" % obj)
# END verify type

oldbinsha = None
oldbinsha: bytes = b''
if logmsg is not None:
try:
oldbinsha = self.commit.binsha
Expand All @@ -357,15 +363,15 @@ def set_reference(self, ref: Union[str, Commit_ish, 'SymbolicReference'], logmsg
if logmsg is not None:
self.log_append(oldbinsha, logmsg)

return None
return self

@ property
def reference(self) -> Union['Head', 'RemoteReference', 'TagReference', 'Reference']:
return self._get_reference()

@ reference.setter
def reference(self, ref: Union[str, Commit_ish, 'SymbolicReference'], logmsg: Union[str, None] = None
) -> None:
) -> 'SymbolicReference':
return self.set_reference(ref=ref, logmsg=logmsg)

def is_valid(self) -> bool:
Expand All @@ -392,7 +398,7 @@ def is_detached(self):
except TypeError:
return True

def log(self):
def log(self) -> 'RefLog':
"""
:return: RefLog for this reference. Its last entry reflects the latest change
applied to this reference
Expand All @@ -401,7 +407,8 @@ def log(self):
instead of calling this method repeatedly. It should be considered read-only."""
return RefLog.from_file(RefLog.path(self))

def log_append(self, oldbinsha, message, newbinsha=None):
def log_append(self, oldbinsha: bytes, message: Union[str, None],
newbinsha: Union[bytes, None] = None) -> 'RefLogEntry':
"""Append a logentry to the logfile of this ref

:param oldbinsha: binary sha this ref used to point to
Expand All @@ -413,15 +420,19 @@ def log_append(self, oldbinsha, message, newbinsha=None):
# correct to allow overriding the committer on a per-commit level.
# See https://github.com/gitpython-developers/GitPython/pull/146
try:
committer_or_reader = self.commit.committer
committer_or_reader: Union['Actor', 'GitConfigParser'] = self.commit.committer
except ValueError:
committer_or_reader = self.repo.config_reader()
# end handle newly cloned repositories
return RefLog.append_entry(committer_or_reader, RefLog.path(self), oldbinsha,
(newbinsha is None and self.commit.binsha) or newbinsha,
message)
if newbinsha is None:
newbinsha = self.commit.binsha

if message is None:
message = ''

return RefLog.append_entry(committer_or_reader, RefLog.path(self), oldbinsha, newbinsha, message)

def log_entry(self, index):
def log_entry(self, index: int) -> RefLogEntry:
""":return: RefLogEntry at the given index
:param index: python list compatible positive or negative index

Expand Down Expand Up @@ -540,7 +551,7 @@ def _create(cls: Type[T_References], repo: 'Repo', path: PathLike, resolve: bool

@classmethod
def create(cls: Type[T_References], repo: 'Repo', path: PathLike,
reference: Union[Commit_ish, str, 'SymbolicReference'] = 'SymbolicReference',
reference: Union[str, 'SymbolicReference'] = 'SymbolicReference',
logmsg: Union[str, None] = None, force: bool = False, **kwargs: Any) -> T_References:
"""Create a new symbolic reference, hence a reference pointing , to another reference.

Expand All @@ -553,7 +564,7 @@ def create(cls: Type[T_References], repo: 'Repo', path: PathLike,

:param reference:
The reference to which the new symbolic reference should point to.
If it is a commit'ish, the symbolic ref will be detached.
If it is a ref to a commit'ish, the symbolic ref will be detached.

:param force:
if True, force creation even if a symbolic reference with that name already exists.
Expand Down
4 changes: 2 additions & 2 deletions git/refs/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def object(self) -> Commit_ish: # type: ignore[override]

@classmethod
def create(cls: Type['TagReference'], repo: 'Repo', path: PathLike,
reference: Union[Commit_ish, str, 'SymbolicReference'] = 'HEAD',
reference: Union[str, 'SymbolicReference'] = 'HEAD',
logmsg: Union[str, None] = None,
force: bool = False, **kwargs: Any) -> 'TagReference':
"""Create a new tag reference.
Expand All @@ -80,7 +80,7 @@ def create(cls: Type['TagReference'], repo: 'Repo', path: PathLike,
The prefix refs/tags is implied

:param ref:
A reference to the object you want to tag. It can be a commit, tree or
A reference to the Object you want to tag. The Object can be a commit, tree or
blob.

:param logmsg:
Expand Down