Per the 3.6.0 docs:
CPython implementation detail: In CPython 3.6 and later, the
__class__cell is passed to the metaclass as a__classcell__entry in the class namespace. If present, this must be propagated up to thetype.__new__call in order for the class to be initialized correctly. Failing to do so will result in aDeprecationWarningin Python 3.6, and aRuntimeWarningin the future.
Can someone provide an example of doing this correctly?
An example where it's actually needed?
The warning is raised if you use super that relies on
__class__being available or reference__class__inside the class body.What the text essentially says is that, this is needed if you define a custom meta-class and tamper with the namespace you get before passing it up to
type.__new__. You'll need to be careful and always make sure you pass__classcell__totype.__new__in yourmetaclass.__new__.That is, if you create a new fancy namespace to pass up, always check if
__classcell__is defined in the original namespace created and add it:The file you linked in the comment is actually the first of many attempted patches,
issue23722_classcell_reference_validation_v2.diffis the final patch that made it in, from Issue 23722.An example of doing this correctly can be seen in a pull request made to Django that uses this to fix an issue that was introduced in Python 3.6:
The
__classcell__is simply added to the new namespace before being passed totype.__new__.