Created
May 3, 2025 00:48
-
-
Save mypy-play/9b83b2a942d6f762f5069641df5d0dc3 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 __future__ import annotations | |
from enum import IntEnum | |
from typing import Protocol, Self, cast, reveal_type | |
class SupportsAdd(Protocol): | |
def __add__(self, other: Self, /) -> Self: ... | |
def add_one[T: SupportsAdd](value: T) -> T: | |
match value: | |
case int(): | |
return cast("T", value + 1) | |
case str(): | |
return cast("T", value + " and one more") | |
case _: | |
raise TypeError() | |
class Options(IntEnum): | |
A = 1 | |
B = 2 | |
C = 3 | |
def test(i: int, s: str, i_s: int | str, o: Options) -> None: | |
reveal_type(add_one(i)) | |
reveal_type(add_one(s)) | |
reveal_type(add_one(i_s)) | |
reveal_type(add_one(o)) | |
reveal_type(add_one(int(o))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment