Type Hint for Generic
Recipes
Generic Function
def generic_func[T](arg: T) -> T:
return arg + 1
# Python 3.11-
from typing import TypeVar
# Generic Type Variables
T = TypeVar['T', int, float]
def generic_func(arg: T) -> T:
return arg + 1
Generic Class
class GenericClass[T]:
pass
# Python 3.11-
from typing import TypeVar, Generic
# Generic Type Variables
T = TypeVar['T', int, float]
class GenericClass(Generic[T]):
pass