| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,12 +15,13 @@ | |||
| 15 | 15 | ||
| 16 | 16 | # typing ------------------------------------------------------------------ | |
| 17 | 17 | ||
| 18 | - from typing import Any, Iterator, List, Match, Optional, Tuple, Type, Union, TYPE_CHECKING, cast | ||
| 18 | + from typing import Any, Iterator, List, Match, Optional, Tuple, Type, TypeVar, Union, TYPE_CHECKING | ||
| 19 | 19 | from git.types import PathLike, TBD, Literal, TypeGuard | |
| 20 | 20 | ||
| 21 | 21 | if TYPE_CHECKING: | |
| 22 | 22 | from .objects.tree import Tree | |
| 23 | 23 | from git.repo.base import Repo | |
| 24 | + from git.objects.base import IndexObject | ||
| 24 | 25 | ||
| 25 | 26 | from subprocess import Popen | |
| 26 | 27 | ||
@@ -175,7 +176,10 @@ def diff(self, other: Union[Type[Index], Type['Tree'], object, None, str] = Inde | |||
| 175 | 176 | return index | |
| 176 | 177 | ||
| 177 | 178 | ||
| 178 | - class DiffIndex(list): | ||
| 179 | + T_Diff = TypeVar('T_Diff', bound='Diff') | ||
| 180 | + | ||
| 181 | + | ||
| 182 | + class DiffIndex(List[T_Diff]): | ||
| 179 | 183 | ||
| 180 | 184 | """Implements an Index for diffs, allowing a list of Diffs to be queried by | |
| 181 | 185 | the diff properties. | |
@@ -189,7 +193,7 @@ class DiffIndex(list): | |||
| 189 | 193 | # T = Changed in the type | |
| 190 | 194 | change_type = ("A", "C", "D", "R", "M", "T") | |
| 191 | 195 | ||
| 192 | - def iter_change_type(self, change_type: Lit_change_type) -> Iterator['Diff']: | ||
| 196 | + def iter_change_type(self, change_type: Lit_change_type) -> Iterator[T_Diff]: | ||
| 193 | 197 | """ | |
| 194 | 198 | :return: | |
| 195 | 199 | iterator yielding Diff instances that match the given change_type | |
@@ -207,8 +211,6 @@ def iter_change_type(self, change_type: Lit_change_type) -> Iterator['Diff']: | |||
| 207 | 211 | raise ValueError("Invalid change type: %s" % change_type) | |
| 208 | 212 | ||
| 209 | 213 | for diff in self: | |
| 210 | - diff = cast('Diff', diff) | ||
| 211 | - | ||
| 212 | 214 | if diff.change_type == change_type: | |
| 213 | 215 | yield diff | |
| 214 | 216 | elif change_type == "A" and diff.new_file: | |
@@ -289,7 +291,7 @@ def __init__(self, repo: 'Repo', | |||
| 289 | 291 | a_mode: Union[bytes, str, None], b_mode: Union[bytes, str, None], | |
| 290 | 292 | new_file: bool, deleted_file: bool, copied_file: bool, | |
| 291 | 293 | raw_rename_from: Optional[bytes], raw_rename_to: Optional[bytes], | |
| 292 | - diff: Union[str, bytes, None], change_type: Optional[str], score: Optional[int]) -> None: | ||
| 294 | + diff: Union[str, bytes, None], change_type: Optional[Lit_change_type], score: Optional[int]) -> None: | ||
| 293 | 295 | ||
| 294 | 296 | assert a_rawpath is None or isinstance(a_rawpath, bytes) | |
| 295 | 297 | assert b_rawpath is None or isinstance(b_rawpath, bytes) | |
@@ -308,19 +310,21 @@ def __init__(self, repo: 'Repo', | |||
| 308 | 310 | repo = submodule.module() | |
| 309 | 311 | break | |
| 310 | 312 | ||
| 313 | + self.a_blob: Union['IndexObject', None] | ||
| 311 | 314 | if a_blob_id is None or a_blob_id == self.NULL_HEX_SHA: | |
| 312 | 315 | self.a_blob = None | |
| 313 | 316 | else: | |
| 314 | 317 | self.a_blob = Blob(repo, hex_to_bin(a_blob_id), mode=self.a_mode, path=self.a_path) | |
| 315 | 318 | ||
| 319 | + self.b_blob: Union['IndexObject', None] | ||
| 316 | 320 | if b_blob_id is None or b_blob_id == self.NULL_HEX_SHA: | |
| 317 | 321 | self.b_blob = None | |
| 318 | 322 | else: | |
| 319 | 323 | self.b_blob = Blob(repo, hex_to_bin(b_blob_id), mode=self.b_mode, path=self.b_path) | |
| 320 | 324 | ||
| 321 | - self.new_file = new_file | ||
| 322 | - self.deleted_file = deleted_file | ||
| 323 | - self.copied_file = copied_file | ||
| 325 | + self.new_file: bool = new_file | ||
| 326 | + self.deleted_file: bool = deleted_file | ||
| 327 | + self.copied_file: bool = copied_file | ||
| 324 | 328 | ||
| 325 | 329 | # be clear and use None instead of empty strings | |
| 326 | 330 | assert raw_rename_from is None or isinstance(raw_rename_from, bytes) | |
@@ -329,7 +333,7 @@ def __init__(self, repo: 'Repo', | |||
| 329 | 333 | self.raw_rename_to = raw_rename_to or None | |
| 330 | 334 | ||
| 331 | 335 | self.diff = diff | |
| 332 | - self.change_type = change_type | ||
| 336 | + self.change_type: Union[Lit_change_type, None] = change_type | ||
| 333 | 337 | self.score = score | |
| 334 | 338 | ||
| 335 | 339 | def __eq__(self, other: object) -> bool: | |
@@ -449,7 +453,7 @@ def _index_from_patch_format(cls, repo: 'Repo', proc: TBD) -> DiffIndex: | |||
| 449 | 453 | ||
| 450 | 454 | # for now, we have to bake the stream | |
| 451 | 455 | text = b''.join(text_list) | |
| 452 | - index = DiffIndex() | ||
| 456 | + index: 'DiffIndex' = DiffIndex() | ||
| 453 | 457 | previous_header = None | |
| 454 | 458 | header = None | |
| 455 | 459 | a_path, b_path = None, None # for mypy | |
@@ -560,7 +564,7 @@ def _index_from_raw_format(cls, repo: 'Repo', proc: 'Popen') -> 'DiffIndex': | |||
| 560 | 564 | # handles | |
| 561 | 565 | # :100644 100644 687099101... 37c5e30c8... M .gitignore | |
| 562 | 566 | ||
| 563 | - index = DiffIndex() | ||
| 567 | + index: 'DiffIndex' = DiffIndex() | ||
| 564 | 568 | handle_process_output(proc, lambda byt: cls._handle_diff_line(byt, repo, index), | |
| 565 | 569 | None, finalize_process, decode_streams=False) | |
| 566 | 570 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -52,7 +52,7 @@ def __del__(self) -> None: | |||
| 52 | 52 | ||
| 53 | 53 | #{ Decorators | |
| 54 | 54 | ||
| 55 | - def post_clear_cache(func: Callable[..., Any]) -> Callable[..., Any]: | ||
| 55 | + def post_clear_cache(func: Callable[..., _T]) -> Callable[..., _T]: | ||
| 56 | 56 | """Decorator for functions that alter the index using the git command. This would | |
| 57 | 57 | invalidate our possibly existing entries dictionary which is why it must be | |
| 58 | 58 | deleted to allow it to be lazily reread later. | |
@@ -63,7 +63,7 @@ def post_clear_cache(func: Callable[..., Any]) -> Callable[..., Any]: | |||
| 63 | 63 | """ | |
| 64 | 64 | ||
| 65 | 65 | @wraps(func) | |
| 66 | - def post_clear_cache_if_not_raised(self, *args: Any, **kwargs: Any) -> Any: | ||
| 66 | + def post_clear_cache_if_not_raised(self, *args: Any, **kwargs: Any) -> _T: | ||
| 67 | 67 | rval = func(self, *args, **kwargs) | |
| 68 | 68 | self._delete_entries_cache() | |
| 69 | 69 | return rval | |
@@ -72,13 +72,13 @@ def post_clear_cache_if_not_raised(self, *args: Any, **kwargs: Any) -> Any: | |||
| 72 | 72 | return post_clear_cache_if_not_raised | |
| 73 | 73 | ||
| 74 | 74 | ||
| 75 | - def default_index(func: Callable[..., Any]) -> Callable[..., Any]: | ||
| 75 | + def default_index(func: Callable[..., _T]) -> Callable[..., _T]: | ||
| 76 | 76 | """Decorator assuring the wrapped method may only run if we are the default | |
| 77 | 77 | repository index. This is as we rely on git commands that operate | |
| 78 | 78 | on that index only. """ | |
| 79 | 79 | ||
| 80 | 80 | @wraps(func) | |
| 81 | - def check_default_index(self, *args: Any, **kwargs: Any) -> Any: | ||
| 81 | + def check_default_index(self, *args: Any, **kwargs: Any) -> _T: | ||
| 82 | 82 | if self._file_path != self._index_path(): | |
| 83 | 83 | raise AssertionError( | |
| 84 | 84 | "Cannot call %r on indices that do not represent the default git index" % func.__name__) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments