Repository: omni-us/jsonargparse

Issue:
`default` and `default_factory` do not work in nested list with parse_object
<!-- If you like this project, please ⭐ star it https://github.com/omni-us/jsonargparse/ -->

## 🐛 Bug report

<!-- A clear and concise description of the bug. -->

### To reproduce

```python
from dataclasses import dataclass, field

from jsonargparse import ArgumentParser

@dataclass
class A:
    x: list = field(default_factory=list)
	v: int = 1

@dataclass
class B:
    a: list[A]

def main():
    parser = ArgumentParser()
    parser.add_dataclass_arguments(B, 'conf')
    args = parser.parse_object({
        'conf': {
            'a': [{}]
        }
    })
    print(args.conf)

if __name__ == '__main__':
    main()
```

### Expected result

```
Namespace(a=[Namespace(v=1, x=[])])
```

### Actual result

```
Namespace(a=[Namespace()])
```

### Environment

<!-- Fill in the list below. -->

- jsonargparse version (e.g., 4.8.0): 4.23.1
- Python version (e.g., 3.9): 3.11.4
- How jsonargparse was installed: `pip install jsonargparse[all]`
- OS (e.g., Linux): Ubuntu 22.04


Comments:

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 65d6bd7..73f7a38 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -19,6 +19,8 @@ Fixed
 ^^^^^
 - Remove private ``linked_targets`` parameter from API Reference (`#317
   <https://github.com/omni-us/jsonargparse/issues/317>`__).
+- Dataclass nested in list not setting defaults (`#357
+  <https://github.com/omni-us/jsonargparse/issues/357>`__)
 
 
 v4.24.0 (2023-08-23)
diff --git a/jsonargparse/_typehints.py b/jsonargparse/_typehints.py
index 47f9bed..40b94a2 100644
--- a/jsonargparse/_typehints.py
+++ b/jsonargparse/_typehints.py
@@ -576,6 +576,7 @@ def adapt_typehints(
     instantiate_classes=False,
     prev_val=None,
     append=False,
+    list_item=False,
     enable_path=False,
     sub_add_kwargs=None,
 ):
@@ -717,7 +718,7 @@ def adapt_typehints(
             for n, v in enumerate(val):
                 if isinstance(prev_val, list) and len(prev_val) == len(val):
                     adapt_kwargs_n = {**adapt_kwargs, "prev_val": prev_val[n]}
-                val[n] = adapt_typehints(v, subtypehints[0], **adapt_kwargs_n)
+                val[n] = adapt_typehints(v, subtypehints[0], list_item=True, **adapt_kwargs_n)
 
     # Dict, Mapping
     elif typehint_origin in mapping_origin_types:
@@ -802,7 +803,7 @@ def adapt_typehints(
         if serialize:
             val = load_value(parser.dump(val, **dump_kwargs.get()))
         elif isinstance(val, (dict, Namespace)):
-            val = parser.parse_object(val, defaults=sub_defaults.get())
+            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}"])
         else:


Test patch:
diff --git a/jsonargparse_tests/test_dataclass_like.py b/jsonargparse_tests/test_dataclass_like.py
index 2fe5092..50d55d6 100644
--- a/jsonargparse_tests/test_dataclass_like.py
+++ b/jsonargparse_tests/test_dataclass_like.py
@@ -98,6 +98,23 @@ def test_add_dataclass_arguments(parser, subtests):
             parser.add_dataclass_arguments(MixedClass, "c")
 
 
+@dataclasses.dataclass
+class NestedDefaultsA:
+    x: list = dataclasses.field(default_factory=list)
+    v: int = 1
+
+
+@dataclasses.dataclass
+class NestedDefaultsB:
+    a: List[NestedDefaultsA]
+
+
+def test_add_dataclass_nested_defaults(parser):
+    parser.add_dataclass_arguments(NestedDefaultsB, "data")
+    cfg = parser.parse_args(["--data.a=[{}]"])
+    assert cfg.data == Namespace(a=[Namespace(x=[], v=1)])
+
+
 class ClassDataAttributes:
     def __init__(
         self,