msg164606 - (view) |
Author: William Schwartz (William.Schwartz) * |
Date: 2012-07-03 15:08 |
Section 3.3.3.2. "Preparing the class namespace" of the documentation (http://docs.python.org/dev/reference/datamodel.html#preparing-the-class-namespace) states that "If the metaclass has a __prepare__ attribute, it is called as ``namespace = metaclass.__prepare__(name, bases, **kwds)``...." This isn't quite true. By just defining a ``__prepare__`` method in a metaclass, the interpreter calls it as it would a static method -- there is no implicit first argument referring to ``metaclass`` as the documentation implies. The documentation should be amended to say that users can decorate ``__prepare__`` as a class method to get ``metaclass`` passed in as the implicit first argument. |
|
|
msg164607 - (view) |
Author: William Schwartz (William.Schwartz) * |
Date: 2012-07-03 15:30 |
Attached a unittest script to demonstrate that __prepare__ is implicitly a staticmethod. |
|
|
msg164685 - (view) |
Author: Daniel Urban (daniel.urban) *  |
Date: 2012-07-05 16:47 |
__prepare__ is not implicitly a staticmethod, and it is called exactly as documented (also in types.prepare_class). There is no implicit first argument because the method is called on the (meta)class object. |
|
|
msg164696 - (view) |
Author: William Schwartz (William.Schwartz) * |
Date: 2012-07-05 19:59 |
Daniel, Good point. However it would still be useful for documentation to point out that __prepare__ can be passed the metaclass as the implicit first argument by being decorated by classmethod. I'll post a small patch when I get a chance to add a sentence saying as much in the documentation and reorganize the test cases in Lib/test/test_metaclass.py to make it part of the narrative of the doctests. |
|
|
msg164697 - (view) |
Author: Daniel Urban (daniel.urban) *  |
Date: 2012-07-05 20:52 |
Actually the docs contained a similar sentence ("If the metaclass has a :meth:`__prepare__` attribute (usually implemented as a class or static method), ..."), but it was removed in befd56673c80 when Nick rewrote this section. |
|
|
msg221871 - (view) |
Author: Mark Lawrence (BreamoreBoy) * |
Date: 2014-06-29 18:08 |
Does the documentation need amending, yes or no? |
|
|
msg267459 - (view) |
Author: Jelle Zijlstra (JelleZijlstra) *  |
Date: 2016-06-05 19:00 |
I think the current documentation is correct and doesn't need changes. There is also already an example of a working __prepare__ method. |
|
|
msg268356 - (view) |
Author: Berker Peksag (berker.peksag) *  |
Date: 2016-06-12 09:51 |
The documentation should mention that the __prepare__ method must be implemented as a classmethod (see for the reference.) The example at https://docs.python.org/dev/reference/datamodel.html#metaclass-example needs to be linked from the "Preparing the class namespace" section. |
|
|
msg268719 - (view) |
Author: Jaysinh shukla (jaysinh.shukla) * |
Date: 2016-06-17 11:04 |
Submitting patch according to this (http://bugs.python.org/issue15243#msg268356) message. Thanks! |
|
|
msg355470 - (view) |
Author: Benjamin Edwards (benedwards14) * |
Date: 2019-10-27 15:06 |
Is this still open, does the patch still need a pull request? |
|
|
msg360697 - (view) |
Author: Berker Peksag (berker.peksag) *  |
Date: 2020-01-25 18:50 |
New changeset 7de617455ed788e6730c40cf854c4b72b0432194 by Berker Peksag (alclarks) in branch 'master': bpo-15243: Document __prepare__ as classmethod (GH-17124) https://github.com/python/cpython/commit/7de617455ed788e6730c40cf854c4b72b0432194 |
|
|
msg360698 - (view) |
Author: Berker Peksag (berker.peksag) *  |
Date: 2020-01-25 19:04 |
New changeset 321491a536c378227f9d574703f7c06f89c67dcf by Berker Peksag (Miss Islington (bot)) in branch '3.8': bpo-15243: Document __prepare__ as classmethod (GH-17124) https://github.com/python/cpython/commit/321491a536c378227f9d574703f7c06f89c67dcf |
|
|
msg360699 - (view) |
Author: Berker Peksag (berker.peksag) *  |
Date: 2020-01-25 19:04 |
New changeset 548685e364af3d366d3aef924b2077ee6f8d13dc by Berker Peksag (Miss Islington (bot)) in branch '3.7': bpo-15243: Document __prepare__ as classmethod (GH-17124) https://github.com/python/cpython/commit/548685e364af3d366d3aef924b2077ee6f8d13dc |
|
|