Merge pull request #10285 from jmp1985/master

Fixed singledispatch documentation
This commit is contained in:
Takeshi KOMIYA 2022-04-05 02:30:53 +09:00 committed by GitHub
commit f5b3804060
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 78 additions and 29 deletions

View File

@ -1403,8 +1403,8 @@ class FunctionDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # typ
except (AttributeError, TypeError):
# failed to update signature (ex. built-in or extension types)
return None
else:
return None
return func
class DecoratorDocumenter(FunctionDocumenter):
@ -2299,8 +2299,8 @@ class MethodDocumenter(DocstringSignatureMixin, ClassLevelDocumenter): # type:
except (AttributeError, TypeError):
# failed to update signature (ex. built-in or extension types)
return None
else:
return None
return func
def get_doc(self, ignore: int = None) -> Optional[List[List[str]]]:
if self._new_docstrings is not None:

View File

@ -26,3 +26,11 @@ def _func_int(arg, kwarg=None):
def _func_str(arg, kwarg=None):
"""A function for str."""
pass
@func.register
def _func_dict(arg: dict, kwarg=None):
"""A function for dict."""
# This function tests for specifying type through annotations
pass

View File

@ -19,3 +19,9 @@ class Foo:
def _meth_str(self, arg, kwarg=None):
"""A method for str."""
pass
@meth.register
def _meth_dict(self, arg: dict, kwarg=None):
"""A method for dict."""
# This function tests for specifying type through annotations
pass

View File

@ -2064,20 +2064,37 @@ def test_autodoc_for_egged_code(app):
def test_singledispatch(app):
options = {"members": None}
actual = do_autodoc(app, 'module', 'target.singledispatch', options)
assert list(actual) == [
'',
'.. py:module:: target.singledispatch',
'',
'',
'.. py:function:: func(arg, kwarg=None)',
' func(arg: float, kwarg=None)',
' func(arg: int, kwarg=None)',
' func(arg: str, kwarg=None)',
' :module: target.singledispatch',
'',
' A function for general use.',
'',
]
if sys.version_info < (3, 7):
assert list(actual) == [
'',
'.. py:module:: target.singledispatch',
'',
'',
'.. py:function:: func(arg, kwarg=None)',
' func(arg: float, kwarg=None)',
' func(arg: int, kwarg=None)',
' func(arg: str, kwarg=None)',
' :module: target.singledispatch',
'',
' A function for general use.',
'',
]
else:
assert list(actual) == [
'',
'.. py:module:: target.singledispatch',
'',
'',
'.. py:function:: func(arg, kwarg=None)',
' func(arg: float, kwarg=None)',
' func(arg: int, kwarg=None)',
' func(arg: str, kwarg=None)',
' func(arg: dict, kwarg=None)',
' :module: target.singledispatch',
'',
' A function for general use.',
'',
]
@pytest.mark.skipif(sys.version_info < (3, 8),
@ -2101,6 +2118,7 @@ def test_singledispatchmethod(app):
' Foo.meth(arg: float, kwarg=None)',
' Foo.meth(arg: int, kwarg=None)',
' Foo.meth(arg: str, kwarg=None)',
' Foo.meth(arg: dict, kwarg=None)',
' :module: target.singledispatchmethod',
'',
' A method for general use.',
@ -2120,6 +2138,7 @@ def test_singledispatchmethod_automethod(app):
' Foo.meth(arg: float, kwarg=None)',
' Foo.meth(arg: int, kwarg=None)',
' Foo.meth(arg: str, kwarg=None)',
' Foo.meth(arg: dict, kwarg=None)',
' :module: target.singledispatchmethod',
'',
' A method for general use.',

View File

@ -4,6 +4,8 @@ This tests mainly the Documenters; the auto directives are tested in a test
source file translated by test_build.
"""
import sys
import pytest
from .test_ext_autodoc import do_autodoc
@ -111,17 +113,31 @@ def test_decorated(app):
def test_singledispatch(app):
options = {}
actual = do_autodoc(app, 'function', 'target.singledispatch.func', options)
assert list(actual) == [
'',
'.. py:function:: func(arg, kwarg=None)',
' func(arg: float, kwarg=None)',
' func(arg: int, kwarg=None)',
' func(arg: str, kwarg=None)',
' :module: target.singledispatch',
'',
' A function for general use.',
'',
]
if sys.version_info < (3, 7):
assert list(actual) == [
'',
'.. py:function:: func(arg, kwarg=None)',
' func(arg: float, kwarg=None)',
' func(arg: int, kwarg=None)',
' func(arg: str, kwarg=None)',
' :module: target.singledispatch',
'',
' A function for general use.',
'',
]
else:
assert list(actual) == [
'',
'.. py:function:: func(arg, kwarg=None)',
' func(arg: float, kwarg=None)',
' func(arg: int, kwarg=None)',
' func(arg: str, kwarg=None)',
' func(arg: dict, kwarg=None)',
' :module: target.singledispatch',
'',
' A function for general use.',
'',
]
@pytest.mark.sphinx('html', testroot='ext-autodoc')