Skip to content

Commit df20921

Browse files
authored
Fix dataclass with default failing when validate_defaults=True (#771)
1 parent c2c7aa4 commit df20921

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Fixed
2525
^^^^^
2626
- Stubs resolver in some cases failing with maximum recursion error (`#770
2727
<https://github.com/omni-us/jsonargparse/pull/770>`__).
28+
- ``dataclass`` with default failing when ``validate_defaults=True`` (`#771
29+
<https://github.com/omni-us/jsonargparse/pull/771>`__).
2830

2931

3032
v4.41.0 (2025-09-04)

jsonargparse/_common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ def validate_default(container: ActionsContainer, action: argparse.Action):
171171
if action.default is None or not get_parsing_setting("validate_defaults") or not hasattr(action, "_check_type"):
172172
return
173173
try:
174+
from ._core import ArgumentGroup
175+
176+
if isinstance(container, ArgumentGroup):
177+
container = container.parser # type: ignore[assignment]
174178
with parser_context(parent_parser=container):
175179
default = action.default
176180
action.default = None

jsonargparse_tests/test_parsing_settings.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ def test_validate_defaults_failure(parser):
4545
parser.add_argument("--num", type=int, default="x")
4646

4747

48+
@dataclass
49+
class DataWithDefault:
50+
param: str = "foo"
51+
52+
53+
def test_validate_defaults_dataclass(parser):
54+
set_parsing_settings(validate_defaults=True)
55+
56+
added_args = parser.add_class_arguments(DataWithDefault)
57+
assert added_args == ["param"]
58+
59+
4860
# parse_optionals_as_positionals
4961

5062

0 commit comments

Comments
 (0)