title: How to type the __new__ method in a Python metaclass so that mypy is happy
body: <p>I am trying to type the <code>__new__</code> method in a metaclass in Python so that it pleases mypy. The code would be something like this (taken from <a href="https://www.python.org/dev/peps/pep-3115/#example" rel="noreferrer">pep-3115</a> - &quot;Metaclasses in Python 3000&quot; and stripped down a bit):</p>
  <pre class="lang-py prettyprint-override"><code>from __future__ import annotations
  
  from typing import Type
  
  
  # The metaclass
  class MetaClass(type):
  
      # The metaclass invocation
      def __new__(cls: Type[type], name: str, bases: tuple, classdict: dict) -&gt; type:
          result = type.__new__(cls, name, bases, classdict)
          print('in __new__')
          return result
  
  
  class MyClass(metaclass=MetaClass):
      pass
  </code></pre>
  <p>With this, mypy complains, <code>Incompatible return type for &quot;__new__&quot; (returns &quot;type&quot;, but must return a subtype of &quot;MetaClass&quot;)</code>, pointing at the line <code>def __new__</code>.</p>
  <p>I have also tried with:</p>
  <pre class="lang-py prettyprint-override"><code>def __new__(cls: Type[MetaClass], name: str, bases: tuple, classdict: dict) -&gt; MetaClass:
  </code></pre>
  <p>Then mypy complains (about the <code>return result</code> line): <code>Incompatible return value type (got &quot;type&quot;, expected &quot;MetaClass&quot;)</code>.</p>
  <p>I have also tried with a type var (<code>TSubMetaclass = TypeVar('TSubMetaclass', bound='MetaClass')</code>) and the result is the same as using <code>MetaClass</code>.</p>
  <p>Using <code>super().__new__</code> instead of <code>type.__new__</code> gave similar results.</p>
  <p>What would be the correct way to do it?</p>