I slightly modified the documented mypy generic example but got an error:
from typing import Generic, TypeVar, Union
T = TypeVar("T", bound=Union[int, float])
class Person(Generic[T]):
def __init__(self, salary: T) -> None:
self.salary: T = salary
def get_yearly_bonus(self, amount: T) -> None:
self.salary += amount
def __str__(self) -> str:
return f"Person(salary={self.salary})"
p = Person[int](9)
p.get_yearly_bonus(6)
print(p)
It seems mypy fails to recognize the fact that self.salary and amount must
be of the same type.
$ mypy test.py
test.py:11: error: Incompatible types in assignment (expression has type "float", variable has type "T")
test.py:11: error: Unsupported operand types for + (likely involving Union)
Found 2 errors in 1 file (checked 1 source file)