From f271c58adb36550a02607811e97cc19feae4bafb Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 00:00:19 +0100 Subject: tests TraversableIterableObj typeguard --- git/objects/util.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'git/objects/util.py') diff --git a/git/objects/util.py b/git/objects/util.py index 0b449b7b..5de9c3e9 100644 --- a/git/objects/util.py +++ b/git/objects/util.py @@ -33,7 +33,7 @@ if TYPE_CHECKING: from .tree import Tree, TraversedTreeTup from subprocess import Popen - + T_TIobj = TypeVar('T_TIobj', bound='TraversableIterableObj') # for TraversableIterableObj.traverse() TraversedTup = Union[Tuple[Union['Traversable', None], 'Traversable'], # for commit, submodule @@ -314,9 +314,9 @@ class Traversable(object): def is_TraversableIterableObj(inp: 'Traversable') -> TypeGuard['TraversableIterableObj']: # return isinstance(self, TraversableIterableObj) - # Can it be anythin else? - return isinstance(self, Traversable) - + # Can it be anything else? Check this + return isinstance(self, TraversableIterableObj) + assert is_TraversableIterableObj(self), f"{type(self)}" out: IterableList['TraversableIterableObj'] = IterableList(self._id_attribute_) out.extend(self.traverse(*args, **kwargs)) @@ -364,7 +364,7 @@ class Traversable(object): Submodule -> Iterator[Submodule, Tuple[Submodule, Submodule]] Tree -> Iterator[Union[Blob, Tree, Submodule, Tuple[Union[Submodule, Tree], Union[Blob, Tree, Submodule]]] - + ignore_self=True is_edge=True -> Iterator[item] ignore_self=True is_edge=False --> Iterator[item] ignore_self=False is_edge=True -> Iterator[item] | Iterator[Tuple[src, item]] -- cgit v1.2.1 From 4802a36bd0fec7e6ae03d6713ceae70de8e1783a Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 00:07:42 +0100 Subject: improve TraversableIterableObj typeguard --- git/objects/util.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'git/objects/util.py') diff --git a/git/objects/util.py b/git/objects/util.py index 5de9c3e9..5fb4c58a 100644 --- a/git/objects/util.py +++ b/git/objects/util.py @@ -317,10 +317,12 @@ class Traversable(object): # Can it be anything else? Check this return isinstance(self, TraversableIterableObj) - assert is_TraversableIterableObj(self), f"{type(self)}" - out: IterableList['TraversableIterableObj'] = IterableList(self._id_attribute_) - out.extend(self.traverse(*args, **kwargs)) - return out + if is_TraversableIterableObj(self): + out: IterableList['TraversableIterableObj'] = IterableList(self._id_attribute_) + out.extend(self.traverse(*args, **kwargs)) + return out + else: + return IterableList("") # Its a Tree, which doesnt have _id_attribute_ def traverse(self, predicate: Callable[[Union['Traversable', 'Blob', TraversedTup], int], bool] = lambda i, d: True, -- cgit v1.2.1 From 1faa25fcf828062d2c4ad35a962b4d8d3b05e855 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 00:56:20 +0100 Subject: Rmv typeguard from list_traverse(), was wrong --- git/objects/util.py | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) (limited to 'git/objects/util.py') diff --git a/git/objects/util.py b/git/objects/util.py index 5fb4c58a..7173bc7a 100644 --- a/git/objects/util.py +++ b/git/objects/util.py @@ -23,7 +23,7 @@ from datetime import datetime, timedelta, tzinfo from typing import (Any, Callable, Deque, Iterator, NamedTuple, overload, Sequence, TYPE_CHECKING, Tuple, Type, TypeVar, Union, cast) -from git.types import Literal, TypeGuard +from git.types import Literal if TYPE_CHECKING: from io import BytesIO, StringIO @@ -306,23 +306,19 @@ class Traversable(object): """ raise NotImplementedError("To be implemented in subclass") - def list_traverse(self, *args: Any, **kwargs: Any) -> IterableList['TraversableIterableObj']: + def list_traverse(self, *args: Any, **kwargs: Any) -> IterableList: """ :return: IterableList with the results of the traversal as produced by traverse() - List objects must be IterableObj and Traversable e.g. Commit, Submodule""" - - def is_TraversableIterableObj(inp: 'Traversable') -> TypeGuard['TraversableIterableObj']: - # return isinstance(self, TraversableIterableObj) - # Can it be anything else? Check this - return isinstance(self, TraversableIterableObj) + """ + if isinstance(self, TraversableIterableObj): + id = self._id_attribute_ + else: # Tree + id = "" - if is_TraversableIterableObj(self): - out: IterableList['TraversableIterableObj'] = IterableList(self._id_attribute_) - out.extend(self.traverse(*args, **kwargs)) - return out - else: - return IterableList("") # Its a Tree, which doesnt have _id_attribute_ + out: IterableList = IterableList(id) + out.extend(self.traverse(*args, **kwargs)) + return out def traverse(self, predicate: Callable[[Union['Traversable', 'Blob', TraversedTup], int], bool] = lambda i, d: True, @@ -449,7 +445,7 @@ class TraversableIterableObj(Traversable, IterableObj): TIobj_tuple = Tuple[Union[T_TIobj, None], T_TIobj] - @overload # type: ignore + @ overload # type: ignore def traverse(self: T_TIobj, predicate: Callable[[Union[T_TIobj, Tuple[Union[T_TIobj, None], T_TIobj]], int], bool], prune: Callable[[Union[T_TIobj, Tuple[Union[T_TIobj, None], T_TIobj]], int], bool], @@ -459,7 +455,7 @@ class TraversableIterableObj(Traversable, IterableObj): ) -> Iterator[T_TIobj]: ... - @overload + @ overload def traverse(self: T_TIobj, predicate: Callable[[Union[T_TIobj, Tuple[Union[T_TIobj, None], T_TIobj]], int], bool], prune: Callable[[Union[T_TIobj, Tuple[Union[T_TIobj, None], T_TIobj]], int], bool], @@ -469,7 +465,7 @@ class TraversableIterableObj(Traversable, IterableObj): ) -> Iterator[Tuple[Union[T_TIobj, None], T_TIobj]]: ... - @overload + @ overload def traverse(self: T_TIobj, predicate: Callable[[Union[T_TIobj, TIobj_tuple], int], bool], prune: Callable[[Union[T_TIobj, TIobj_tuple], int], bool], -- cgit v1.2.1 From f4cb7dbc707ea83f312aa669f3bea4dc10d3a24c Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 10:08:58 +0100 Subject: Change type of list_traverse() again. --- git/objects/util.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'git/objects/util.py') diff --git a/git/objects/util.py b/git/objects/util.py index 7173bc7a..982e7ac7 100644 --- a/git/objects/util.py +++ b/git/objects/util.py @@ -32,6 +32,7 @@ if TYPE_CHECKING: from .tag import TagObject from .tree import Tree, TraversedTreeTup from subprocess import Popen + from .submodule.base import Submodule T_TIobj = TypeVar('T_TIobj', bound='TraversableIterableObj') # for TraversableIterableObj.traverse() @@ -306,18 +307,28 @@ class Traversable(object): """ raise NotImplementedError("To be implemented in subclass") - def list_traverse(self, *args: Any, **kwargs: Any) -> IterableList: + def list_traverse(self, *args: Any, **kwargs: Any) -> IterableList[Union['Commit', 'Submodule', 'Tree', 'Blob']]: """ :return: IterableList with the results of the traversal as produced by traverse() + Commit -> IterableList['Commit'] + Submodule -> IterableList['Submodule'] + Tree -> IterableList[Union['Submodule', 'Tree', 'Blob']] """ - if isinstance(self, TraversableIterableObj): + # Commit and Submodule have id.__attribute__ as IterableObj + # Tree has id.__attribute__ inherited from IndexObject + if isinstance(self, (TraversableIterableObj, Tree)): id = self._id_attribute_ - else: # Tree - id = "" + else: + id = "" # shouldn't reach here, unless Traversable subclass created with no _id_attribute_ + # could add _id_attribute_ to Traversable, or make all Traversable also Iterable? + + out: IterableList[Union['Commit', 'Submodule', 'Tree', 'Blob']] = IterableList(id) + # overloads in subclasses (mypy does't allow typing self: subclass) + # Union[IterableList['Commit'], IterableList['Submodule'], IterableList[Union['Submodule', 'Tree', 'Blob']]] - out: IterableList = IterableList(id) - out.extend(self.traverse(*args, **kwargs)) + # NOTE: if is_edge=True, self.traverse returns a Tuple, so should be prevented or flattened? + out.extend(self.traverse(*args, **kwargs)) # type: ignore return out def traverse(self, -- 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/util.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'git/objects/util.py') diff --git a/git/objects/util.py b/git/objects/util.py index 982e7ac7..4dce0aee 100644 --- a/git/objects/util.py +++ b/git/objects/util.py @@ -19,6 +19,8 @@ import time import calendar from datetime import datetime, timedelta, tzinfo +from git.objects.base import IndexObject # just for an isinstance check + # typing ------------------------------------------------------------ from typing import (Any, Callable, Deque, Iterator, NamedTuple, overload, Sequence, TYPE_CHECKING, Tuple, Type, TypeVar, Union, cast) @@ -317,7 +319,7 @@ class Traversable(object): """ # Commit and Submodule have id.__attribute__ as IterableObj # Tree has id.__attribute__ inherited from IndexObject - if isinstance(self, (TraversableIterableObj, Tree)): + if isinstance(self, (TraversableIterableObj, IndexObject)): id = self._id_attribute_ else: id = "" # shouldn't reach here, unless Traversable subclass created with no _id_attribute_ @@ -456,6 +458,9 @@ class TraversableIterableObj(Traversable, IterableObj): TIobj_tuple = Tuple[Union[T_TIobj, None], T_TIobj] + def list_traverse(self: T_TIobj, *args: Any, **kwargs: Any) -> IterableList[T_TIobj]: # type: ignore[override] + return super(TraversableIterableObj, self).list_traverse(* args, **kwargs) + @ overload # type: ignore def traverse(self: T_TIobj, predicate: Callable[[Union[T_TIobj, Tuple[Union[T_TIobj, None], T_TIobj]], int], bool], -- 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/util.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'git/objects/util.py') diff --git a/git/objects/util.py b/git/objects/util.py index 4dce0aee..1c266563 100644 --- a/git/objects/util.py +++ b/git/objects/util.py @@ -19,13 +19,11 @@ import time import calendar from datetime import datetime, timedelta, tzinfo -from git.objects.base import IndexObject # just for an isinstance check - # typing ------------------------------------------------------------ from typing import (Any, Callable, Deque, Iterator, NamedTuple, overload, Sequence, TYPE_CHECKING, Tuple, Type, TypeVar, Union, cast) -from git.types import Literal +from git.types import Has_id_attribute, Literal if TYPE_CHECKING: from io import BytesIO, StringIO @@ -319,7 +317,7 @@ class Traversable(object): """ # Commit and Submodule have id.__attribute__ as IterableObj # Tree has id.__attribute__ inherited from IndexObject - if isinstance(self, (TraversableIterableObj, IndexObject)): + if isinstance(self, (TraversableIterableObj, Has_id_attribute)): id = self._id_attribute_ else: id = "" # shouldn't reach here, unless Traversable subclass created with no _id_attribute_ -- cgit v1.2.1 From 3c6deb002c82c852bbd044fc9af2c1ecc9611efb Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 11:56:16 +0100 Subject: Flatten list_traverse() --- git/objects/util.py | 1 + 1 file changed, 1 insertion(+) (limited to 'git/objects/util.py') diff --git a/git/objects/util.py b/git/objects/util.py index 1c266563..d3f3a622 100644 --- a/git/objects/util.py +++ b/git/objects/util.py @@ -328,6 +328,7 @@ class Traversable(object): # Union[IterableList['Commit'], IterableList['Submodule'], IterableList[Union['Submodule', 'Tree', 'Blob']]] # NOTE: if is_edge=True, self.traverse returns a Tuple, so should be prevented or flattened? + kwargs['as_edge'] = False out.extend(self.traverse(*args, **kwargs)) # type: ignore return out -- cgit v1.2.1 From a024bddd2a36c67967eda4e9f931c648924f0b19 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 14:27:40 +0100 Subject: Move TraverseNT to global, cos mypy complained on testing --- git/objects/util.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'git/objects/util.py') diff --git a/git/objects/util.py b/git/objects/util.py index d3f3a622..fbe3d9de 100644 --- a/git/objects/util.py +++ b/git/objects/util.py @@ -35,6 +35,12 @@ if TYPE_CHECKING: from .submodule.base import Submodule +class TraverseNT(NamedTuple): + depth: int + item: Union['Traversable', 'Blob'] + src: Union['Traversable', None] + + T_TIobj = TypeVar('T_TIobj', bound='TraversableIterableObj') # for TraversableIterableObj.traverse() TraversedTup = Union[Tuple[Union['Traversable', None], 'Traversable'], # for commit, submodule @@ -379,10 +385,6 @@ class Traversable(object): ignore_self=True is_edge=False --> Iterator[item] ignore_self=False is_edge=True -> Iterator[item] | Iterator[Tuple[src, item]] ignore_self=False is_edge=False -> Iterator[Tuple[src, item]]""" - class TraverseNT(NamedTuple): - depth: int - item: Union['Traversable', 'Blob'] - src: Union['Traversable', None] visited = set() stack = deque() # type: Deque[TraverseNT] -- cgit v1.2.1