Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ The semantic versioning only considers the public API as described in
paths are considered internals and can change in minor and patch releases.


v4.32.1 (2024-08-??)
--------------------

Fixed
^^^^^
- ``dict`` types not correctly forwarding previous nested values when parsing
(`#559 <https://github.com/omni-us/jsonargparse/pull/559>`__).


v4.32.0 (2024-07-19)
--------------------

Expand Down
7 changes: 6 additions & 1 deletion jsonargparse/_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,12 @@ def adapt_typehints(
for t in sub_add_kwargs["linked_targets"]
}
else:
kwargs = adapt_kwargs
kwargs = adapt_kwargs.copy()
if kwargs.get("prev_val"):
if isinstance(kwargs["prev_val"], dict):
kwargs["prev_val"] = kwargs["prev_val"].get(k)
else:
kwargs["prev_val"] = None
val[k] = adapt_typehints(v, subtypehints[1], **kwargs)
if get_import_path(typehint.__class__) == "typing._TypedDictMeta":
if typehint.__total__:
Expand Down
25 changes: 25 additions & 0 deletions jsonargparse_tests/test_dataclass_like.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import dataclasses
import json
from typing import Any, Dict, Generic, List, Optional, Tuple, TypeVar, Union
from unittest.mock import patch

Expand Down Expand Up @@ -684,6 +685,13 @@ class OptionalPydantic:
def __init__(self, a: Optional[PydanticModel] = None):
self.a = a

class NestedModel(pydantic.BaseModel):
inputs: List[str]
outputs: List[str]

class PydanticNestedDict(pydantic.BaseModel):
nested: Optional[Dict[str, NestedModel]] = None


def none(x):
return x
Expand Down Expand Up @@ -817,6 +825,23 @@ def test_optional_pydantic_model(self, parser):
assert cfg.b.class_path == f"{__name__}.OptionalPydantic"
assert cfg.b.init_args == Namespace(a=Namespace(p1="x", p2=3))

def test_nested_dict(self, parser):
parser.add_argument("--config", action="config")
parser.add_argument("--model", type=PydanticNestedDict)
model = {
"nested": {
"key": {
"inputs": ["a", "b"],
"outputs": ["x", "y"],
}
}
}
cfg = parser.parse_args(["--model", json.dumps(model)])
assert cfg.model.nested["key"] == Namespace(inputs=["a", "b"], outputs=["x", "y"])
init = parser.instantiate_classes(cfg)
assert isinstance(init.model, PydanticNestedDict)
assert isinstance(init.model.nested["key"], NestedModel)


# attrs tests

Expand Down