mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #8355 from sphinx-doc/7613_class__signature__
Fix #7613: autodoc: autodoc does not respect __signature__ of the class
This commit is contained in:
commit
f46c14bbca
2
CHANGES
2
CHANGES
@ -41,6 +41,8 @@ Features added
|
|||||||
Bugs fixed
|
Bugs fixed
|
||||||
----------
|
----------
|
||||||
|
|
||||||
|
* #7613: autodoc: autodoc does not respect __signature__ of the class
|
||||||
|
|
||||||
Testing
|
Testing
|
||||||
--------
|
--------
|
||||||
|
|
||||||
|
@ -1391,7 +1391,12 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
|
|||||||
# This sequence is copied from inspect._signature_from_callable.
|
# This sequence is copied from inspect._signature_from_callable.
|
||||||
# ValueError means that no signature could be found, so we keep going.
|
# ValueError means that no signature could be found, so we keep going.
|
||||||
|
|
||||||
# First, let's see if it has an overloaded __call__ defined
|
# First, we check the obj has a __signature__ attribute
|
||||||
|
if (hasattr(self.object, '__signature__') and
|
||||||
|
isinstance(self.object.__signature__, Signature)):
|
||||||
|
return None, None, self.object.__signature__
|
||||||
|
|
||||||
|
# Next, let's see if it has an overloaded __call__ defined
|
||||||
# in its metaclass
|
# in its metaclass
|
||||||
call = get_user_defined_function_or_method(type(self.object), '__call__')
|
call = get_user_defined_function_or_method(type(self.object), '__call__')
|
||||||
|
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
from inspect import Parameter, Signature
|
||||||
|
|
||||||
|
|
||||||
class Foo:
|
class Foo:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -10,3 +13,11 @@ class Bar:
|
|||||||
class Baz:
|
class Baz:
|
||||||
def __new__(cls, x, y):
|
def __new__(cls, x, y):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Qux:
|
||||||
|
__signature__ = Signature(parameters=[Parameter('foo', Parameter.POSITIONAL_OR_KEYWORD),
|
||||||
|
Parameter('bar', Parameter.POSITIONAL_OR_KEYWORD)])
|
||||||
|
|
||||||
|
def __init__(self, x, y):
|
||||||
|
pass
|
||||||
|
50
tests/test_ext_autodoc_autoclass.py
Normal file
50
tests/test_ext_autodoc_autoclass.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
"""
|
||||||
|
test_ext_autodoc_autoclass
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Test the autodoc extension. This tests mainly the Documenters; the auto
|
||||||
|
directives are tested in a test source file translated by test_build.
|
||||||
|
|
||||||
|
:copyright: Copyright 2007-2020 by the Sphinx team, see AUTHORS.
|
||||||
|
:license: BSD, see LICENSE for details.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from test_ext_autodoc import do_autodoc
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.sphinx('html', testroot='ext-autodoc')
|
||||||
|
def test_classes(app):
|
||||||
|
actual = do_autodoc(app, 'function', 'target.classes.Foo')
|
||||||
|
assert list(actual) == [
|
||||||
|
'',
|
||||||
|
'.. py:function:: Foo()',
|
||||||
|
' :module: target.classes',
|
||||||
|
'',
|
||||||
|
]
|
||||||
|
|
||||||
|
actual = do_autodoc(app, 'function', 'target.classes.Bar')
|
||||||
|
assert list(actual) == [
|
||||||
|
'',
|
||||||
|
'.. py:function:: Bar(x, y)',
|
||||||
|
' :module: target.classes',
|
||||||
|
'',
|
||||||
|
]
|
||||||
|
|
||||||
|
actual = do_autodoc(app, 'function', 'target.classes.Baz')
|
||||||
|
assert list(actual) == [
|
||||||
|
'',
|
||||||
|
'.. py:function:: Baz(x, y)',
|
||||||
|
' :module: target.classes',
|
||||||
|
'',
|
||||||
|
]
|
||||||
|
|
||||||
|
actual = do_autodoc(app, 'function', 'target.classes.Qux')
|
||||||
|
assert list(actual) == [
|
||||||
|
'',
|
||||||
|
'.. py:function:: Qux(foo, bar)',
|
||||||
|
' :module: target.classes',
|
||||||
|
'',
|
||||||
|
]
|
||||||
|
|
@ -42,6 +42,14 @@ def test_classes(app):
|
|||||||
'',
|
'',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
actual = do_autodoc(app, 'function', 'target.classes.Qux')
|
||||||
|
assert list(actual) == [
|
||||||
|
'',
|
||||||
|
'.. py:function:: Qux(foo, bar)',
|
||||||
|
' :module: target.classes',
|
||||||
|
'',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.sphinx('html', testroot='ext-autodoc')
|
@pytest.mark.sphinx('html', testroot='ext-autodoc')
|
||||||
def test_callable(app):
|
def test_callable(app):
|
||||||
|
Loading…
Reference in New Issue
Block a user