From 2e2fe186d09272c3cb6c96467fff362deb90994f Mon Sep 17 00:00:00 2001 From: Yobmod Date: Thu, 8 Jul 2021 11:30:16 +0100 Subject: Increase mypy strictness (no_implicit_optional & warn_redundant_casts) and fix errors --- git/objects/tree.py | 1 - 1 file changed, 1 deletion(-) (limited to 'git/objects/tree.py') diff --git a/git/objects/tree.py b/git/objects/tree.py index 2e8d8a79..34fb93dc 100644 --- a/git/objects/tree.py +++ b/git/objects/tree.py @@ -216,7 +216,6 @@ class Tree(IndexObject, diff.Diffable, util.Traversable, util.Serializable): def _get_intermediate_items(cls, index_object: 'Tree', ) -> Union[Tuple['Tree', ...], Tuple[()]]: if index_object.type == "tree": - index_object = cast('Tree', index_object) return tuple(index_object._iter_convert_to_object(index_object._cache)) return () -- cgit v1.2.1 From 5d3818ed3d51d400517a352b5b62e966164af8cf Mon Sep 17 00:00:00 2001 From: Yobmod Date: Thu, 8 Jul 2021 21:42:30 +0100 Subject: Finish initial typing of index folder --- git/objects/tree.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'git/objects/tree.py') diff --git a/git/objects/tree.py b/git/objects/tree.py index 34fb93dc..528cf5ca 100644 --- a/git/objects/tree.py +++ b/git/objects/tree.py @@ -21,8 +21,8 @@ from .fun import ( # typing ------------------------------------------------- -from typing import (Callable, Dict, Generic, Iterable, Iterator, List, - Tuple, Type, TypeVar, Union, cast, TYPE_CHECKING) +from typing import (Callable, Dict, Iterable, Iterator, List, + Tuple, Type, Union, cast, TYPE_CHECKING) from git.types import PathLike, TypeGuard @@ -30,7 +30,7 @@ if TYPE_CHECKING: from git.repo import Repo from io import BytesIO -T_Tree_cache = TypeVar('T_Tree_cache', bound=Tuple[bytes, int, str]) +TreeCacheTup = Tuple[bytes, int, str] TraversedTreeTup = Union[Tuple[Union['Tree', None], IndexObjUnion, Tuple['Submodule', 'Submodule']]] @@ -42,7 +42,7 @@ cmp: Callable[[str, str], int] = lambda a, b: (a > b) - (a < b) __all__ = ("TreeModifier", "Tree") -def git_cmp(t1: T_Tree_cache, t2: T_Tree_cache) -> int: +def git_cmp(t1: TreeCacheTup, t2: TreeCacheTup) -> int: a, b = t1[2], t2[2] assert isinstance(a, str) and isinstance(b, str) # Need as mypy 9.0 cannot unpack TypeVar properly len_a, len_b = len(a), len(b) @@ -55,8 +55,8 @@ def git_cmp(t1: T_Tree_cache, t2: T_Tree_cache) -> int: return len_a - len_b -def merge_sort(a: List[T_Tree_cache], - cmp: Callable[[T_Tree_cache, T_Tree_cache], int]) -> None: +def merge_sort(a: List[TreeCacheTup], + cmp: Callable[[TreeCacheTup, TreeCacheTup], int]) -> None: if len(a) < 2: return None @@ -91,7 +91,7 @@ def merge_sort(a: List[T_Tree_cache], k = k + 1 -class TreeModifier(Generic[T_Tree_cache], object): +class TreeModifier(object): """A utility class providing methods to alter the underlying cache in a list-like fashion. @@ -99,7 +99,7 @@ class TreeModifier(Generic[T_Tree_cache], object): the cache of a tree, will be sorted. Assuring it will be in a serializable state""" __slots__ = '_cache' - def __init__(self, cache: List[T_Tree_cache]) -> None: + def __init__(self, cache: List[TreeCacheTup]) -> None: self._cache = cache def _index_by_name(self, name: str) -> int: @@ -141,7 +141,7 @@ class TreeModifier(Generic[T_Tree_cache], object): sha = to_bin_sha(sha) index = self._index_by_name(name) - def is_tree_cache(inp: Tuple[bytes, int, str]) -> TypeGuard[T_Tree_cache]: + def is_tree_cache(inp: Tuple[bytes, int, str]) -> TypeGuard[TreeCacheTup]: return isinstance(inp[0], bytes) and isinstance(inp[1], int) and isinstance([inp], str) item = (sha, mode, name) @@ -167,7 +167,7 @@ class TreeModifier(Generic[T_Tree_cache], object): For more information on the parameters, see ``add`` :param binsha: 20 byte binary sha""" assert isinstance(binsha, bytes) and isinstance(mode, int) and isinstance(name, str) - tree_cache = cast(T_Tree_cache, (binsha, mode, name)) + tree_cache = (binsha, mode, name) self._cache.append(tree_cache) -- cgit v1.2.1 From 4f13b4e23526616f307370dc9a869b067e90b276 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Thu, 8 Jul 2021 23:49:01 +0100 Subject: fix base,ours,theirs typing --- git/objects/tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git/objects/tree.py') diff --git a/git/objects/tree.py b/git/objects/tree.py index 528cf5ca..d3681e23 100644 --- a/git/objects/tree.py +++ b/git/objects/tree.py @@ -44,7 +44,7 @@ __all__ = ("TreeModifier", "Tree") def git_cmp(t1: TreeCacheTup, t2: TreeCacheTup) -> int: a, b = t1[2], t2[2] - assert isinstance(a, str) and isinstance(b, str) # Need as mypy 9.0 cannot unpack TypeVar properly + # assert isinstance(a, str) and isinstance(b, str) len_a, len_b = len(a), len(b) min_len = min(len_a, len_b) min_cmp = cmp(a[:min_len], b[:min_len]) -- cgit v1.2.1 From 030b1fded8b8e1bcf3855beaf9035b4e3e755f5c Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 10:23:14 +0100 Subject: Add list_traverse() to Tree and TraversableIterableObj. --- git/objects/tree.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'git/objects/tree.py') diff --git a/git/objects/tree.py b/git/objects/tree.py index d3681e23..804554d8 100644 --- a/git/objects/tree.py +++ b/git/objects/tree.py @@ -4,7 +4,7 @@ # This module is part of GitPython and is released under # the BSD License: http://www.opensource.org/licenses/bsd-license.php -from git.util import join_path +from git.util import IterableList, join_path import git.diff as diff from git.util import to_bin_sha @@ -21,7 +21,7 @@ from .fun import ( # typing ------------------------------------------------- -from typing import (Callable, Dict, Iterable, Iterator, List, +from typing import (Any, Callable, Dict, Iterable, Iterator, List, Tuple, Type, Union, cast, TYPE_CHECKING) from git.types import PathLike, TypeGuard @@ -323,6 +323,9 @@ class Tree(IndexObject, diff.Diffable, util.Traversable, util.Serializable): super(Tree, self).traverse(predicate, prune, depth, # type: ignore branch_first, visit_once, ignore_self)) + def list_traverse(self, *args: Any, **kwargs: Any) -> IterableList[Union['Tree', 'Submodule', 'Blob']]: + return super(Tree, self).list_traverse(* args, **kwargs) + # List protocol def __getslice__(self, i: int, j: int) -> List[IndexObjUnion]: -- cgit v1.2.1 From 3710e24d6b60213454af10b0dc0ff0c49717169f Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 10:33:12 +0100 Subject: Rmv circular import, create Has_id_attribute Protocol instead --- git/objects/tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git/objects/tree.py') diff --git a/git/objects/tree.py b/git/objects/tree.py index 804554d8..e168c6c4 100644 --- a/git/objects/tree.py +++ b/git/objects/tree.py @@ -323,7 +323,7 @@ class Tree(IndexObject, diff.Diffable, util.Traversable, util.Serializable): super(Tree, self).traverse(predicate, prune, depth, # type: ignore branch_first, visit_once, ignore_self)) - def list_traverse(self, *args: Any, **kwargs: Any) -> IterableList[Union['Tree', 'Submodule', 'Blob']]: + def list_traverse(self, *args: Any, **kwargs: Any) -> IterableList[IndexObjUnion]: return super(Tree, self).list_traverse(* args, **kwargs) # List protocol -- cgit v1.2.1 From 5eea8910b2e07d424a2e33299149d13392a80a54 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 10:37:31 +0100 Subject: Fix list_traverse() docstring for Autodoc --- git/objects/tree.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'git/objects/tree.py') diff --git a/git/objects/tree.py b/git/objects/tree.py index e168c6c4..9d221652 100644 --- a/git/objects/tree.py +++ b/git/objects/tree.py @@ -324,6 +324,11 @@ class Tree(IndexObject, diff.Diffable, util.Traversable, util.Serializable): branch_first, visit_once, ignore_self)) def list_traverse(self, *args: Any, **kwargs: Any) -> IterableList[IndexObjUnion]: + """ + :return: IterableList with the results of the traversal as produced by + traverse() + Tree -> IterableList[Union['Submodule', 'Tree', 'Blob']] + """ return super(Tree, self).list_traverse(* args, **kwargs) # List protocol -- cgit v1.2.1 From 7c6ae2b94cfd1593c12366b6abc0cd5bbb6e07b2 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 15:07:50 +0100 Subject: Try to distinguation git.diff module from diff.Diff.diff and diff.Daffable.diff() --- git/objects/tree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'git/objects/tree.py') diff --git a/git/objects/tree.py b/git/objects/tree.py index 9d221652..4c2a02bf 100644 --- a/git/objects/tree.py +++ b/git/objects/tree.py @@ -5,7 +5,7 @@ # the BSD License: http://www.opensource.org/licenses/bsd-license.php from git.util import IterableList, join_path -import git.diff as diff +import git.diff as git_diff from git.util import to_bin_sha from . import util @@ -180,7 +180,7 @@ class TreeModifier(object): #} END mutators -class Tree(IndexObject, diff.Diffable, util.Traversable, util.Serializable): +class Tree(IndexObject, git_diff.Diffable, util.Traversable, util.Serializable): """Tree objects represent an ordered list of Blobs and other Trees. -- cgit v1.2.1 From e6a27adb71d21c81628acbdd65bf07037604ff90 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 23:33:53 +0100 Subject: Use TreeCacheTup type alias throughout --- git/objects/tree.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'git/objects/tree.py') diff --git a/git/objects/tree.py b/git/objects/tree.py index 4c2a02bf..a9656c1d 100644 --- a/git/objects/tree.py +++ b/git/objects/tree.py @@ -31,9 +31,14 @@ if TYPE_CHECKING: from io import BytesIO TreeCacheTup = Tuple[bytes, int, str] + TraversedTreeTup = Union[Tuple[Union['Tree', None], IndexObjUnion, Tuple['Submodule', 'Submodule']]] + +def is_tree_cache(inp: Tuple[bytes, int, str]) -> TypeGuard[TreeCacheTup]: + return isinstance(inp[0], bytes) and isinstance(inp[1], int) and isinstance([inp], str) + #-------------------------------------------------------- @@ -141,11 +146,8 @@ class TreeModifier(object): sha = to_bin_sha(sha) index = self._index_by_name(name) - def is_tree_cache(inp: Tuple[bytes, int, str]) -> TypeGuard[TreeCacheTup]: - return isinstance(inp[0], bytes) and isinstance(inp[1], int) and isinstance([inp], str) - item = (sha, mode, name) - assert is_tree_cache(item) + # assert is_tree_cache(item) if index == -1: self._cache.append(item) @@ -223,12 +225,12 @@ class Tree(IndexObject, git_diff.Diffable, util.Traversable, util.Serializable): if attr == "_cache": # Set the data when we need it ostream = self.repo.odb.stream(self.binsha) - self._cache: List[Tuple[bytes, int, str]] = tree_entries_from_data(ostream.read()) + self._cache: List[TreeCacheTup] = tree_entries_from_data(ostream.read()) else: super(Tree, self)._set_cache_(attr) # END handle attribute - def _iter_convert_to_object(self, iterable: Iterable[Tuple[bytes, int, str]] + def _iter_convert_to_object(self, iterable: Iterable[TreeCacheTup] ) -> Iterator[IndexObjUnion]: """Iterable yields tuples of (binsha, mode, name), which will be converted to the respective object representation""" -- cgit v1.2.1