|
34 | 34 | Type, |
35 | 35 | Union, |
36 | 36 | cast, |
| 37 | + overload, |
37 | 38 | ) |
38 | 39 |
|
39 | 40 | from git.types import PathLike |
@@ -175,6 +176,8 @@ class Tree(IndexObject, git_diff.Diffable, util.Traversable): |
175 | 176 |
|
176 | 177 | * Access a specific blob using the ``tree["filename"]`` notation. |
177 | 178 | * You may likewise access by index, like ``blob = tree[0]``. |
| 179 | + * Slicing, like ``tree[:2]``, returns a list of objects. |
| 180 | + * ``reversed(tree)`` iterates over objects in reverse order. |
178 | 181 | """ |
179 | 182 |
|
180 | 183 | type: Literal["tree"] = "tree" |
@@ -377,7 +380,16 @@ def __iter__(self) -> Iterator[IndexObjUnion]: |
377 | 380 | def __len__(self) -> int: |
378 | 381 | return len(self._cache) |
379 | 382 |
|
380 | | - def __getitem__(self, item: Union[str, int, slice]) -> IndexObjUnion: |
| 383 | + @overload |
| 384 | + def __getitem__(self, item: Union[str, int]) -> IndexObjUnion: ... |
| 385 | + |
| 386 | + @overload |
| 387 | + def __getitem__(self, item: slice) -> List[IndexObjUnion]: ... |
| 388 | + |
| 389 | + def __getitem__(self, item: Union[str, int, slice]) -> Union[IndexObjUnion, List[IndexObjUnion]]: |
| 390 | + if isinstance(item, slice): |
| 391 | + return list(self._iter_convert_to_object(self._cache[item])) |
| 392 | + |
381 | 393 | if isinstance(item, int): |
382 | 394 | info = self._cache[item] |
383 | 395 | return self._map_id_to_type[info[1] >> 12](self.repo, info[0], info[1], join_path(self.path, info[2])) |
@@ -409,7 +421,7 @@ def __contains__(self, item: Union[IndexObjUnion, PathLike]) -> bool: |
409 | 421 | return False |
410 | 422 |
|
411 | 423 | def __reversed__(self) -> Iterator[IndexObjUnion]: |
412 | | - return reversed(self._iter_convert_to_object(self._cache)) # type: ignore[call-overload] |
| 424 | + return self._iter_convert_to_object(reversed(self._cache)) |
413 | 425 |
|
414 | 426 | def _serialize(self, stream: "BytesIO") -> "Tree": |
415 | 427 | """Ask Git to build this cache as a tree, then copy its raw object contents.""" |
|
0 commit comments