From afff7b512465dfce5afd11a925e0557205fa50fe Mon Sep 17 00:00:00 2001 From: Julian Smith Date: Mon, 5 Aug 2024 11:57:09 +0100 Subject: [PATCH] src/__init__.py:Document.__get_item__(): improve annotations. Improve typing using @typing.overload - we accept `page`, `(chapter, page)` or `slice`. This addresses #3706. --- src/__init__.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/__init__.py b/src/__init__.py index b1d4e5ca3..f715c072d 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -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)), \