[New-bugs-announce] [issue47144] Allow setting __classcell__

Douglas Raillard report at bugs.python.org
Mon Mar 28 10:32:05 EDT 2022


New submission from Douglas Raillard <douglas.raillard at arm.com>:

The cell object __classcell__ currently cannot be set by code invoked by metaclass.__new__. Attempts to do so will be caught by builtin___build_class__ in bltimodule.c:

                } else {
                    const char *msg =
                        "__class__ set to %.200R defining %.200R as %.200R";
                    PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
                }

This is unfortunate as there is a use-case for such trickery: if the method of a class A are only used to be grafted onto another class B (monkey patching), A.__classcell__ should be set to B so that super() works as expected.

This can be useful in contexts where e.g. A.__new__ returns instances of B. B is not necessarily something under direct user control (it can be dynamically created by inheriting from a "template"), so A would be a better place for users to define custom method.

----------
components: Interpreter Core
messages: 416172
nosy: douglas-raillard-arm
priority: normal
severity: normal
status: open
title: Allow setting __classcell__
versions: Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue47144>
_______________________________________


More information about the New-bugs-announce mailing list