Nick Coghlan added the comment: Rather than taking a snapshot of the whole class namespace, I think it will make more sense to make a new empty dictionary of descriptors to notify, and then split the current loop into two loops:
- the first loop adds descriptors with __set_name__ attributes to the notification dict - the second loop calls __set_name__ on all the descriptors in the notification dict Serhiy's patch effectively already does that via the initial PyDict_Copy, but that approach also redundantly copies items that don't define __set_name__ into the snapshot and then filters them out on the second pass. Reviewing the patch also made me realise we're currently missing a specification of the expected behaviour in https://docs.python.org/dev/reference/datamodel.html#creating-the-class-object. I suggest adding the following paragraph between the one about setting __class__ and the one about calling class descriptors: """ When using the default metaclass :cls:`type`, or any metaclass that ultimately calls ``type.__new__``, the following additional customisation steps are invoked: * first, ``type.__new__`` collects all of the descriptors in the class namespace that define a ``__set_name__`` method * second, all of these ``__set_name__`` methods are called with the class being defined and the assigned name of that particular descriptor * finally, the ``__init_subclass__`` hook is called on the immediate parent of the new class in its method resolution order """ ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue28797> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com