Skip to content

Commit

Permalink
autodoc: blacklist typing.Generic.__new__
Browse files Browse the repository at this point in the history
When documenting classes derived from typing.Generic (essentially all classes in the
typing module) the constructor signature would show an unhelpful (*args, **kwds).

typing.Generic has a __new__ method which was picked up by sphinx. With this patch it
is skipped and constructor signatures for generic classes are shown as they should.
  • Loading branch information
jcarrano committed Aug 21, 2020
1 parent 6426861 commit 740be7f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
11 changes: 11 additions & 0 deletions sphinx/ext/autodoc/__init__.py
Expand Up @@ -1317,6 +1317,12 @@ def format_args(self, **kwargs: Any) -> Any:
]


# 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.
Expand Down Expand Up @@ -1385,6 +1391,11 @@ def get_user_defined_function_or_method(obj: Any, attr: str) -> Any:

# 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:
Expand Down
1 change: 0 additions & 1 deletion tests/test_ext_autodoc.py
Expand Up @@ -291,7 +291,6 @@ def foo3(self, d='\n'):


@pytest.mark.skipif(sys.version_info < (3, 5), reason='typing is available since python3.5.')
@pytest.mark.xfail
@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_autodoc_process_signature_typing_generic(app):
actual = do_autodoc(app, 'class', 'target.generic_class.A', {})
Expand Down

0 comments on commit 740be7f

Please sign in to comment.