Created
June 9, 2026 15:43
-
-
Save mypy-play/087708861f528a42b0fee02a04d2a28e to your computer and use it in GitHub Desktop.
Shared via mypy Playground
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 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