title: How to get the runtime type of a class generic type by using a decorator?
body: <p>I have a generic class like</p>
  <pre class="lang-py prettyprint-override"><code>from typing import Generic, TypeVar, List
  
  T = TypeVar('T')
  
  @my_decorator
  class Foo(Generic[T]):
      pass
  
  f: Foo[int] = Foo()
  g: Foo[List[float]] = Foo()
  </code></pre>
  <p>Is there a clean way to get the type annotations of the constructor call <code>Foo[int], Foo[List[float]]</code> in the decorator? I want to do some type checking at runtime.</p>
  <p>I'm able do access the constructor call of <code>Foo</code> via the decorator and I even get the code line <code>f: Foo[int] = Foo()</code> of the constructor call by using <code>inspect.stack()</code> and <code>inspect.getsource(my_frame)</code> in a very unpretty way. Then I'm able to get <code>Foo[int]</code> by some string manipulation.</p>
  <p>Beside that this is a very dirty way of doing it, it only get the type as a string. But I need the actually type. In this example I could use <code>eval()</code> to &quot;parse&quot; the string and convert it into a type. But that doesn't work for custom classes, like in this example:</p>
  <pre class="lang-py prettyprint-override"><code>from typing import Generic, TypeVar, List
  
  T = TypeVar('T')
  
  class Bar:
      pass
  
  @my_decorator
  class Foo(Generic[T]):
      pass
  
  h: Foo[List[Bar]] = Foo()
  </code></pre>
  <p>In this case I cannot use <code>eval()</code> because I have no idea how I can get the proper <code>context</code>.
  I like to get something like <code>my_file.Foo[typing.List[my_file.Bar]]</code> which would allow me to do type checking at runtime.</p>
  <p>So is there any clean way of doing it? Or is there at least a (dirty) way to get the correct context for <code>eval()</code> to &quot;parse&quot; the string?</p>