Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Added

Fixed
^^^^^
- Dict override syntax to allow standard form: --cfg.dict.key val (`#824
<https://github.com/omni-us/jsonargparse/pull/824>`__).
- Evaluation of postponed annotations for dataclass inheritance across modules
not working correctly (`#814
<https://github.com/omni-us/jsonargparse/pull/814>`__).
Expand Down
6 changes: 5 additions & 1 deletion jsonargparse/_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,11 @@ def adapt_typehints(
elif typehint_origin in mapping_origin_types:
if isinstance(val, NestedArg):
if isinstance(prev_val, dict):
val = {**prev_val, val.key: val.val}
if isinstance(val.key, str) and "." in val.key:
key_prefix, key_suffix = val.key.split(".", 1)
val = {**prev_val, key_prefix: {key_suffix: val.val}}
else:
val = {**prev_val, val.key: val.val}
else:
val = {val.key: val.val}
elif isinstance(val, MappingProxyType):
Expand Down
28 changes: 27 additions & 1 deletion jsonargparse_tests/test_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import uuid
from calendar import Calendar, TextCalendar
from collections import OrderedDict
from dataclasses import dataclass
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from types import MappingProxyType
Expand Down Expand Up @@ -580,6 +580,32 @@
assert cfg.dict == {"one": 1, "two": 2}


@dataclass
class _Cfg:
"""Needs to be defined outside of test_nested_dict_command_line_set_items"""

@dataclass
class _Vals:
val_0: int = 0
val_1: int = 0

vals: dict[str, _Vals] = field(default_factory=dict)


def test_nested_dict_command_line_set_items(parser):
parser.add_class_arguments(_Cfg, nested_key="cfg")

# works before #824
args = ["--cfg", '{"vals": {"a": {"val_0": 0, "val_1": 1}}}', "--cfg.vals.a", '{"val_0": 100}']
cfg = parser.parse_args(args).cfg

# does not work before #824
args = ["--cfg", '{"vals": {"a": {"val_0": 0, "val_1": 1}}}', "--cfg.vals.a.val_0", "100"]
cfg = parser.parse_args(args).cfg

assert (cfg.vals["a"].val_0, cfg.vals["a"].val_1) == (100, 1)


def test_dict_command_line_set_items_with_space(parser):
parser.add_argument("--dict", type=dict)
cfg = parser.parse_args(["--dict.a=x y"])
Expand Down
Loading