Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created June 9, 2026 15:43
Show Gist options
  • Select an option

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

Select an option

Save mypy-play/087708861f528a42b0fee02a04d2a28e to your computer and use it in GitHub Desktop.
Shared via mypy Playground
from typing import TypeVar
from functools import singledispatch
#T = TypeVar("T", bound=str | int) # should give errors
T = TypeVar("T", str, int) # should work
@singledispatch
def times_two(v: T) -> T:
raise NotImplementedError
@times_two.register
def _(v: int) -> int:
return v * 2
@times_two.register
def _(v: str) -> str:
return str(int(v) * 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment