From e82aa9f4d73a7c9a8609e83d9d54c025648661b7 Mon Sep 17 00:00:00 2001 From: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com> Date: Tue, 13 Aug 2024 09:29:48 +0200 Subject: [PATCH 1/2] Fix dict types not correctly forwarding previous nested values when parsing (#555). --- CHANGELOG.rst | 9 ++++++++ jsonargparse/_typehints.py | 7 ++++++- jsonargparse_tests/test_dataclass_like.py | 25 +++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index eeeff791..cbb8199b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 + (`#??? `__). + + v4.32.0 (2024-07-19) -------------------- diff --git a/jsonargparse/_typehints.py b/jsonargparse/_typehints.py index aec416d1..4c90a742 100644 --- a/jsonargparse/_typehints.py +++ b/jsonargparse/_typehints.py @@ -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__: diff --git a/jsonargparse_tests/test_dataclass_like.py b/jsonargparse_tests/test_dataclass_like.py index 9d884c4f..963eeaf6 100644 --- a/jsonargparse_tests/test_dataclass_like.py +++ b/jsonargparse_tests/test_dataclass_like.py @@ -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 @@ -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 @@ -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 From 4f311a9b28626483c4ad679ca164e098491eb559 Mon Sep 17 00:00:00 2001 From: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com> Date: Tue, 13 Aug 2024 09:34:25 +0200 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index cbb8199b..e531ed73 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -18,7 +18,7 @@ v4.32.1 (2024-08-??) Fixed ^^^^^ - ``dict`` types not correctly forwarding previous nested values when parsing - (`#??? `__). + (`#559 `__). v4.32.0 (2024-07-19)