Type Traversable.traverse() better, start types of submodule · gitpython-developers/GitPython@82b131c
@@ -3,12 +3,10 @@
33#
44# This module is part of GitPython and is released under
55# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6-76from gitdb import IStream
87from git.util import (
98hex_to_bin,
109Actor,
11-IterableObj,
1210Stats,
1311finalize_process
1412)
@@ -17,8 +15,8 @@
1715from .tree import Tree
1816from . import base
1917from .util import (
20-Traversable,
2118Serializable,
19+TraversableIterableObj,
2220parse_date,
2321altz_to_utctz_str,
2422parse_actor_and_date,
@@ -36,18 +34,25 @@
3634from io import BytesIO
3735import logging
383639-from typing import List, Tuple, Union, TYPE_CHECKING
37+38+# typing ------------------------------------------------------------------
39+40+from typing import Any, Iterator, List, Sequence, Tuple, Union, TYPE_CHECKING
41+42+from git.types import PathLike
40434144if TYPE_CHECKING:
4245from git.repo import Repo
434647+# ------------------------------------------------------------------------
48+4449log = logging.getLogger('git.objects.commit')
4550log.addHandler(logging.NullHandler())
46514752__all__ = ('Commit', )
4853495450-class Commit(base.Object, IterableObj, Diffable, Traversable, Serializable):
55+class Commit(base.Object, TraversableIterableObj, Diffable, Serializable):
51565257"""Wraps a git Commit object.
5358@@ -73,7 +78,8 @@ class Commit(base.Object, IterableObj, Diffable, Traversable, Serializable):
7378"message", "parents", "encoding", "gpgsig")
7479_id_attribute_ = "hexsha"
758076-def __init__(self, repo, binsha, tree=None, author=None, authored_date=None, author_tz_offset=None,
81+def __init__(self, repo, binsha, tree=None, author: Union[Actor, None] = None,
82+authored_date=None, author_tz_offset=None,
7783committer=None, committed_date=None, committer_tz_offset=None,
7884message=None, parents: Union[Tuple['Commit', ...], List['Commit'], None] = None,
7985encoding=None, gpgsig=None):
@@ -139,7 +145,7 @@ def __init__(self, repo, binsha, tree=None, author=None, authored_date=None, aut
139145self.gpgsig = gpgsig
140146141147@classmethod
142-def _get_intermediate_items(cls, commit: 'Commit') -> Tuple['Commit', ...]: # type: ignore ## cos overriding super
148+def _get_intermediate_items(cls, commit: 'Commit') -> Tuple['Commit', ...]:
143149return tuple(commit.parents)
144150145151@classmethod
@@ -225,7 +231,9 @@ def name_rev(self):
225231return self.repo.git.name_rev(self)
226232227233@classmethod
228-def iter_items(cls, repo, rev, paths='', **kwargs):
234+def iter_items(cls, repo: 'Repo', rev, # type: ignore
235+paths: Union[PathLike, Sequence[PathLike]] = '', **kwargs: Any
236+ ) -> Iterator['Commit']:
229237"""Find all commits matching the given criteria.
230238231239 :param repo: is the Repo
@@ -245,15 +253,23 @@ def iter_items(cls, repo, rev, paths='', **kwargs):
245253246254# use -- in any case, to prevent possibility of ambiguous arguments
247255# see https://github.com/gitpython-developers/GitPython/issues/264
248-args = ['--']
256+257+args_list: List[Union[PathLike, Sequence[PathLike]]] = ['--']
258+249259if paths:
250-args.extend((paths, ))
260+paths_tup: Tuple[PathLike, ...]
261+if isinstance(paths, (str, os.PathLike)):
262+paths_tup = (paths, )
263+else:
264+paths_tup = tuple(paths)
265+266+args_list.extend(paths_tup)
251267# END if paths
252268253-proc = repo.git.rev_list(rev, args, as_process=True, **kwargs)
269+proc = repo.git.rev_list(rev, args_list, as_process=True, **kwargs)
254270return cls._iter_from_process_or_stream(repo, proc)
255271256-def iter_parents(self, paths='', **kwargs):
272+def iter_parents(self, paths: Union[PathLike, Sequence[PathLike]] = '', **kwargs) -> Iterator['Commit']:
257273"""Iterate _all_ parents of this commit.
258274259275 :param paths:
@@ -269,7 +285,7 @@ def iter_parents(self, paths='', **kwargs):
269285270286return self.iter_items(self.repo, self, paths, **kwargs)
271287272-@property
288+@ property
273289def stats(self):
274290"""Create a git stat from changes between this commit and its first parent
275291 or from all changes done if this is the very first commit.
@@ -286,7 +302,7 @@ def stats(self):
286302text = self.repo.git.diff(self.parents[0].hexsha, self.hexsha, '--', numstat=True)
287303return Stats._list_from_string(self.repo, text)
288304289-@classmethod
305+@ classmethod
290306def _iter_from_process_or_stream(cls, repo, proc_or_stream):
291307"""Parse out commit information into a list of Commit objects
292308 We expect one-line per commit, and parse the actual commit information directly
@@ -317,7 +333,7 @@ def _iter_from_process_or_stream(cls, repo, proc_or_stream):
317333if hasattr(proc_or_stream, 'wait'):
318334finalize_process(proc_or_stream)
319335320-@classmethod
336+@ classmethod
321337def create_from_tree(cls, repo, tree, message, parent_commits=None, head=False, author=None, committer=None,
322338author_date=None, commit_date=None):
323339"""Commit the given tree, creating a commit object.