Fix "fixed" descriptor handling, and add tests.

This commit is contained in:
Georg Brandl 2009-10-25 11:26:10 +01:00
parent 9088649e0f
commit f04ca65690
3 changed files with 26 additions and 3 deletions

View File

@ -1,6 +1,8 @@
Release 0.6.4 (in development) Release 0.6.4 (in development)
============================== ==============================
* Fix the handling of non-data descriptors in autodoc.
Release 0.6.3 (Sep 03, 2009) Release 0.6.3 (Sep 03, 2009)
============================ ============================

View File

@ -1015,11 +1015,14 @@ class AttributeDocumenter(ClassLevelDocumenter):
# some non-data descriptors as methods # some non-data descriptors as methods
priority = 10 priority = 10
method_types = (FunctionType, BuiltinFunctionType, MethodType)
@classmethod @classmethod
def can_document_member(cls, member, membername, isattr, parent): def can_document_member(cls, member, membername, isattr, parent):
return (isdescriptor(member) and not isdatadesc = isdescriptor(member) and not \
isinstance(member, (FunctionType, BuiltinFunctionType))) \ isinstance(member, cls.method_types)
or (not isinstance(parent, ModuleDocumenter) and isattr) return isdatadesc or \
(isattr and not isinstance(parent, ModuleDocumenter))
def document_members(self, all_members=False): def document_members(self, all_members=False):
pass pass

View File

@ -379,6 +379,7 @@ def test_generate():
options.exclude_members = set(['excludemeth']) options.exclude_members = set(['excludemeth'])
assert_processes(should, 'class', 'Class') assert_processes(should, 'class', 'Class')
should.extend([('attribute', 'test_autodoc.Class.prop'), should.extend([('attribute', 'test_autodoc.Class.prop'),
('attribute', 'test_autodoc.Class.descr'),
('attribute', 'test_autodoc.Class.attr'), ('attribute', 'test_autodoc.Class.attr'),
('attribute', 'test_autodoc.Class.docattr'), ('attribute', 'test_autodoc.Class.docattr'),
('attribute', 'test_autodoc.Class.udocattr')]) ('attribute', 'test_autodoc.Class.udocattr')])
@ -427,6 +428,10 @@ def test_generate():
('method', 'test_autodoc.Outer.Inner.meth')], ('method', 'test_autodoc.Outer.Inner.meth')],
'class', 'Outer', all_members=True) 'class', 'Outer', all_members=True)
# test descriptor docstrings
assert_result_contains(' Descriptor instance docstring.',
'attribute', 'test_autodoc.Class.descr')
# test generation for C modules (which have no source file) # test generation for C modules (which have no source file)
directive.env.currmodule = 'time' directive.env.currmodule = 'time'
assert_processes([('function', 'time.asctime')], 'function', 'asctime') assert_processes([('function', 'time.asctime')], 'function', 'asctime')
@ -445,6 +450,17 @@ class CustomEx(Exception):
def f(self): def f(self):
"""Exception method.""" """Exception method."""
class CustomDataDescriptor(object):
"""Descriptor class docstring."""
def __init__(self, doc):
self.__doc__ = doc
def __get__(self, obj, type=None):
if obj is None:
return self
return 42
class Base(object): class Base(object):
def inheritedmeth(self): def inheritedmeth(self):
"""Inherited function.""" """Inherited function."""
@ -452,6 +468,8 @@ class Base(object):
class Class(Base): class Class(Base):
"""Class to document.""" """Class to document."""
descr = CustomDataDescriptor("Descriptor instance docstring.")
def meth(self): def meth(self):
"""Function.""" """Function."""