Skip to content

Commit

Permalink
src/__init__.py:Document.__get_item__(): improve annotations.
Browse files Browse the repository at this point in the history
Improve typing using @typing.overload - we accept `page`, `(chapter, page)` or
`slice`.

This addresses #3706.
  • Loading branch information
julian-smith-artifex-com committed Aug 5, 2024
1 parent 5853620 commit afff7b5
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2677,7 +2677,20 @@ def __enter__(self):
def __exit__(self, *args):
self.close()

def __getitem__(self, i: int =0):
@typing.overload
def __getitem__(self, i: int = 0) -> Page:
...

if sys.version_info >= (3, 9):
@typing.overload
def __getitem__(self, i: slice) -> list[Page]:
...

@typing.overload
def __getitem__(self, i: tuple[int, int]) -> Page:
...

def __getitem__(self, i=0):
if isinstance(i, slice):
return [self[j] for j in range(*i.indices(len(self)))]
assert isinstance(i, int) or (isinstance(i, tuple) and len(i) == 2 and all(isinstance(x, int) for x in i)), \
Expand Down

0 comments on commit afff7b5

Please sign in to comment.