diff options
-rw-r--r-- | git/types.py | 9 | ||||
-rw-r--r-- | git/util.py | 12 |
2 files changed, 11 insertions, 10 deletions
diff --git a/git/types.py b/git/types.py index 91d35b56..a410cb36 100644 --- a/git/types.py +++ b/git/types.py @@ -7,15 +7,12 @@ import sys from typing import Union, Any if sys.version_info[:2] >= (3, 8): - from typing import Final, Literal # noqa: F401 + from typing import Final, Literal, SupportsIndex # noqa: F401 else: - from typing_extensions import Final, Literal # noqa: F401 + from typing_extensions import Final, Literal, SupportsIndex # noqa: F401 -if sys.version_info[:2] < (3, 6): - # os.PathLike (PEP-519) only got introduced with Python 3.6 - PathLike = str -elif sys.version_info[:2] < (3, 9): +if sys.version_info[:2] < (3, 9): # Python >= 3.6, < 3.9 PathLike = Union[str, os.PathLike] elif sys.version_info[:2] >= (3, 9): diff --git a/git/util.py b/git/util.py index 05eeb4ad..516c315c 100644 --- a/git/util.py +++ b/git/util.py @@ -29,7 +29,7 @@ import pathlib if TYPE_CHECKING: from git.remote import Remote from git.repo.base import Repo -from .types import PathLike, TBD, Literal +from .types import PathLike, TBD, Literal, SupportsIndex # --------------------------------------------------------------------- @@ -971,7 +971,10 @@ class IterableList(list): # END for each item return list.__getattribute__(self, attr) - def __getitem__(self, index: Union[int, slice, str]) -> Any: # type: ignore + def __getitem__(self, index: Union[SupportsIndex, int, slice, str]) -> Any: + + assert isinstance(index, (int, str, slice)), "Index of IterableList should be an int or str" + if isinstance(index, int): return list.__getitem__(self, index) elif isinstance(index, slice): @@ -983,12 +986,13 @@ class IterableList(list): raise IndexError("No item found with id %r" % (self._prefix + index)) from e # END handle getattr - def __delitem__(self, index: Union[int, str, slice]) -> None: # type: ignore + def __delitem__(self, index: Union[SupportsIndex, int, slice, str]) -> Any: + + assert isinstance(index, (int, str)), "Index of IterableList should be an int or str" delindex = cast(int, index) if not isinstance(index, int): delindex = -1 - assert not isinstance(index, slice) name = self._prefix + index for i, item in enumerate(self): if getattr(item, self._id_attr) == name: |