Skip to content

Commit 9377462

Browse files
committed
Make has_repo protocol runtime checkable and use in Diffable
1 parent 5eea891 commit 9377462

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

‎git/config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ def get_config_path(config_level: Lit_config_levels) -> str:
234234
elif config_level == "repository":
235235
raise ValueError("No repo to get repository configuration from. Use Repo._get_config_path")
236236
else:
237-
# Should not reach here. Will raise ValueError if does. Static typing will warn about extra and missing elifs
238-
assert_never(config_level, ValueError("Invalid configuration level: %r" % config_level))
237+
# Should not reach here. Will raise ValueError if does. Static typing will warn missing elifs
238+
assert_never(config_level, ValueError(f"Invalid configuration level: {config_level!r}"))
239239

240240

241241
class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, object)): # type: ignore ## mypy does not understand dynamic class creation # noqa: E501

‎git/diff.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# typing ------------------------------------------------------------------
1717

1818
from typing import Any, Iterator, List, Match, Optional, Tuple, Type, TypeVar, Union, TYPE_CHECKING
19-
from git.types import PathLike, TBD, Literal, TypeGuard
19+
from git.types import Has_Repo, PathLike, TBD, Literal, TypeGuard
2020

2121
if TYPE_CHECKING:
2222
from .objects.tree import Tree
@@ -141,8 +141,10 @@ def diff(self, other: Union[Type[Index], Type['Tree'], object, None, str] = Inde
141141
if paths is not None and not isinstance(paths, (tuple, list)):
142142
paths = [paths]
143143

144-
if hasattr(self, 'repo'): # else raise Error?
145-
self.repo = self.repo # type: 'Repo'
144+
if isinstance(self, Has_Repo):
145+
self.repo: Repo = self.repo
146+
else:
147+
raise AttributeError("No repo member found, cannot create DiffIndex")
146148

147149
diff_cmd = self.repo.git.diff
148150
if other is self.Index:

‎git/types.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import os
66
import sys
7-
from typing import (Callable, Dict, NoReturn, Tuple, Union, Any, Iterator, # noqa: F401
7+
from typing import (Callable, Dict, NoReturn, Sequence, Tuple, Union, Any, Iterator, # noqa: F401
88
NamedTuple, TYPE_CHECKING, TypeVar) # noqa: F401
99

1010
if TYPE_CHECKING:
@@ -37,6 +37,8 @@
3737
Tree_ish = Union['Commit', 'Tree']
3838
Commit_ish = Union['Commit', 'TagObject', 'Blob', 'Tree']
3939

40+
# Config_levels ---------------------------------------------------------
41+
4042
Lit_config_levels = Literal['system', 'global', 'user', 'repository']
4143

4244

@@ -47,12 +49,25 @@ def is_config_level(inp: str) -> TypeGuard[Lit_config_levels]:
4749

4850
ConfigLevels_Tup = Tuple[Literal['system'], Literal['user'], Literal['global'], Literal['repository']]
4951

52+
#-----------------------------------------------------------------------------------
53+
54+
55+
def assert_never(inp: NoReturn, raise_error: bool = True, exc: Union[Exception, None] = None) -> None:
56+
"""For use in exhaustive checking of literal or Enum in if/else chain.
57+
Should only be reached if all memebers not handled OR attempt to pass non-members through chain.
58+
59+
If all members handled, type is Empty. Otherwise, will cause mypy error.
60+
If non-members given, should cause mypy error at variable creation.
5061
51-
def assert_never(inp: NoReturn, exc: Union[Exception, None] = None) -> NoReturn:
52-
if exc is None:
53-
assert False, f"An unhandled Literal ({inp}) in an if/else chain was found"
62+
If raise_error is True, will also raise AssertionError or the Exception passed to exc.
63+
"""
64+
if raise_error:
65+
if exc is None:
66+
raise ValueError(f"An unhandled Literal ({inp}) in an if/else chain was found")
67+
else:
68+
raise exc
5469
else:
55-
raise exc
70+
pass
5671

5772

5873
class Files_TD(TypedDict):

0 commit comments

Comments
 (0)