From 16f0607ed29f20c09e89f2cacc0e28e982309d60 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Mon, 5 Jul 2021 12:31:00 +0100 Subject: Improve typing of config_levels, add assert_never() --- git/repo/base.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'git/repo/base.py') diff --git a/git/repo/base.py b/git/repo/base.py index d77b19c1..e60b6f6c 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -36,13 +36,15 @@ import gitdb # typing ------------------------------------------------------ -from git.types import TBD, PathLike, Lit_config_levels, Commit_ish, Tree_ish +from git.types import ConfigLevels_NT, TBD, PathLike, Lit_config_levels, Commit_ish, Tree_ish from typing import (Any, BinaryIO, Callable, Dict, Iterator, List, Mapping, Optional, Sequence, TextIO, Tuple, Type, Union, NamedTuple, cast, TYPE_CHECKING) -if TYPE_CHECKING: # only needed for types +from git.types import ConfigLevels_Tup + +if TYPE_CHECKING: from git.util import IterableList from git.refs.symbolic import SymbolicReference from git.objects import Tree @@ -55,12 +57,11 @@ log = logging.getLogger(__name__) __all__ = ('Repo',) -BlameEntry = NamedTuple('BlameEntry', [ - ('commit', Dict[str, TBD]), - ('linenos', range), - ('orig_path', Optional[str]), - ('orig_linenos', range)] -) +class BlameEntry(NamedTuple): + commit: Dict[str, TBD] + linenos: range + orig_path: Optional[str] + orig_linenos: range class Repo(object): @@ -95,7 +96,7 @@ class Repo(object): # invariants # represents the configuration level of a configuration file - config_level = ("system", "user", "global", "repository") # type: Tuple[Lit_config_levels, ...] + config_level: ConfigLevels_Tup = ConfigLevels_NT("system", "user", "global", "repository") # Subclass configuration # Subclasses may easily bring in their own custom types by placing a constructor or type here -- cgit v1.2.1 From 41e9781b640983cd3f38223e5b349eb299a0e4f6 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Mon, 5 Jul 2021 14:32:57 +0100 Subject: Improve BlameEntry.commit typing --- git/repo/base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'git/repo/base.py') diff --git a/git/repo/base.py b/git/repo/base.py index e60b6f6c..e1b1fc76 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -36,7 +36,7 @@ import gitdb # typing ------------------------------------------------------ -from git.types import ConfigLevels_NT, TBD, PathLike, Lit_config_levels, Commit_ish, Tree_ish +from git.types import TBD, PathLike, Lit_config_levels, Commit_ish, Tree_ish from typing import (Any, BinaryIO, Callable, Dict, Iterator, List, Mapping, Optional, Sequence, TextIO, Tuple, Type, Union, @@ -58,7 +58,7 @@ __all__ = ('Repo',) class BlameEntry(NamedTuple): - commit: Dict[str, TBD] + commit: Dict[str, 'Commit'] linenos: range orig_path: Optional[str] orig_linenos: range @@ -96,7 +96,7 @@ class Repo(object): # invariants # represents the configuration level of a configuration file - config_level: ConfigLevels_Tup = ConfigLevels_NT("system", "user", "global", "repository") + config_level: ConfigLevels_Tup = ("system", "user", "global", "repository") # Subclass configuration # Subclasses may easily bring in their own custom types by placing a constructor or type here @@ -802,7 +802,7 @@ class Repo(object): should get a continuous range spanning all line numbers in the file. """ data = self.git.blame(rev, '--', file, p=True, incremental=True, stdout_as_string=False, **kwargs) - commits = {} # type: Dict[str, TBD] + commits: Dict[str, Commit] = {} stream = (line for line in data.split(b'\n') if line) while True: -- cgit v1.2.1 From 3ce319f1296a5402079e9280500e96cc1d12fd04 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Tue, 6 Jul 2021 14:06:59 +0100 Subject: Add types to submodule.update() --- git/repo/base.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'git/repo/base.py') diff --git a/git/repo/base.py b/git/repo/base.py index e1b1fc76..ea86139b 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -48,6 +48,8 @@ if TYPE_CHECKING: from git.util import IterableList from git.refs.symbolic import SymbolicReference from git.objects import Tree + from git.objects.submodule.base import UpdateProgress + from git.remote import RemoteProgress # ----------------------------------------------------------- @@ -575,7 +577,7 @@ class Repo(object): return Commit.iter_items(self, rev, paths, **kwargs) def merge_base(self, *rev: TBD, **kwargs: Any - ) -> List[Union['SymbolicReference', Commit_ish, None]]: + ) -> List[Union[Commit_ish, None]]: """Find the closest common ancestor for the given revision (e.g. Commits, Tags, References, etc) :param rev: At least two revs to find the common ancestor for. @@ -588,7 +590,7 @@ class Repo(object): raise ValueError("Please specify at least two revs, got only %i" % len(rev)) # end handle input - res = [] # type: List[Union['SymbolicReference', Commit_ish, None]] + res = [] # type: List[Union[Commit_ish, None]] try: lines = self.git.merge_base(*rev, **kwargs).splitlines() # List[str] except GitCommandError as err: @@ -1014,7 +1016,8 @@ class Repo(object): @classmethod def _clone(cls, git: 'Git', url: PathLike, path: PathLike, odb_default_type: Type[GitCmdObjectDB], - progress: Optional[Callable], multi_options: Optional[List[str]] = None, **kwargs: Any + progress: Union['RemoteProgress', 'UpdateProgress', Callable[..., 'RemoteProgress'], None], + multi_options: Optional[List[str]] = None, **kwargs: Any ) -> 'Repo': odbt = kwargs.pop('odbt', odb_default_type) -- cgit v1.2.1 From deafa6a0ab837dffb8f78177f7c22d21ac65bf62 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Tue, 6 Jul 2021 15:40:06 +0100 Subject: Fix for mrepo2 --- git/repo/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git/repo/base.py') diff --git a/git/repo/base.py b/git/repo/base.py index ea86139b..a6f91aee 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -1016,7 +1016,7 @@ class Repo(object): @classmethod def _clone(cls, git: 'Git', url: PathLike, path: PathLike, odb_default_type: Type[GitCmdObjectDB], - progress: Union['RemoteProgress', 'UpdateProgress', Callable[..., 'RemoteProgress'], None], + progress: Union['RemoteProgress', 'UpdateProgress', Callable[..., 'RemoteProgress'], None] = None, multi_options: Optional[List[str]] = None, **kwargs: Any ) -> 'Repo': odbt = kwargs.pop('odbt', odb_default_type) -- cgit v1.2.1 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/repo/base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'git/repo/base.py') diff --git a/git/repo/base.py b/git/repo/base.py index a6f91aee..3214b528 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -36,7 +36,7 @@ import gitdb # typing ------------------------------------------------------ -from git.types import TBD, PathLike, Lit_config_levels, Commit_ish, Tree_ish +from git.types import TBD, PathLike, Lit_config_levels, Commit_ish, Tree_ish, is_config_level from typing import (Any, BinaryIO, Callable, Dict, Iterator, List, Mapping, Optional, Sequence, TextIO, Tuple, Type, Union, @@ -498,7 +498,7 @@ class Repo(object): unknown, instead the global path will be used.""" files = None if config_level is None: - files = [self._get_config_path(f) for f in self.config_level] + files = [self._get_config_path(f) for f in self.config_level if is_config_level(f)] else: files = [self._get_config_path(config_level)] return GitConfigParser(files, read_only=True, repo=self) @@ -623,7 +623,7 @@ class Repo(object): raise return True - def is_valid_object(self, sha: str, object_type: str = None) -> bool: + def is_valid_object(self, sha: str, object_type: Union[str, None] = None) -> bool: try: complete_sha = self.odb.partial_to_complete_sha_hex(sha) object_info = self.odb.info(complete_sha) @@ -976,7 +976,7 @@ class Repo(object): return blames @classmethod - def init(cls, path: PathLike = None, mkdir: bool = True, odbt: Type[GitCmdObjectDB] = GitCmdObjectDB, + def init(cls, path: Union[PathLike, None] = None, mkdir: bool = True, odbt: Type[GitCmdObjectDB] = GitCmdObjectDB, expand_vars: bool = True, **kwargs: Any) -> 'Repo': """Initialize a git repository at the given path if specified -- cgit v1.2.1