summaryrefslogtreecommitdiff
path: root/git/util.py
diff options
context:
space:
mode:
authorYobmod <yobmod@gmail.com>2021-06-30 18:41:06 +0100
committerYobmod <yobmod@gmail.com>2021-06-30 18:41:06 +0100
commit82b131cf2afebbed3723df5b5dfd5cd820716f97 (patch)
treeef007c76db64b1f0cbfc99941a9a4de7d3fd3b8a /git/util.py
parent75dbf90efb5e292bac5f54700f7f0efedf3e47d5 (diff)
downloadgitpython-82b131cf2afebbed3723df5b5dfd5cd820716f97.tar.gz
Type Traversable.traverse() better, start types of submodule
Diffstat (limited to 'git/util.py')
-rw-r--r--git/util.py39
1 files changed, 21 insertions, 18 deletions
diff --git a/git/util.py b/git/util.py
index eccaa74e..c7c8d07f 100644
--- a/git/util.py
+++ b/git/util.py
@@ -4,6 +4,9 @@
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
+from .exc import InvalidGitRepositoryError
+import os.path as osp
+from .compat import is_win
import contextlib
from functools import wraps
import getpass
@@ -20,9 +23,11 @@ from unittest import SkipTest
from urllib.parse import urlsplit, urlunsplit
import warnings
+# from git.objects.util import Traversable
+
# typing ---------------------------------------------------------
-from typing import (Any, AnyStr, BinaryIO, Callable, Dict, Generator, IO, Iterator, List,
+from typing import (Any, AnyStr, BinaryIO, Callable, Dict, Generator, IO, Iterable as typIter, Iterator, List,
Optional, Pattern, Sequence, Tuple, TypeVar, Union, cast, TYPE_CHECKING, overload)
import pathlib
@@ -32,8 +37,11 @@ if TYPE_CHECKING:
from git.repo.base import Repo
from git.config import GitConfigParser, SectionConstraint
-from .types import PathLike, Literal, SupportsIndex, HSH_TD, Files_TD
+from .types import PathLike, Literal, SupportsIndex, HSH_TD, Total_TD, Files_TD
+
+T_IterableObj = TypeVar('T_IterableObj', bound=Union['IterableObj', typIter], covariant=True)
+# So IterableList[Head] is subtype of IterableList[IterableObj]
# ---------------------------------------------------------------------
@@ -49,11 +57,6 @@ from gitdb.util import ( # NOQA @IgnorePep8
hex_to_bin, # @UnusedImport
)
-from .compat import is_win
-import os.path as osp
-
-from .exc import InvalidGitRepositoryError
-
# NOTE: Some of the unused imports might be used/imported by others.
# Handle once test-cases are back up and running.
@@ -181,6 +184,7 @@ else:
# no need for any work on linux
def to_native_path_linux(path: PathLike) -> PathLike:
return path
+
to_native_path = to_native_path_linux
@@ -433,7 +437,7 @@ class RemoteProgress(object):
Handler providing an interface to parse progress information emitted by git-push
and git-fetch and to dispatch callbacks allowing subclasses to react to the progress.
"""
- _num_op_codes = 9
+ _num_op_codes: int = 9
BEGIN, END, COUNTING, COMPRESSING, WRITING, RECEIVING, RESOLVING, FINDING_SOURCES, CHECKING_OUT = \
[1 << x for x in range(_num_op_codes)]
STAGE_MASK = BEGIN | END
@@ -746,8 +750,6 @@ class Stats(object):
files = number of changed files as int"""
__slots__ = ("total", "files")
- from git.types import Total_TD, Files_TD
-
def __init__(self, total: Total_TD, files: Dict[PathLike, Files_TD]):
self.total = total
self.files = files
@@ -931,10 +933,7 @@ class BlockingLockFile(LockFile):
# END endless loop
-T = TypeVar('T', bound='IterableObj')
-
-
-class IterableList(List[T]):
+class IterableList(List[T_IterableObj]):
"""
List of iterable objects allowing to query an object by id or by named index::
@@ -1046,7 +1045,7 @@ class Iterable(object):
@classmethod
def list_items(cls, repo, *args, **kwargs):
"""
- Deprecaated, use IterableObj instead.
+ Deprecated, use IterableObj instead.
Find all items of this type - subclasses can specify args and kwargs differently.
If no args are given, subclasses are obliged to return all items if no additional
arguments arg given.
@@ -1068,12 +1067,15 @@ class Iterable(object):
class IterableObj():
"""Defines an interface for iterable items which is to assure a uniform
- way to retrieve and iterate items within the git repository"""
+ way to retrieve and iterate items within the git repository
+
+ Subclasses = [Submodule, Commit, Reference, PushInfo, FetchInfo, Remote]"""
+
__slots__ = ()
_id_attribute_ = "attribute that most suitably identifies your instance"
@classmethod
- def list_items(cls, repo: 'Repo', *args: Any, **kwargs: Any) -> IterableList[T]:
+ def list_items(cls, repo: 'Repo', *args: Any, **kwargs: Any) -> IterableList[T_IterableObj]:
"""
Find all items of this type - subclasses can specify args and kwargs differently.
If no args are given, subclasses are obliged to return all items if no additional
@@ -1087,7 +1089,8 @@ class IterableObj():
return out_list
@classmethod
- def iter_items(cls, repo: 'Repo', *args: Any, **kwargs: Any) -> Iterator[T]:
+ def iter_items(cls, repo: 'Repo', *args: Any, **kwargs: Any
+ ) -> Iterator[T_IterableObj]:
# return typed to be compatible with subtypes e.g. Remote
"""For more information about the arguments, see list_items
:return: iterator yielding Items"""