classification
Title: Misleading documentation for __prepare__
Type: behavior Stage: needs patch
Components: Documentation, Tests Versions: Python 3.3, Python 3.2
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: William.Schwartz, daniel.urban, docs@python, eric.araujo, ncoghlan
Priority: normal Keywords:

Created on 2012-07-03 15:08 by William.Schwartz, last changed 2012-07-05 20:52 by daniel.urban.

Files
File name Uploaded Description Edit
test_metaclass.py William.Schwartz, 2012-07-03 15:30 Test to demonstrate implicit staticmethodness of __prepare__
Messages (5)
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.
History
Date User Action Args
2012-07-05 20:52:19daniel.urbansetmessages: + msg164697
2012-07-05 19:59:06William.Schwartzsetmessages: + msg164696
2012-07-05 16:47:07daniel.urbansetmessages: + msg164685
2012-07-05 16:35:52eric.araujosetnosy: + ncoghlan, eric.araujo
stage: needs patch

versions: + Python 3.2
2012-07-03 17:25:37daniel.urbansetnosy: + daniel.urban
2012-07-03 15:30:12William.Schwartzsetfiles: + test_metaclass.py

messages: + msg164607
2012-07-03 15:08:37William.Schwartzcreate