diff --git a/jsonargparse/_core.py b/jsonargparse/_core.py index 83eae07b..f9f10098 100644 --- a/jsonargparse/_core.py +++ b/jsonargparse/_core.py @@ -801,11 +801,11 @@ def _dump_cleanup_actions(self, cfg, actions, dump_kwargs, prefix=""): value = cfg.get(action_dest) if value is not None: with parser_context(parent_parser=self): - try: + if dump_kwargs.get("skip_validation"): + with suppress(ValueError): + value = action.serialize(value, dump_kwargs=dump_kwargs) + else: value = action.serialize(value, dump_kwargs=dump_kwargs) - except Exception: - if not dump_kwargs.get("skip_validation"): - raise cfg.update(value, action_dest) def _dump_delete_default_entries(self, subcfg, subdefaults): diff --git a/jsonargparse_tests/test_core.py b/jsonargparse_tests/test_core.py index 2fefc4e2..315e25bb 100644 --- a/jsonargparse_tests/test_core.py +++ b/jsonargparse_tests/test_core.py @@ -499,6 +499,22 @@ def test_dump_skip_default_nested(parser): assert dump == expected +def test_dump_skip_validation(parser): + parser.add_argument("--key", type=int) + cfg = Namespace(key="-") + with pytest.raises(TypeError): + parser.dump(cfg) + dump = parser.dump(cfg, skip_validation=True) + assert "-" in dump + + +def test_dump_unexpected_kwarg(parser): + parser.add_argument("--key", type=int) + cfg = Namespace(key="-") + with pytest.raises(ValueError, match="Unexpected keyword parameter"): + parser.dump(cfg, unexpected=True) + + @skip_if_no_pyyaml def test_dump_order(parser, subtests): args = {} diff --git a/jsonargparse_tests/test_deprecated.py b/jsonargparse_tests/test_deprecated.py index 111c5e12..ddc73483 100644 --- a/jsonargparse_tests/test_deprecated.py +++ b/jsonargparse_tests/test_deprecated.py @@ -352,7 +352,7 @@ def test_deprecated_skip_check_method(parser): ) -def test_deprecated_skip_check_parameter(parser): +def test_deprecated_dump_skip_check_parameter(parser): parser.add_argument("--key", type=int) cfg = Namespace(key="-") with catch_warnings(record=True) as w: @@ -363,8 +363,6 @@ def test_deprecated_skip_check_parameter(parser): message="skip_check parameter was deprecated", code="parser.dump(cfg, skip_check=True)", ) - with pytest.raises(ValueError, match="Unexpected keyword parameters"): - parser.dump(cfg, unexpected=True) def test_ActionPath(tmp_cwd):