Skip to content Skip to sidebar Skip to footer

Python Type Hinting With Constraint On Type Superclass

Is it possible to add type hint in Python with constraint on the type's superclass? For example, I want something that looks like this: def foo(g: T(S)) -> T: pass which wo

Solution 1:

Per PEP484, the syntax you want seems to be:

from typing import TypeVar

T = TypeVar('T', bound=S)

def foo(g: T) -> T:
    pass

Post a Comment for "Python Type Hinting With Constraint On Type Superclass"