diff options
Diffstat (limited to 'git')
-rw-r--r-- | git/diff.py | 28 | ||||
-rw-r--r-- | git/index/util.py | 8 |
2 files changed, 20 insertions, 16 deletions
diff --git a/git/diff.py b/git/diff.py index 6c34a871..d3b18652 100644 --- a/git/diff.py +++ b/git/diff.py @@ -15,12 +15,13 @@ from .objects.util import mode_str_to_int # typing ------------------------------------------------------------------ -from typing import Any, Iterator, List, Match, Optional, Tuple, Type, Union, TYPE_CHECKING, cast +from typing import Any, Iterator, List, Match, Optional, Tuple, Type, TypeVar, Union, TYPE_CHECKING from git.types import PathLike, TBD, Literal, TypeGuard if TYPE_CHECKING: from .objects.tree import Tree from git.repo.base import Repo + from git.objects.base import IndexObject from subprocess import Popen @@ -175,7 +176,10 @@ class Diffable(object): return index -class DiffIndex(list): +T_Diff = TypeVar('T_Diff', bound='Diff') + + +class DiffIndex(List[T_Diff]): """Implements an Index for diffs, allowing a list of Diffs to be queried by the diff properties. @@ -189,7 +193,7 @@ class DiffIndex(list): # T = Changed in the type change_type = ("A", "C", "D", "R", "M", "T") - def iter_change_type(self, change_type: Lit_change_type) -> Iterator['Diff']: + def iter_change_type(self, change_type: Lit_change_type) -> Iterator[T_Diff]: """ :return: iterator yielding Diff instances that match the given change_type @@ -207,8 +211,6 @@ class DiffIndex(list): raise ValueError("Invalid change type: %s" % change_type) for diff in self: - diff = cast('Diff', diff) - if diff.change_type == change_type: yield diff elif change_type == "A" and diff.new_file: @@ -289,7 +291,7 @@ class Diff(object): a_mode: Union[bytes, str, None], b_mode: Union[bytes, str, None], new_file: bool, deleted_file: bool, copied_file: bool, raw_rename_from: Optional[bytes], raw_rename_to: Optional[bytes], - diff: Union[str, bytes, None], change_type: Optional[str], score: Optional[int]) -> None: + diff: Union[str, bytes, None], change_type: Optional[Lit_change_type], score: Optional[int]) -> None: assert a_rawpath is None or isinstance(a_rawpath, bytes) assert b_rawpath is None or isinstance(b_rawpath, bytes) @@ -308,19 +310,21 @@ class Diff(object): repo = submodule.module() break + self.a_blob: Union['IndexObject', None] if a_blob_id is None or a_blob_id == self.NULL_HEX_SHA: self.a_blob = None else: self.a_blob = Blob(repo, hex_to_bin(a_blob_id), mode=self.a_mode, path=self.a_path) + self.b_blob: Union['IndexObject', None] if b_blob_id is None or b_blob_id == self.NULL_HEX_SHA: self.b_blob = None else: self.b_blob = Blob(repo, hex_to_bin(b_blob_id), mode=self.b_mode, path=self.b_path) - self.new_file = new_file - self.deleted_file = deleted_file - self.copied_file = copied_file + self.new_file: bool = new_file + self.deleted_file: bool = deleted_file + self.copied_file: bool = copied_file # be clear and use None instead of empty strings assert raw_rename_from is None or isinstance(raw_rename_from, bytes) @@ -329,7 +333,7 @@ class Diff(object): self.raw_rename_to = raw_rename_to or None self.diff = diff - self.change_type = change_type + self.change_type: Union[Lit_change_type, None] = change_type self.score = score def __eq__(self, other: object) -> bool: @@ -449,7 +453,7 @@ class Diff(object): # for now, we have to bake the stream text = b''.join(text_list) - index = DiffIndex() + index: 'DiffIndex' = DiffIndex() previous_header = None header = None a_path, b_path = None, None # for mypy @@ -560,7 +564,7 @@ class Diff(object): # handles # :100644 100644 687099101... 37c5e30c8... M .gitignore - index = DiffIndex() + index: 'DiffIndex' = DiffIndex() handle_process_output(proc, lambda byt: cls._handle_diff_line(byt, repo, index), None, finalize_process, decode_streams=False) diff --git a/git/index/util.py b/git/index/util.py index e0daef0c..3b3d6489 100644 --- a/git/index/util.py +++ b/git/index/util.py @@ -52,7 +52,7 @@ class TemporaryFileSwap(object): #{ Decorators -def post_clear_cache(func: Callable[..., Any]) -> Callable[..., Any]: +def post_clear_cache(func: Callable[..., _T]) -> Callable[..., _T]: """Decorator for functions that alter the index using the git command. This would invalidate our possibly existing entries dictionary which is why it must be deleted to allow it to be lazily reread later. @@ -63,7 +63,7 @@ def post_clear_cache(func: Callable[..., Any]) -> Callable[..., Any]: """ @wraps(func) - def post_clear_cache_if_not_raised(self, *args: Any, **kwargs: Any) -> Any: + def post_clear_cache_if_not_raised(self, *args: Any, **kwargs: Any) -> _T: rval = func(self, *args, **kwargs) self._delete_entries_cache() return rval @@ -72,13 +72,13 @@ def post_clear_cache(func: Callable[..., Any]) -> Callable[..., Any]: return post_clear_cache_if_not_raised -def default_index(func: Callable[..., Any]) -> Callable[..., Any]: +def default_index(func: Callable[..., _T]) -> Callable[..., _T]: """Decorator assuring the wrapped method may only run if we are the default repository index. This is as we rely on git commands that operate on that index only. """ @wraps(func) - def check_default_index(self, *args: Any, **kwargs: Any) -> Any: + def check_default_index(self, *args: Any, **kwargs: Any) -> _T: if self._file_path != self._index_path(): raise AssertionError( "Cannot call %r on indices that do not represent the default git index" % func.__name__) |