Supose that I have:
T = TypeVar('T', bound='Client') Now, I want a function that gets the type T and returns Client, or, yet better a class Client.
How I can get it in Python 3.9?
You can do so via typing_inspect.get_bound from the typing_inspect package. It offers cross-version support for runtime inspection of type annotations:
>>> class Foo: pass >>> T = TypeVar("T", bound=Foo) >>> typing_inspect.get_bound(T) __main__.Foo >>> T = TypeVar("T", bound="Foo") >>> typing_inspect.get_bound(T) ForwardRef('Foo') When bound is set to a string, the returned value is a "forward reference". I don't think there's currently a public API to evaluate that to a concrete class. There is a private API that you could use like this:
>>> ref = typing_inspect.get_bound(T) >>> ref._evaluate(globals(), globals(), set()) __main__.Foo typing_inspect in favor of __bound__ is that it provides a stable public API that works across Python versions. There is no guarantee that __bound__ works in past versions and will work in future versions, as it's not part of the standard. For the other question, it seems that you've already made it work, so it'd be great if you could accept the answer.