Author ncoghlan
Recipients Martin.Teichmann, Wombatz, abarry, ncoghlan, ned.deily, rhettinger, serhiy.storchaka
Date 2016-11-29.03:45:40
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1480391141.65.0.895420067872.issue28797@psf.upfronthosting.co.za>
In-reply-to
Content
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
"""
History
Date User Action Args
2016-11-29 03:45:41ncoghlansetrecipients: + ncoghlan, rhettinger, ned.deily, serhiy.storchaka, Martin.Teichmann, abarry, Wombatz
2016-11-29 03:45:41ncoghlansetmessageid: <1480391141.65.0.895420067872.issue28797@psf.upfronthosting.co.za>
2016-11-29 03:45:41ncoghlanlinkissue28797 messages
2016-11-29 03:45:40ncoghlancreate