diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-10-03 01:23:36 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-03 01:23:36 +0900 |
commit | ec06f5571ede25613aca90c7cfe41519c925533a (patch) | |
tree | 10c35034b3e4fe6ce33ca0e0c0ab1556338a55d1 | |
parent | bbf0754db1d0a8961a467621fa2964df5afb8ffd (diff) | |
parent | 740be7f2a5c03c0bbfd96420843074c1c5cb6c10 (diff) | |
download | sphinx-git-ec06f5571ede25613aca90c7cfe41519c925533a.tar.gz |
Merge pull request #8142 from jcarrano/typing-generic-signatures
autodoc: fix constructor signatures for classes derived from typing.Generic
-rw-r--r-- | sphinx/ext/autodoc/__init__.py | 11 | ||||
-rw-r--r-- | tests/roots/test-ext-autodoc/target/generic_class.py | 12 | ||||
-rw-r--r-- | tests/test_ext_autodoc.py | 15 |
3 files changed, 38 insertions, 0 deletions
diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index 23fb43a4d..35bb1c90d 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -1325,6 +1325,12 @@ _METACLASS_CALL_BLACKLIST = [ ] +# Types whose __new__ signature is a pass-thru. +_CLASS_NEW_BLACKLIST = [ + 'typing.Generic.__new__', +] + + class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type: ignore """ Specialized Documenter subclass for classes. @@ -1393,6 +1399,11 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type: # Now we check if the 'obj' class has a '__new__' method new = get_user_defined_function_or_method(self.object, '__new__') + + if new is not None: + if "{0.__module__}.{0.__qualname__}".format(new) in _CLASS_NEW_BLACKLIST: + new = None + if new is not None: self.env.app.emit('autodoc-before-process-signature', new, True) try: diff --git a/tests/roots/test-ext-autodoc/target/generic_class.py b/tests/roots/test-ext-autodoc/target/generic_class.py new file mode 100644 index 000000000..cf4c5ed37 --- /dev/null +++ b/tests/roots/test-ext-autodoc/target/generic_class.py @@ -0,0 +1,12 @@ +from typing import TypeVar, Generic + + +T = TypeVar('T') + + +# Test that typing.Generic's __new__ method does not mask our class' +# __init__ signature. +class A(Generic[T]): + """docstring for A""" + def __init__(self, a, b=None): + pass diff --git a/tests/test_ext_autodoc.py b/tests/test_ext_autodoc.py index 1ba64a0a7..7fff09bb6 100644 --- a/tests/test_ext_autodoc.py +++ b/tests/test_ext_autodoc.py @@ -290,6 +290,21 @@ def test_format_signature(app): '(b, c=42, *d, **e)' +@pytest.mark.skipif(sys.version_info < (3, 5), reason='typing is available since python3.5.') +@pytest.mark.sphinx('html', testroot='ext-autodoc') +def test_autodoc_process_signature_typing_generic(app): + actual = do_autodoc(app, 'class', 'target.generic_class.A', {}) + + assert list(actual) == [ + '', + '.. py:class:: A(a, b=None)', + ' :module: target.generic_class', + '', + ' docstring for A', + '', + ] + + def test_autodoc_process_signature_typehints(app): captured = [] |