Skip to content
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

Start adding types to Submodule, add py.typed to manifest #1282

Merged
merged 12 commits into from
Jul 1, 2021
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ include README.md
include VERSION
include requirements.txt
include test-requirements.txt
include git/py.typed

recursive-include doc *
recursive-exclude test *
Expand Down
4 changes: 1 addition & 3 deletions git/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

import configparser as cp

from pathlib import Path

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

from typing import Any, Callable, IO, List, Dict, Sequence, TYPE_CHECKING, Tuple, Union, cast, overload
Expand Down Expand Up @@ -330,7 +328,7 @@ def _acquire_lock(self) -> None:
"Write-ConfigParsers can operate on a single file only, multiple files have been passed")
# END single file check

if isinstance(self._file_or_files, (str, Path)): # cannot narrow by os._pathlike until 3.5 dropped
if isinstance(self._file_or_files, (str, os.PathLike)):
file_or_files = self._file_or_files
else:
file_or_files = cast(IO, self._file_or_files).name
Expand Down
5 changes: 3 additions & 2 deletions git/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
from git.refs.reference import Reference

import glob
from io import BytesIO
import os
Expand Down Expand Up @@ -74,6 +74,7 @@
if TYPE_CHECKING:
from subprocess import Popen
from git.repo import Repo
from git.refs.reference import Reference


StageType = int
Expand Down Expand Up @@ -1191,7 +1192,7 @@ def handle_stderr(proc: 'Popen[bytes]', iter_checked_out_files: Iterable[PathLik
assert "Should not reach this point"

@default_index
def reset(self, commit: Union[Commit, Reference, str] = 'HEAD', working_tree: bool = False,
def reset(self, commit: Union[Commit, 'Reference', str] = 'HEAD', working_tree: bool = False,
paths: Union[None, Iterable[PathLike]] = None,
head: bool = False, **kwargs: Any) -> 'IndexFile':
"""Reset the index to reflect the tree at the given commit. This will not
Expand Down
9 changes: 5 additions & 4 deletions git/objects/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

from typing import Any, TYPE_CHECKING, Optional, Union

from git.types import PathLike
from git.types import PathLike, Commit_ish

if TYPE_CHECKING:
from git.repo import Repo
from gitdb.base import OStream
from .tree import Tree
from .blob import Blob
from .tag import TagObject
from .commit import Commit
from .submodule.base import Submodule

IndexObjUnion = Union['Tree', 'Blob', 'Submodule']

# --------------------------------------------------------------------------

Expand Down Expand Up @@ -71,7 +72,7 @@ def new(cls, repo: 'Repo', id): # @ReservedAssignment
return repo.rev_parse(str(id))

@classmethod
def new_from_sha(cls, repo: 'Repo', sha1: bytes) -> Union['Commit', 'TagObject', 'Tree', 'Blob']:
def new_from_sha(cls, repo: 'Repo', sha1: bytes) -> Commit_ish:
"""
:return: new object instance of a type appropriate to represent the given
binary sha1
Expand Down
46 changes: 31 additions & 15 deletions git/objects/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
#
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php

from gitdb import IStream
from git.util import (
hex_to_bin,
Actor,
IterableObj,
Stats,
finalize_process
)
Expand All @@ -17,8 +15,8 @@
from .tree import Tree
from . import base
from .util import (
Traversable,
Serializable,
TraversableIterableObj,
parse_date,
altz_to_utctz_str,
parse_actor_and_date,
Expand All @@ -36,18 +34,25 @@
from io import BytesIO
import logging

from typing import List, Tuple, Union, TYPE_CHECKING

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

from typing import Any, Iterator, List, Sequence, Tuple, Union, TYPE_CHECKING

from git.types import PathLike

if TYPE_CHECKING:
from git.repo import Repo

# ------------------------------------------------------------------------

log = logging.getLogger('git.objects.commit')
log.addHandler(logging.NullHandler())

__all__ = ('Commit', )


class Commit(base.Object, IterableObj, Diffable, Traversable, Serializable):
class Commit(base.Object, TraversableIterableObj, Diffable, Serializable):

"""Wraps a git Commit object.

Expand All @@ -73,7 +78,8 @@ class Commit(base.Object, IterableObj, Diffable, Traversable, Serializable):
"message", "parents", "encoding", "gpgsig")
_id_attribute_ = "hexsha"

def __init__(self, repo, binsha, tree=None, author=None, authored_date=None, author_tz_offset=None,
def __init__(self, repo, binsha, tree=None, author: Union[Actor, None] = None,
authored_date=None, author_tz_offset=None,
committer=None, committed_date=None, committer_tz_offset=None,
message=None, parents: Union[Tuple['Commit', ...], List['Commit'], None] = None,
encoding=None, gpgsig=None):
Expand Down Expand Up @@ -139,7 +145,7 @@ def __init__(self, repo, binsha, tree=None, author=None, authored_date=None, aut
self.gpgsig = gpgsig

@classmethod
def _get_intermediate_items(cls, commit: 'Commit') -> Tuple['Commit', ...]: # type: ignore ## cos overriding super
def _get_intermediate_items(cls, commit: 'Commit') -> Tuple['Commit', ...]:
return tuple(commit.parents)

@classmethod
Expand Down Expand Up @@ -225,7 +231,9 @@ def name_rev(self):
return self.repo.git.name_rev(self)

@classmethod
def iter_items(cls, repo, rev, paths='', **kwargs):
def iter_items(cls, repo: 'Repo', rev, # type: ignore
paths: Union[PathLike, Sequence[PathLike]] = '', **kwargs: Any
) -> Iterator['Commit']:
"""Find all commits matching the given criteria.

:param repo: is the Repo
Expand All @@ -245,15 +253,23 @@ def iter_items(cls, repo, rev, paths='', **kwargs):

# use -- in any case, to prevent possibility of ambiguous arguments
# see https://github.com/gitpython-developers/GitPython/issues/264
args = ['--']

args_list: List[Union[PathLike, Sequence[PathLike]]] = ['--']

if paths:
args.extend((paths, ))
paths_tup: Tuple[PathLike, ...]
if isinstance(paths, (str, os.PathLike)):
paths_tup = (paths, )
else:
paths_tup = tuple(paths)

args_list.extend(paths_tup)
# END if paths

proc = repo.git.rev_list(rev, args, as_process=True, **kwargs)
proc = repo.git.rev_list(rev, args_list, as_process=True, **kwargs)
return cls._iter_from_process_or_stream(repo, proc)

def iter_parents(self, paths='', **kwargs):
def iter_parents(self, paths: Union[PathLike, Sequence[PathLike]] = '', **kwargs) -> Iterator['Commit']:
"""Iterate _all_ parents of this commit.

:param paths:
Expand All @@ -269,7 +285,7 @@ def iter_parents(self, paths='', **kwargs):

return self.iter_items(self.repo, self, paths, **kwargs)

@property
@ property
def stats(self):
"""Create a git stat from changes between this commit and its first parent
or from all changes done if this is the very first commit.
Expand All @@ -286,7 +302,7 @@ def stats(self):
text = self.repo.git.diff(self.parents[0].hexsha, self.hexsha, '--', numstat=True)
return Stats._list_from_string(self.repo, text)

@classmethod
@ classmethod
def _iter_from_process_or_stream(cls, repo, proc_or_stream):
"""Parse out commit information into a list of Commit objects
We expect one-line per commit, and parse the actual commit information directly
Expand Down Expand Up @@ -317,7 +333,7 @@ def _iter_from_process_or_stream(cls, repo, proc_or_stream):
if hasattr(proc_or_stream, 'wait'):
finalize_process(proc_or_stream)

@classmethod
@ classmethod
def create_from_tree(cls, repo, tree, message, parent_commits=None, head=False, author=None, committer=None,
author_date=None, commit_date=None):
"""Commit the given tree, creating a commit object.
Expand Down
Loading