Skip to content

Commit 18b6aa5

Browse files
committed
add SupportsIndex to IterableList, with version import guards
1 parent f8ec952 commit 18b6aa5

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

git/types.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77
from typing import Union, Any
88

99
if sys.version_info[:2] >= (3, 8):
10-
from typing import Final, Literal # noqa: F401
10+
from typing import Final, Literal, SupportsIndex # noqa: F401
1111
else:
12-
from typing_extensions import Final, Literal # noqa: F401
12+
from typing_extensions import Final, Literal, SupportsIndex # noqa: F401
1313

1414

15-
if sys.version_info[:2] < (3, 6):
16-
# os.PathLike (PEP-519) only got introduced with Python 3.6
17-
PathLike = str
18-
elif sys.version_info[:2] < (3, 9):
15+
if sys.version_info[:2] < (3, 9):
1916
# Python >= 3.6, < 3.9
2017
PathLike = Union[str, os.PathLike]
2118
elif sys.version_info[:2] >= (3, 9):

git/util.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
if TYPE_CHECKING:
3030
from git.remote import Remote
3131
from git.repo.base import Repo
32-
from .types import PathLike, TBD, Literal
32+
from .types import PathLike, TBD, Literal, SupportsIndex
3333

3434
# ---------------------------------------------------------------------
3535

@@ -971,7 +971,10 @@ def __getattr__(self, attr: str) -> Any:
971971
# END for each item
972972
return list.__getattribute__(self, attr)
973973

974-
def __getitem__(self, index: Union[int, slice, str]) -> Any: # type: ignore
974+
def __getitem__(self, index: Union[SupportsIndex, int, slice, str]) -> Any:
975+
976+
assert isinstance(index, (int, str, slice)), "Index of IterableList should be an int or str"
977+
975978
if isinstance(index, int):
976979
return list.__getitem__(self, index)
977980
elif isinstance(index, slice):
@@ -983,12 +986,13 @@ def __getitem__(self, index: Union[int, slice, str]) -> Any: # type: ignore
983986
raise IndexError("No item found with id %r" % (self._prefix + index)) from e
984987
# END handle getattr
985988

986-
def __delitem__(self, index: Union[int, str, slice]) -> None: # type: ignore
989+
def __delitem__(self, index: Union[SupportsIndex, int, slice, str]) -> Any:
990+
991+
assert isinstance(index, (int, str)), "Index of IterableList should be an int or str"
987992

988993
delindex = cast(int, index)
989994
if not isinstance(index, int):
990995
delindex = -1
991-
assert not isinstance(index, slice)
992996
name = self._prefix + index
993997
for i, item in enumerate(self):
994998
if getattr(item, self._id_attr) == name:

0 commit comments

Comments
 (0)