Skip to content

Instantly share code, notes, and snippets.

@kurtbrose
Created April 8, 2026 22:46
Show Gist options
  • Select an option

  • Save kurtbrose/cd1c05feddf745eb936e3755a1f258e8 to your computer and use it in GitHub Desktop.

Select an option

Save kurtbrose/cd1c05feddf745eb936e3755a1f258e8 to your computer and use it in GitHub Desktop.
just a little helper -- cast a tuple / list to this when you want to keep index NewType(int) straight
from collections.abc import Iterator
from collections.abc import Sequence
from typing import Protocol
from typing import runtime_checkable
@runtime_checkable
class TypedIndexSeq[K: int, V](Protocol):
"""
Sequence-like object where indexed access is restricted to a typed int-like key.
Pair with NewType(int) for K to keep from crossing up indexes.
"""
def __len__(self) -> int: ...
def __iter__(self) -> Iterator[V]: ...
@overload
def __getitem__(self, idx: K) -> V: ...
@overload
def __getitem__(self, idx: slice) -> Sequence[V]: ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment