Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created February 12, 2026 14:06
Show Gist options
  • Select an option

  • Save mypy-play/2a9def866495688f257f1839bdc1634a to your computer and use it in GitHub Desktop.

Select an option

Save mypy-play/2a9def866495688f257f1839bdc1634a to your computer and use it in GitHub Desktop.
Shared via mypy Playground
from typing import Any, Protocol, assert_type
class WithTuple[*Ts](Protocol):
tup: tuple[*Ts]
def get_tuple[*Ts](e: WithTuple[*Ts]) -> tuple[*Ts]:
return e.tup
def get_first[T](e: WithTuple[T, *tuple[Any, ...]]) -> T:
return e.tup[0]
class X:
tup: tuple[int, str]
assert_type(get_tuple(X()), tuple[int, str])
assert_type(get_first(X()), int)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment