Message281942
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
""" |
|
Date |
User |
Action |
Args |
2016-11-29 03:45:41 | ncoghlan | set | recipients:
+ ncoghlan, rhettinger, ned.deily, serhiy.storchaka, Martin.Teichmann, abarry, Wombatz |
2016-11-29 03:45:41 | ncoghlan | set | messageid: <1480391141.65.0.895420067872.issue28797@psf.upfronthosting.co.za> |
2016-11-29 03:45:41 | ncoghlan | link | issue28797 messages |
2016-11-29 03:45:40 | ncoghlan | create | |
|