mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
refactor: autodoc: class processors on autofunction is no longer needed
This commit is contained in:
parent
6ce265dc81
commit
24fe05f14f
@ -1049,23 +1049,7 @@ class FunctionDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # typ
|
|||||||
sig = inspect.signature(unwrapped)
|
sig = inspect.signature(unwrapped)
|
||||||
args = stringify_signature(sig, **kwargs)
|
args = stringify_signature(sig, **kwargs)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
if (inspect.is_builtin_class_method(unwrapped, '__new__') and
|
args = ''
|
||||||
inspect.is_builtin_class_method(unwrapped, '__init__')):
|
|
||||||
raise TypeError('%r is a builtin class' % unwrapped)
|
|
||||||
|
|
||||||
# if a class should be documented as function (yay duck
|
|
||||||
# typing) we try to use the constructor signature as function
|
|
||||||
# signature without the first argument.
|
|
||||||
try:
|
|
||||||
self.env.app.emit('autodoc-before-process-signature',
|
|
||||||
unwrapped.__new__, True)
|
|
||||||
sig = inspect.signature(unwrapped.__new__, bound_method=True)
|
|
||||||
args = stringify_signature(sig, show_return_annotation=False, **kwargs)
|
|
||||||
except TypeError:
|
|
||||||
self.env.app.emit('autodoc-before-process-signature',
|
|
||||||
unwrapped.__init__, True)
|
|
||||||
sig = inspect.signature(unwrapped.__init__, bound_method=True)
|
|
||||||
args = stringify_signature(sig, show_return_annotation=False, **kwargs)
|
|
||||||
|
|
||||||
if self.env.config.strip_signature_backslash:
|
if self.env.config.strip_signature_backslash:
|
||||||
# escape backslashes for reST
|
# escape backslashes for reST
|
||||||
|
@ -402,12 +402,6 @@ def signature(subject: Callable, bound_method: bool = False) -> inspect.Signatur
|
|||||||
|
|
||||||
:param bound_method: Specify *subject* is a bound method or not
|
:param bound_method: Specify *subject* is a bound method or not
|
||||||
"""
|
"""
|
||||||
# check subject is not a built-in class (ex. int, str)
|
|
||||||
if (isinstance(subject, type) and
|
|
||||||
is_builtin_class_method(subject, "__new__") and
|
|
||||||
is_builtin_class_method(subject, "__init__")):
|
|
||||||
raise TypeError("can't compute signature for built-in type {}".format(subject))
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
signature = inspect.signature(subject)
|
signature = inspect.signature(subject)
|
||||||
parameters = list(signature.parameters.values())
|
parameters = list(signature.parameters.values())
|
||||||
|
12
tests/roots/test-ext-autodoc/target/classes.py
Normal file
12
tests/roots/test-ext-autodoc/target/classes.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
class Foo:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Bar:
|
||||||
|
def __init__(self, x, y):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Baz:
|
||||||
|
def __new__(cls, x, y):
|
||||||
|
pass
|
@ -1164,6 +1164,33 @@ def test_descriptor_class(app):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.sphinx('html', testroot='ext-autodoc')
|
||||||
|
def test_autofunction_for_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',
|
||||||
|
'',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.sphinx('html', testroot='ext-autodoc')
|
@pytest.mark.sphinx('html', testroot='ext-autodoc')
|
||||||
def test_autofunction_for_callable(app):
|
def test_autofunction_for_callable(app):
|
||||||
actual = do_autodoc(app, 'function', 'target.callable.function')
|
actual = do_autodoc(app, 'function', 'target.callable.function')
|
||||||
|
@ -30,10 +30,10 @@ def test_signature():
|
|||||||
inspect.signature('')
|
inspect.signature('')
|
||||||
|
|
||||||
# builitin classes
|
# builitin classes
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(ValueError):
|
||||||
inspect.signature(int)
|
inspect.signature(int)
|
||||||
|
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(ValueError):
|
||||||
inspect.signature(str)
|
inspect.signature(str)
|
||||||
|
|
||||||
# normal function
|
# normal function
|
||||||
|
Loading…
Reference in New Issue
Block a user