title: What's the correct way to check if an object is a typing.Generic?
body: <p>I'm trying to write code that validates type hints, and in order to do so I have to find out what kind of object the annotation is. For example, consider this snippet that's supposed to tell the user what kind of value is expected:</p>
  
  <pre><code>import typing
  
  typ = typing.Union[int, str]
  
  if issubclass(typ, typing.Union):
      print('value type should be one of', typ.__args__)
  elif issubclass(typ, typing.Generic):
      print('value type should be a structure of', typ.__args__[0])
  else:
      print('value type should be', typ)
  </code></pre>
  
  <p>This should print "value type should be one of (int, str)", but instead it throws an exception:</p>
  
  <pre><code>Traceback (most recent call last):
    File "untitled.py", line 6, in &lt;module&gt;
      if issubclass(typ, typing.Union):
    File "C:\Python34\lib\site-packages\typing.py", line 829, in __subclasscheck__
      raise TypeError("Unions cannot be used with issubclass().")
  TypeError: Unions cannot be used with issubclass().
  </code></pre>
  
  <p><code>isinstance</code> doesn't work either:</p>
  
  <pre><code>&gt;&gt;&gt; isinstance(typ, typing.Union)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "C:\Python34\lib\site-packages\typing.py", line 826, in __instancecheck__
      raise TypeError("Unions cannot be used with isinstance().")
  TypeError: Unions cannot be used with isinstance().
  </code></pre>
  
  <hr>
  
  <p>What's the correct way to check if <code>typ</code> is a <code>typing.Generic</code>?</p>
  
  <p><strong>If possible, I would like to see a solution that's backed by documentation or a PEP or some other resource.</strong> A "solution" that "works" by accessing undocumented, internal attributes is easy to find. But more likely than not, it'll turn out to be an implementation detail and will change in future versions. I'm looking for <em>"the right way"</em> to do it.</p>