title: How do I get compatible type() behaviour in python 2 & 3 with unicode_literals?
body: <p>This question looks strikingly similar to <a href="https://stackoverflow.com/questions/19618031/unicode-literals-and-type">this one</a>, however the suggestion in the comments there doesn't work (anymore?) as demonstrated below.</p>
  
  <p>I'm trying to write a python2-3 compatible package, and one of my methods has a class generator in it, and <code>type()</code> is giving me problems in the python-2.7 tests:</p>
  
  <pre><code>Python 2.7.13 (default, Mar 18 2017, 17:03:32) 
  [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; from __future__ import unicode_literals
  &gt;&gt;&gt; from builtins import str
  &gt;&gt;&gt; type('MyClass', (object,), {})
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: type() argument 1 must be string, not unicode
  &gt;&gt;&gt; type(str('MyClass'), (object,), {})
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  TypeError: type() argument 1 must be string, not newstr
  </code></pre>
  
  <p>The <a href="http://python-future.org/overview.html" rel="nofollow noreferrer">Python-Future overview</a> page says:</p>
  
  <pre><code># Compatible output from isinstance() across Py2/3:
  assert isinstance(2**64, int)        # long integers
  assert isinstance(u'blah', str)
  assert isinstance('blah', str)       # only if unicode_literals is in effect
  </code></pre>
  
  <p>I expected this would give me consistent behaviour anywhere that strings are required, but apparently not.</p>
  
  <p>What's the correct, version-independent, way to do this?  The other question I linked to was asked in the era of python-2.6, and it seems like the behaviour has changed since then.  I don't think I can just dump unicode_literals, since I run into portability problems (elsewhere) with calls to hashlib if I don't have it.</p>