Skip to content

Commit 5bfeb5b

Browse files
committed
Fixed decorator classes not being processed as classes
Fixes #111.
1 parent 0d81051 commit 5bfeb5b

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ UNRELEASED
33

44
* Rewrote the annotation formatting logic (fixes Python 3.5.2 compatibility regressions and an
55
``AttributeError`` regression introduced in v1.9.0)
6+
* Fixed decorator classes not being processed as classes
67

78

89
1.9.0

sphinx_autodoc_typehints.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ def process_signature(app, what: str, name: str, obj, options, signature, return
147147
if not callable(obj):
148148
return
149149

150-
if what in ('class', 'exception'):
150+
original_obj = obj
151+
if inspect.isclass(obj):
151152
obj = getattr(obj, '__init__', getattr(obj, '__new__', None))
152153

153154
if not getattr(obj, '__annotations__', None):
@@ -167,7 +168,7 @@ def process_signature(app, what: str, name: str, obj, options, signature, return
167168
return
168169

169170
if parameters:
170-
if what in ('class', 'exception'):
171+
if inspect.isclass(original_obj):
171172
del parameters[0]
172173
elif what == 'method':
173174
outer = inspect.getmodule(obj)
@@ -346,11 +347,12 @@ def add(val):
346347

347348

348349
def process_docstring(app, what, name, obj, options, lines):
350+
original_obj = obj
349351
if isinstance(obj, property):
350352
obj = obj.fget
351353

352354
if callable(obj):
353-
if what in ('class', 'exception'):
355+
if inspect.isclass(obj):
354356
obj = getattr(obj, '__init__')
355357

356358
obj = inspect.unwrap(obj)
@@ -383,7 +385,7 @@ def process_docstring(app, what, name, obj, options, lines):
383385
':type {}: {}'.format(argname, formatted_annotation)
384386
)
385387

386-
if 'return' in type_hints and what not in ('class', 'exception'):
388+
if 'return' in type_hints and not inspect.isclass(original_obj):
387389
formatted_annotation = format_annotation(
388390
type_hints['return'], fully_qualified=app.config.typehints_fully_qualified)
389391

tests/roots/test-dummy/dummy_module.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,14 @@ def undocumented_function(x: int) -> str:
237237
@dataclass
238238
class DataClass:
239239
"""Class docstring."""
240+
241+
242+
class Decorator:
243+
"""
244+
Initializer docstring.
245+
246+
:param func: function
247+
"""
248+
249+
def __init__(self, func: Callable[[int, str], str]):
250+
pass

tests/roots/test-dummy/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ Dummy Module
3232
.. autoclass:: dummy_module.DataClass
3333
:undoc-members:
3434
:special-members: __init__
35+
36+
.. autodecorator:: dummy_module.Decorator

tests/test_sphinx_autodoc_typehints.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,17 @@ def test_sphinx_output(app, status, warning, always_document_param_types):
215215
warnings = warning.getvalue().strip()
216216
assert 'Cannot resolve forward reference in type annotations of ' in warnings, warnings
217217

218+
format_args = {}
218219
if always_document_param_types:
219-
undoc_params = '''
220-
221-
Parameters:
222-
**x** ("int") --'''
220+
format_args['undoc_params'] = '\n\n Parameters:\n **x** ("int") --'
221+
else:
222+
format_args['undoc_params'] = ""
223223

224+
if sys.version_info < (3, 6):
225+
format_args['dataclass_docstring'] = ('Initialize self. See help(type(self)) for '
226+
'accurate signature.')
224227
else:
225-
undoc_params = ""
228+
format_args['dataclass_docstring'] = 'Return type:\n "None"'
226229

227230
text_path = pathlib.Path(app.srcdir) / '_build' / 'text' / 'index.txt'
228231
with text_path.open('r') as f:
@@ -475,16 +478,15 @@ class dummy_module.DataClass
475478
Class docstring.
476479
477480
__init__()
478-
'''.format(undoc_params=undoc_params)).replace('–', '--')
479-
480-
if sys.version_info < (3, 6):
481-
expected_contents += '''
482-
Initialize self. See help(type(self)) for accurate signature.
483-
'''
484-
else:
485-
expected_contents += '''
486-
Return type:
487-
"None"
488-
'''
489481
482+
{dataclass_docstring}
483+
484+
@dummy_module.Decorator(func)
485+
486+
Initializer docstring.
487+
488+
Parameters:
489+
**func** ("Callable"[["int", "str"], "str"]) -- function
490+
''')
491+
expected_contents = expected_contents.format(**format_args).replace('–', '--')
490492
assert text_contents == expected_contents

0 commit comments

Comments
 (0)