Created
November 22, 2022 13:38
-
-
Save mypy-play/ba0ae4e284ea9a09007a3b84e183ad26 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
This file contains 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 abc import abstractmethod | |
from typing import Protocol, TypeVar | |
T = TypeVar("T") | |
T_add = TypeVar("T_add", bound="Addable") | |
class Addable(Protocol): | |
def __add__(self: T, other: T) -> T: | |
pass | |
def foo(x: T_add, y: T_add) -> T_add: | |
return x + y | |
myint: int = foo(1, 2) | |
mylist: list[int] = foo([1], [2]) | |
mydict: dict = foo({"a": 1}, {"b": 2}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This seems to work -
myint
andmylist
are accepted,mydict
is rejected for the right reason.(You can put type hint
dict[str, int]
on mydict to silence a different error)