Skip to content

Commit 38b61b4

Browse files
committed
Fixed inconsistent data class formatting
1 parent 25b43eb commit 38b61b4

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
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
* Dropped support for Sphinx < 3.0
55
* Fixed ``TypeError`` when formatting a parametrized ``typing.IO`` annotation
6+
* Fixed data class displaying a return type in its ``__init__()`` method
67

78

89
1.10.3

sphinx_autodoc_typehints.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,10 @@ def process_docstring(app, what, name, obj, options, lines):
391391
)
392392

393393
if 'return' in type_hints and not inspect.isclass(original_obj):
394+
# This avoids adding a return type for data class __init__ methods
395+
if what == 'method' and name.endswith('.__init__'):
396+
return
397+
394398
formatted_annotation = format_annotation(
395399
type_hints['return'], fully_qualified=app.config.typehints_fully_qualified)
396400

tests/test_sphinx_autodoc_typehints.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,6 @@ def test_sphinx_output(app, status, warning, always_document_param_types):
228228
else:
229229
format_args['undoc_params'] = ""
230230

231-
if sys.version_info < (3, 6):
232-
format_args['dataclass_docstring'] = ('Initialize self. See help(type(self)) for '
233-
'accurate signature.')
234-
else:
235-
format_args['dataclass_docstring'] = 'Return type:\n "None"'
236-
237231
text_path = pathlib.Path(app.srcdir) / '_build' / 'text' / 'index.txt'
238232
with text_path.open('r') as f:
239233
text_contents = f.read().replace('–', '--')
@@ -486,7 +480,7 @@ class dummy_module.DataClass
486480
487481
__init__()
488482
489-
{dataclass_docstring}
483+
Initialize self. See help(type(self)) for accurate signature.
490484
491485
@dummy_module.Decorator(func)
492486

0 commit comments

Comments
 (0)