79518256

Date: 2025-03-18 17:32:46
Score: 1.5
Natty:
Report link

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.

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @jonrsharpe
  • User mentioned (0): @chepner
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: 502E532E