title: Python metaclasses handling double class creation with same `__classcell__` attribute
body: <p>I wrote a metaclass to handle the creation of two classes at class definition (with one accessible as an attribute of the other).</p>
  <p>MRE with logic removed:</p>
  <pre><code>class MyMeta(type):
  
      def __new__(mcs, clsname, bases, namespace, **kwds):
          result = super().__new__(mcs, clsname, bases, namespace)
          
          # Omitted logic to prefix namespace
  
          print(namespace)
          # namespace.pop('__classcell__', None)
          disambiguated_cls = super().__new__(mcs, clsname + 'Prefixed', bases, namespace)
  
          result.disambiguated_cls = disambiguated_cls
          return result
  
      
  class A(metaclass=MyMeta):
      
      @classmethod
      def f(cls, x):
          return x + 1
  
  class B(A):
      
      @classmethod
      def f(cls, x):
          # return A.f(x)
          return super().f(x)
  </code></pre>
  <p>Anytime <code>super</code> is called (e.g. in class <code>B</code> definition) a <code>__classcell__</code> object is added to the namespace. If that <code>__classcell__</code> object is duplicated (e.g. in the creation of <code>disambiguated_cls</code>) the following exception is raised:</p>
  <pre><code>TypeError: __class__ set to &lt;class '__main__.B'&gt; defining 'B' as &lt;class '__main__.B'&gt;
  </code></pre>
  <p>Looks like there are two solutions (corresponding to the two commented lines in the example):</p>
  <ul>
  <li>Don't call super (just directly call function on the superclass: <code>A.f(x)</code>)</li>
  <li>Pop the <code>__classcell__</code> out of the namespace</li>
  </ul>
  <p>The first means manual resolution of superclasses and I'm not sure what downstream effects the latter can have.</p>
  <p>For reference <a href="https://docs.python.org/3/reference/datamodel.html#creating-the-class-object" rel="nofollow noreferrer">the docs on <code>__classcell__</code> in metaclasses</a> state:</p>
  <blockquote>
  <p>In CPython 3.6 and later, the <code>__class__</code> cell is passed to the metaclass as a <code>__classcell_</code>_ entry in the class namespace. If present, this must be propagated up to the <code>type.__new__</code> call in order for the class to be initialised correctly. Failing to do so will result in a RuntimeError in Python 3.8.</p>
  </blockquote>
  <p>Is there another option? What is the best way to do this?</p>