Skip to content

Instantly share code, notes, and snippets.

Created Nov 22, 2022
What would you like to do?
Shared via mypy Playground
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:
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})
Copy link

This seems to work - myint and mylist are accepted, mydict is rejected for the right reason.

(You can put type hint dict[str, int] on mydict to silence a different error)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment