test: Move class inheritance samples into target.inheritance module

This commit is contained in:
Takeshi KOMIYA 2019-06-03 22:34:33 +09:00
parent 806fa22f34
commit 42e9fd896e
3 changed files with 40 additions and 41 deletions

View File

@ -34,26 +34,7 @@ def _funky_classmethod(name, b, c, d, docstring=None):
return classmethod(function) return classmethod(function)
class Base(object): class Class(object):
def inheritedmeth(self):
"""Inherited function."""
@classmethod
def inheritedclassmeth(cls):
"""Inherited class method."""
@staticmethod
def inheritedstaticmeth(cls):
"""Inherited static method."""
class Derived(Base):
def inheritedmeth(self):
# no docstring here
pass
class Class(Base):
"""Class to document.""" """Class to document."""
def meth(self): def meth(self):

View File

@ -0,0 +1,19 @@
class Base(object):
def inheritedmeth(self):
"""Inherited function."""
@classmethod
def inheritedclassmeth(cls):
"""Inherited class method."""
@staticmethod
def inheritedstaticmeth(cls):
"""Inherited static method."""
class Derived(Base):
def inheritedmeth(self):
# no docstring here
pass

View File

@ -454,7 +454,7 @@ def test_get_doc():
assert getdocl('method', J.foo) == ['Method docstring'] assert getdocl('method', J.foo) == ['Method docstring']
assert getdocl('function', J().foo) == ['Method docstring'] assert getdocl('function', J().foo) == ['Method docstring']
from target import Base, Derived from target.inheritance import Base, Derived
# NOTE: inspect.getdoc seems not to work with locally defined classes # NOTE: inspect.getdoc seems not to work with locally defined classes
directive.env.config.autodoc_inherit_docstrings = False directive.env.config.autodoc_inherit_docstrings = False
@ -498,6 +498,7 @@ def test_new_documenter(app):
@pytest.mark.usefixtures('setup_test') @pytest.mark.usefixtures('setup_test')
def test_attrgetter_using(): def test_attrgetter_using():
from target import Class from target import Class
from target.inheritance import Derived
def assert_getter_works(objtype, name, obj, attrs=[], **kw): def assert_getter_works(objtype, name, obj, attrs=[], **kw):
getattr_spy = [] getattr_spy = []
@ -527,7 +528,7 @@ def test_attrgetter_using():
assert_getter_works('class', 'target.Class', Class, ['meth']) assert_getter_works('class', 'target.Class', Class, ['meth'])
options.inherited_members = True options.inherited_members = True
assert_getter_works('class', 'target.Class', Class, ['meth', 'inheritedmeth']) assert_getter_works('class', 'target.inheritance.Derived', Derived, ['inheritedmeth'])
@pytest.mark.sphinx('html', testroot='ext-autodoc') @pytest.mark.sphinx('html', testroot='ext-autodoc')
@ -630,14 +631,14 @@ def test_autodoc_attributes(app):
@pytest.mark.sphinx('html', testroot='ext-autodoc') @pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_autodoc_members(app): def test_autodoc_members(app):
# default (no-members) # default (no-members)
actual = do_autodoc(app, 'class', 'target.Base') actual = do_autodoc(app, 'class', 'target.inheritance.Base')
assert list(filter(lambda l: '::' in l, actual)) == [ assert list(filter(lambda l: '::' in l, actual)) == [
'.. py:class:: Base', '.. py:class:: Base',
] ]
# default ALL-members # default ALL-members
options = {"members": None} options = {"members": None}
actual = do_autodoc(app, 'class', 'target.Base', options) actual = do_autodoc(app, 'class', 'target.inheritance.Base', options)
assert list(filter(lambda l: '::' in l, actual)) == [ assert list(filter(lambda l: '::' in l, actual)) == [
'.. py:class:: Base', '.. py:class:: Base',
' .. py:method:: Base.inheritedclassmeth()', ' .. py:method:: Base.inheritedclassmeth()',
@ -647,7 +648,7 @@ def test_autodoc_members(app):
# default specific-members # default specific-members
options = {"members": "inheritedmeth,inheritedstaticmeth"} options = {"members": "inheritedmeth,inheritedstaticmeth"}
actual = do_autodoc(app, 'class', 'target.Base', options) actual = do_autodoc(app, 'class', 'target.inheritance.Base', options)
assert list(filter(lambda l: '::' in l, actual)) == [ assert list(filter(lambda l: '::' in l, actual)) == [
'.. py:class:: Base', '.. py:class:: Base',
' .. py:method:: Base.inheritedmeth()', ' .. py:method:: Base.inheritedmeth()',
@ -659,7 +660,7 @@ def test_autodoc_members(app):
def test_autodoc_exclude_members(app): def test_autodoc_exclude_members(app):
options = {"members": None, options = {"members": None,
"exclude-members": "inheritedmeth,inheritedstaticmeth"} "exclude-members": "inheritedmeth,inheritedstaticmeth"}
actual = do_autodoc(app, 'class', 'target.Base', options) actual = do_autodoc(app, 'class', 'target.inheritance.Base', options)
assert list(filter(lambda l: '::' in l, actual)) == [ assert list(filter(lambda l: '::' in l, actual)) == [
'.. py:class:: Base', '.. py:class:: Base',
' .. py:method:: Base.inheritedclassmeth()' ' .. py:method:: Base.inheritedclassmeth()'
@ -668,7 +669,7 @@ def test_autodoc_exclude_members(app):
# members vs exclude-members # members vs exclude-members
options = {"members": "inheritedmeth", options = {"members": "inheritedmeth",
"exclude-members": "inheritedmeth"} "exclude-members": "inheritedmeth"}
actual = do_autodoc(app, 'class', 'target.Base', options) actual = do_autodoc(app, 'class', 'target.inheritance.Base', options)
assert list(filter(lambda l: '::' in l, actual)) == [ assert list(filter(lambda l: '::' in l, actual)) == [
'.. py:class:: Base', '.. py:class:: Base',
] ]
@ -702,15 +703,11 @@ def test_autodoc_undoc_members(app):
def test_autodoc_inherited_members(app): def test_autodoc_inherited_members(app):
options = {"members": None, options = {"members": None,
"inherited-members": None} "inherited-members": None}
actual = do_autodoc(app, 'class', 'target.Class', options) actual = do_autodoc(app, 'class', 'target.inheritance.Derived', options)
assert list(filter(lambda l: 'method::' in l, actual)) == [ assert list(filter(lambda l: 'method::' in l, actual)) == [
' .. py:method:: Class.excludemeth()', ' .. py:method:: Derived.inheritedclassmeth()',
' .. py:method:: Class.inheritedclassmeth()', ' .. py:method:: Derived.inheritedmeth()',
' .. py:method:: Class.inheritedmeth()', ' .. py:method:: Derived.inheritedstaticmeth(cls)',
' .. py:method:: Class.inheritedstaticmeth(cls)',
' .. py:method:: Class.meth()',
' .. py:method:: Class.moore(a, e, f) -> happiness',
' .. py:method:: Class.skipmeth()'
] ]
@ -755,10 +752,12 @@ def test_autodoc_special_members(app):
actual = do_autodoc(app, 'class', 'target.Class', options) actual = do_autodoc(app, 'class', 'target.Class', options)
assert list(filter(lambda l: '::' in l, actual)) == [ assert list(filter(lambda l: '::' in l, actual)) == [
'.. py:class:: Class(arg)', '.. py:class:: Class(arg)',
' .. py:attribute:: Class.__dict__',
' .. py:method:: Class.__init__(arg)', ' .. py:method:: Class.__init__(arg)',
' .. py:attribute:: Class.__module__', ' .. py:attribute:: Class.__module__',
' .. py:method:: Class.__special1__()', ' .. py:method:: Class.__special1__()',
' .. py:method:: Class.__special2__()', ' .. py:method:: Class.__special2__()',
' .. py:attribute:: Class.__weakref__',
' .. py:attribute:: Class.attr', ' .. py:attribute:: Class.attr',
' .. py:attribute:: Class.docattr', ' .. py:attribute:: Class.docattr',
' .. py:method:: Class.excludemeth()', ' .. py:method:: Class.excludemeth()',
@ -812,12 +811,12 @@ def test_autodoc_noindex(app):
# TODO: :noindex: should be propagated to children of target item. # TODO: :noindex: should be propagated to children of target item.
actual = do_autodoc(app, 'class', 'target.Base', options) actual = do_autodoc(app, 'class', 'target.inheritance.Base', options)
assert list(actual) == [ assert list(actual) == [
'', '',
'.. py:class:: Base', '.. py:class:: Base',
' :noindex:', ' :noindex:',
' :module: target', ' :module: target.inheritance',
'' ''
] ]
@ -885,11 +884,11 @@ def test_autodoc_inner_class(app):
@pytest.mark.sphinx('html', testroot='ext-autodoc') @pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_autodoc_classmethod(app): def test_autodoc_classmethod(app):
actual = do_autodoc(app, 'method', 'target.Base.inheritedclassmeth') actual = do_autodoc(app, 'method', 'target.inheritance.Base.inheritedclassmeth')
assert list(actual) == [ assert list(actual) == [
'', '',
'.. py:method:: Base.inheritedclassmeth()', '.. py:method:: Base.inheritedclassmeth()',
' :module: target', ' :module: target.inheritance',
' :classmethod:', ' :classmethod:',
'', '',
' Inherited class method.', ' Inherited class method.',
@ -899,11 +898,11 @@ def test_autodoc_classmethod(app):
@pytest.mark.sphinx('html', testroot='ext-autodoc') @pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_autodoc_staticmethod(app): def test_autodoc_staticmethod(app):
actual = do_autodoc(app, 'method', 'target.Base.inheritedstaticmeth') actual = do_autodoc(app, 'method', 'target.inheritance.Base.inheritedstaticmeth')
assert list(actual) == [ assert list(actual) == [
'', '',
'.. py:method:: Base.inheritedstaticmeth(cls)', '.. py:method:: Base.inheritedstaticmeth(cls)',
' :module: target', ' :module: target.inheritance',
' :staticmethod:', ' :staticmethod:',
'', '',
' Inherited static method.', ' Inherited static method.',