Repository: omni-us/jsonargparse

Issue:
Overriding a config's argument makes everything else fall back to the base class
## 🐛 Bug report

### To reproduce

`repro.py`:

```python
from dataclasses import dataclass
from typing import Optional

@dataclass
class Thing:
    foo: int = 1
    bar: int = 2

def fn(thing: Optional[Thing] = None):
    thing = Thing() if thing is None else thing
    print(thing)

from jsonargparse import CLI
CLI(fn)
```

`repro.yaml`:

```yaml
thing:
  foo: 123
  bar: 321
```

`python repro.py --config repro.yaml --thing.foo 9`

### Expected behavior

The above prints

```python
Thing(foo=9, bar=2)
```

but I expected

```python
Thing(foo=9, bar=321)
```

### Context

A more real world example is that you have a `trainer_config.yaml` and you want to only modify the `--train.max_epochs` value without having everything else fall back to dataclass defaults

### Environment

Version 4.275


Comments:

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 87a789b..b6bc59b 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -24,6 +24,8 @@ Fixed
   <https://github.com/omni-us/jsonargparse/issues/465>`__).
 - Optional callable that returns a class instance with a lambda default,
   produces an invalid string default.
+- dataclass single parameter change incorrectly resetting previous values (`#464
+  <https://github.com/omni-us/jsonargparse/issues/464>`__).
 
 
 v4.27.5 (2024-02-12)
diff --git a/jsonargparse/_typehints.py b/jsonargparse/_typehints.py
index eb6f050..67ec5eb 100644
--- a/jsonargparse/_typehints.py
+++ b/jsonargparse/_typehints.py
@@ -863,7 +863,8 @@ def adapt_typehints(
         elif isinstance(val, (dict, Namespace)):
             val = parser.parse_object(val, defaults=sub_defaults.get() or list_item)
         elif isinstance(val, NestedArg):
-            val = parser.parse_args([f"--{val.key}={val.val}"])
+            prev_val = prev_val if isinstance(prev_val, Namespace) else None
+            val = parser.parse_args([f"--{val.key}={val.val}"], namespace=prev_val)
         else:
             raise_unexpected_value(f"Type {typehint} expects a dict or Namespace", val)
 


Test patch:
diff --git a/jsonargparse_tests/test_dataclass_like.py b/jsonargparse_tests/test_dataclass_like.py
index df8b431..0402eee 100644
--- a/jsonargparse_tests/test_dataclass_like.py
+++ b/jsonargparse_tests/test_dataclass_like.py
@@ -8,6 +8,7 @@ import pytest
 import yaml
 
 from jsonargparse import (
+    ActionConfigFile,
     ArgumentError,
     ArgumentParser,
     Namespace,
@@ -359,6 +360,20 @@ def test_optional_dataclass_type_null_value():
     assert cfg == parser_optional_data.instantiate_classes(cfg)
 
 
+@dataclasses.dataclass
+class SingleParamChange:
+    p1: int = 0
+    p2: int = 0
+
+
+def test_optional_dataclass_single_param_change(parser):
+    parser.add_argument("--config", action=ActionConfigFile)
+    parser.add_argument("--data", type=Optional[SingleParamChange])
+    config = {"data": {"p1": 1}}
+    cfg = parser.parse_args([f"--config={config}", "--data.p2=2"])
+    assert cfg.data == Namespace(p1=1, p2=2)
+
+
 @dataclasses.dataclass
 class ModelConfig:
     data: Optional[Dict[str, Any]] = None