From f04ca65690f1d1f63965f24b3b9bd4250f13cc33 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sun, 25 Oct 2009 11:26:10 +0100 Subject: [PATCH] Fix "fixed" descriptor handling, and add tests. --- CHANGES | 2 ++ sphinx/ext/autodoc.py | 9 ++++++--- tests/test_autodoc.py | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index cf7afdb33..c00bbe008 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,8 @@ Release 0.6.4 (in development) ============================== +* Fix the handling of non-data descriptors in autodoc. + Release 0.6.3 (Sep 03, 2009) ============================ diff --git a/sphinx/ext/autodoc.py b/sphinx/ext/autodoc.py index 012244f1e..f5b988789 100644 --- a/sphinx/ext/autodoc.py +++ b/sphinx/ext/autodoc.py @@ -1015,11 +1015,14 @@ class AttributeDocumenter(ClassLevelDocumenter): # some non-data descriptors as methods priority = 10 + method_types = (FunctionType, BuiltinFunctionType, MethodType) + @classmethod def can_document_member(cls, member, membername, isattr, parent): - return (isdescriptor(member) and not - isinstance(member, (FunctionType, BuiltinFunctionType))) \ - or (not isinstance(parent, ModuleDocumenter) and isattr) + isdatadesc = isdescriptor(member) and not \ + isinstance(member, cls.method_types) + return isdatadesc or \ + (isattr and not isinstance(parent, ModuleDocumenter)) def document_members(self, all_members=False): pass diff --git a/tests/test_autodoc.py b/tests/test_autodoc.py index 8e0114386..687fbd0d7 100644 --- a/tests/test_autodoc.py +++ b/tests/test_autodoc.py @@ -379,6 +379,7 @@ def test_generate(): options.exclude_members = set(['excludemeth']) assert_processes(should, 'class', 'Class') should.extend([('attribute', 'test_autodoc.Class.prop'), + ('attribute', 'test_autodoc.Class.descr'), ('attribute', 'test_autodoc.Class.attr'), ('attribute', 'test_autodoc.Class.docattr'), ('attribute', 'test_autodoc.Class.udocattr')]) @@ -427,6 +428,10 @@ def test_generate(): ('method', 'test_autodoc.Outer.Inner.meth')], '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) directive.env.currmodule = 'time' assert_processes([('function', 'time.asctime')], 'function', 'asctime') @@ -445,6 +450,17 @@ class CustomEx(Exception): def f(self): """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): def inheritedmeth(self): """Inherited function.""" @@ -452,6 +468,8 @@ class Base(object): class Class(Base): """Class to document.""" + descr = CustomDataDescriptor("Descriptor instance docstring.") + def meth(self): """Function."""