summaryrefslogtreecommitdiff
path: root/git
diff options
context:
space:
mode:
authorYobmod <yobmod@gmail.com>2021-07-09 11:40:32 +0100
committerYobmod <yobmod@gmail.com>2021-07-09 11:40:32 +0100
commit937746291cfdaa40938de03db305b1137c391907 (patch)
tree6f28d6c757cc33f5dadec2635d4d0e63856e2942 /git
parent5eea8910b2e07d424a2e33299149d13392a80a54 (diff)
downloadgitpython-937746291cfdaa40938de03db305b1137c391907.tar.gz
Make has_repo protocol runtime checkable and use in Diffable
Diffstat (limited to 'git')
-rw-r--r--git/config.py4
-rw-r--r--git/diff.py8
-rw-r--r--git/types.py25
3 files changed, 27 insertions, 10 deletions
diff --git a/git/config.py b/git/config.py
index 19ce1f84..2c863f93 100644
--- a/git/config.py
+++ b/git/config.py
@@ -234,8 +234,8 @@ def get_config_path(config_level: Lit_config_levels) -> str:
elif config_level == "repository":
raise ValueError("No repo to get repository configuration from. Use Repo._get_config_path")
else:
- # Should not reach here. Will raise ValueError if does. Static typing will warn about extra and missing elifs
- assert_never(config_level, ValueError("Invalid configuration level: %r" % config_level))
+ # Should not reach here. Will raise ValueError if does. Static typing will warn missing elifs
+ assert_never(config_level, ValueError(f"Invalid configuration level: {config_level!r}"))
class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, object)): # type: ignore ## mypy does not understand dynamic class creation # noqa: E501
diff --git a/git/diff.py b/git/diff.py
index d3b18652..cb216299 100644
--- a/git/diff.py
+++ b/git/diff.py
@@ -16,7 +16,7 @@ from .objects.util import mode_str_to_int
# typing ------------------------------------------------------------------
from typing import Any, Iterator, List, Match, Optional, Tuple, Type, TypeVar, Union, TYPE_CHECKING
-from git.types import PathLike, TBD, Literal, TypeGuard
+from git.types import Has_Repo, PathLike, TBD, Literal, TypeGuard
if TYPE_CHECKING:
from .objects.tree import Tree
@@ -141,8 +141,10 @@ class Diffable(object):
if paths is not None and not isinstance(paths, (tuple, list)):
paths = [paths]
- if hasattr(self, 'repo'): # else raise Error?
- self.repo = self.repo # type: 'Repo'
+ if isinstance(self, Has_Repo):
+ self.repo: Repo = self.repo
+ else:
+ raise AttributeError("No repo member found, cannot create DiffIndex")
diff_cmd = self.repo.git.diff
if other is self.Index:
diff --git a/git/types.py b/git/types.py
index b107c2e1..9181e040 100644
--- a/git/types.py
+++ b/git/types.py
@@ -4,7 +4,7 @@
import os
import sys
-from typing import (Callable, Dict, NoReturn, Tuple, Union, Any, Iterator, # noqa: F401
+from typing import (Callable, Dict, NoReturn, Sequence, Tuple, Union, Any, Iterator, # noqa: F401
NamedTuple, TYPE_CHECKING, TypeVar) # noqa: F401
if TYPE_CHECKING:
@@ -37,6 +37,8 @@ _T = TypeVar('_T')
Tree_ish = Union['Commit', 'Tree']
Commit_ish = Union['Commit', 'TagObject', 'Blob', 'Tree']
+# Config_levels ---------------------------------------------------------
+
Lit_config_levels = Literal['system', 'global', 'user', 'repository']
@@ -47,12 +49,25 @@ def is_config_level(inp: str) -> TypeGuard[Lit_config_levels]:
ConfigLevels_Tup = Tuple[Literal['system'], Literal['user'], Literal['global'], Literal['repository']]
+#-----------------------------------------------------------------------------------
+
+
+def assert_never(inp: NoReturn, raise_error: bool = True, exc: Union[Exception, None] = None) -> None:
+ """For use in exhaustive checking of literal or Enum in if/else chain.
+ Should only be reached if all memebers not handled OR attempt to pass non-members through chain.
+
+ If all members handled, type is Empty. Otherwise, will cause mypy error.
+ If non-members given, should cause mypy error at variable creation.
-def assert_never(inp: NoReturn, exc: Union[Exception, None] = None) -> NoReturn:
- if exc is None:
- assert False, f"An unhandled Literal ({inp}) in an if/else chain was found"
+ If raise_error is True, will also raise AssertionError or the Exception passed to exc.
+ """
+ if raise_error:
+ if exc is None:
+ raise ValueError(f"An unhandled Literal ({inp}) in an if/else chain was found")
+ else:
+ raise exc
else:
- raise exc
+ pass
class Files_TD(TypedDict):