It seems that although the __getitem__
-docs list slice support as optional, the typing information requires both support for integer indices and slices.
Implementing them resolves both warnings.
The code below no longer produces warnings for me.
from collections.abc import Sequence
from typing import override, overload
class MySeq(Sequence[float]):
def __init__(self):
self._data: list[float] = list()
@override
def __len__(self) -> int:
return len(self._data)
@overload
def __getitem__(self, key: int) -> float:
pass
@overload
def __getitem__(self, key: slice) -> Sequence[float]:
pass
@override
def __getitem__(self, key: int | slice) -> float | Sequence[float]:
return self._data[key]
Thanks for the comments of @jonrsharpe and @chepner for providing the right direction with this.