Skip to content

Commit 966d78d

Browse files
authored
Fix getting parameter descriptions from docstrings for dataclass inheritance (#815)
1 parent ac2e8d2 commit 966d78d

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Fixed
2020
- Evaluation of postponed annotations for dataclass inheritance across modules
2121
not working correctly (`#814
2222
<https://github.com/omni-us/jsonargparse/pull/814>`__).
23+
- Getting parameter descriptions from docstrings not working for dataclass
24+
inheritance (`#815 <https://github.com/omni-us/jsonargparse/pull/815>`__).
2325

2426

2527
v4.44.0 (2025-11-25)

jsonargparse/_optionals.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import re
66
from contextlib import contextmanager
77
from copy import deepcopy
8+
from dataclasses import is_dataclass
89
from importlib.metadata import version
910
from importlib.util import find_spec
1011
from typing import Optional, Union
@@ -242,6 +243,10 @@ def parse_docstring(component, params=False, logger=None):
242243
def parse_docs(component, parent, logger):
243244
docs = {}
244245
if docstring_parser_support:
246+
if is_dataclass(parent) and component.__name__ == "__init__":
247+
next_mro = inspect.getmro(parent)[1]
248+
if is_dataclass(next_mro):
249+
docs.update(parse_docs(next_mro, next_mro.__init__, logger))
245250
doc_sources = [component]
246251
if inspect.isclass(parent) and component.__name__ == "__init__":
247252
doc_sources += [parent]

jsonargparse_tests/test_dataclasses.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737

3838
@dataclasses.dataclass
3939
class DifferentModuleBaseData:
40+
"""
41+
Args:
42+
count: between 3 and 9
43+
numbers: list of positive ints
44+
"""
45+
4046
count: Optional[BetweenThreeAndNine] = None # type: ignore[valid-type]
4147
numbers: ListPositiveInt = dataclasses.field(default_factory=list)
4248

jsonargparse_tests/test_postponed_annotations.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99

1010
from jsonargparse import Namespace
11+
from jsonargparse._optionals import docstring_parser_support
1112
from jsonargparse._parameter_resolvers import get_signature_parameters as get_params
1213
from jsonargparse._postponed_annotations import (
1314
TypeCheckingVisitor,
@@ -329,6 +330,11 @@ def test_add_dataclass_with_init_pep585(parser, tmp_cwd):
329330

330331
@dataclasses.dataclass
331332
class InheritDifferentModule(DifferentModuleBaseData):
333+
"""
334+
Args:
335+
extra: an extra string
336+
"""
337+
332338
extra: str = "default"
333339

334340

@@ -339,6 +345,8 @@ def test_get_params_dataclass_inherit_different_module():
339345
params = get_params(InheritDifferentModule)
340346

341347
assert [p.name for p in params] == ["count", "numbers", "extra"]
348+
if docstring_parser_support:
349+
assert [p.doc for p in params] == ["between 3 and 9", "list of positive ints", "an extra string"]
342350
assert all(not isinstance(p.annotation, str) for p in params)
343351
assert not isinstance(params[0].annotation.__args__[0], str)
344352
assert "BetweenThreeAndNine" in str(params[0].annotation)

0 commit comments

Comments
 (0)