Created
April 8, 2026 22:46
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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