Repository: omni-us/jsonargparse

Issue:
Unexpected config error when a default config file is empty
## 🐛 Bug report

If at least one `default_config_files` is set and this file is empty, a `TypeError: Unexpected config: ` is triggered on `parser.parse_args()`. Even if no argument is mandatory.

### To reproduce

```python
from jsonargparse import ArgumentParser

parser = ArgumentParser(default_config_files=['config.yaml'])
parser.add_argument("test", type=int, help="test", default=1)
cfg = parser.parse_args()
print(cfg.as_dict())
```

**no 'config.yaml' file**
```bash
python main.py
> {'test': 1}
```
Work as expected, no error

**empty 'config.yaml' file**
```bash
touch config.yaml
python main.py
> Unexpected config error:
```

**Not empty 'config.yaml' file**
Work as expected, no error
```bash
echo "test: 42" > config.yaml
python bug.py
> {'test': 42, '__default_config__': Path_fr(config.yaml, cwd=/Users/...)}
```

### Expected behavior

I expect to have the same behavior between no configuration file and empty configuration file.
In the case where every argument are optional I don't expect to have an error.

In my case I wanted to generate this default configuration file as suggest [in the document](https://jsonargparse.readthedocs.io/en/v4.24.0/#writing-configuration-files) with:

```bash
python example.py --print_config > config.yaml
````

But an empty file is created before to run the script. Which trigger the issue mentioned above.

### Environment

- jsonargparse version: 4.24.0
- Python version: 3.11.4
- How jsonargparse was installed: `pip install jsonargparse[signatures]~=4.24 docstring-parser~=0.15 PyYAML~=6.0 ruyaml~=0.91`
- OS: macOS 13.4.1


Comments:

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 46cd745..eccf4d3 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -26,6 +26,9 @@ Fixed
   <https://github.com/omni-us/jsonargparse/issues/362>`__).
 - ``ActionJsonSchema`` not setting correctly defaults when schema uses
   ``oneOf``.
+- Recommended ``print_config`` steps not working when ``default_config_files``
+  used due to the config file initially being empty (`#367
+  <https://github.com/omni-us/jsonargparse/issues/367>`__).
 
 
 v4.24.0 (2023-08-23)
diff --git a/jsonargparse/_core.py b/jsonargparse/_core.py
index 014f056..778613c 100644
--- a/jsonargparse/_core.py
+++ b/jsonargparse/_core.py
@@ -631,6 +631,8 @@ class ArgumentParser(ParserDeprecations, ActionsContainer, ArgumentLinking, argp
             cfg_dict = load_value(cfg_str, path=cfg_path, ext_vars=ext_vars)
         except get_loader_exceptions() as ex:
             raise TypeError(f"Problems parsing config: {ex}") from ex
+        if cfg_dict is None:
+            return Namespace()
         if not isinstance(cfg_dict, dict):
             raise TypeError(f"Unexpected config: {cfg_str}")
         return self._apply_actions(cfg_dict, prev_cfg=prev_cfg)


Test patch:
diff --git a/jsonargparse_tests/test_core.py b/jsonargparse_tests/test_core.py
index 950420b..efd4df2 100644
--- a/jsonargparse_tests/test_core.py
+++ b/jsonargparse_tests/test_core.py
@@ -698,6 +698,15 @@ def test_print_config_invalid_flag(print_parser):
     ctx.match('Invalid option "invalid"')
 
 
+@pytest.mark.skipif(sys.version_info[:2] == (3, 6), reason="not supported in python 3.6")
+def test_print_config_empty_default_config_file(print_parser, tmp_cwd):
+    default_config_file = tmp_cwd / "defaults.yaml"
+    default_config_file.touch()
+    print_parser.default_config_files = [default_config_file]
+    out = get_parse_args_stdout(print_parser, ["--print_config"])
+    assert yaml.safe_load(out) == {"g1": {"v2": "2"}, "g2": {"v3": None}, "v1": 1}
+
+
 def test_default_config_files(parser, subtests, tmp_cwd):
     default_config_file = tmp_cwd / "defaults.yaml"
     default_config_file.write_text("op1: from default config file\n")