Code: Select all
from typing import Type
class Animal:
def speak(self):
assert False
class Dog(Animal):
def speak(self):
print("bark")
def make_animal[T : Animal](animal_class: Type[T] = Animal) -> T:
return animal_class()
dog = make_animal(Dog)
dog.speak()
>>>bark
Code: Select all
$ mypy test.py
error: Incompatible default for argument "animal_class" (default has type "type[Animal]", argument has type "type[T]") [assignment]
$ mypy --version
mypy 1.15.0 (compiled: yes)