Skip to content

Commit 06d5ed3

Browse files
committed
Fixed str parameter in subclass incorrectly parsed as dict with implicit null value (#262).
1 parent 3f40766 commit 06d5ed3

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ paths are considered internals and can change in minor and patch releases.
1515
v4.21.0 (2023-04-??)
1616
--------------------
1717

18+
Fixed
19+
^^^^^
20+
- `str` parameter in subclass incorrectly parsed as dict with implicit `null`
21+
value (`#262 <https://github.com/omni-us/jsonargparse/issues/262>`__).
22+
1823
Changed
1924
^^^^^^^
2025
- Switched from ``setup.cfg`` to ``pyproject.toml`` for configuration.
2126
- Removed ``build_sphinx`` from ``setup.py`` and documented how to build.
2227
- Include enum members in error when invalid value is given
23-
`pytorch-lightning#17247
24-
<https://github.com/Lightning-AI/lightning/issues/17247>`__.
28+
(`pytorch-lightning#17247
29+
<https://github.com/Lightning-AI/lightning/issues/17247>`__).
2530

2631

2732
v4.20.1 (2023-03-30)

jsonargparse/loaders_dumpers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,13 @@ def yaml_load(stream):
5656
value = stream
5757
else:
5858
value = yaml.load(stream, Loader=DefaultLoader)
59-
if isinstance(value, dict) and all(v is None for v in value.values()):
60-
keys = {k for k in regex_curly_comma.split(stream) if k}
61-
if len(keys) > 0 and keys == set(value.keys()):
59+
if isinstance(value, dict) and value and all(v is None for v in value.values()):
60+
if len(value) == 1 and stream.strip() == next(iter(value.keys())) + ':':
6261
value = stream
62+
else:
63+
keys = {k for k in regex_curly_comma.split(stream) if k}
64+
if len(keys) > 0 and keys == set(value.keys()):
65+
value = stream
6366
return value
6467

6568

jsonargparse_tests/test_loaders_dumpers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from jsonargparse import ActionConfigFile, ArgumentParser, set_dumper, set_loader
1212
from jsonargparse._common import parser_context
1313
from jsonargparse.loaders_dumpers import load_value, loaders, yaml_dump
14+
from jsonargparse_tests.base import mock_module
1415

1516

1617
class LoadersTests(unittest.TestCase):
@@ -36,6 +37,18 @@ def test_disable_implicit_mapping_values(self):
3637
self.assertEqual('{one,two,three}', parser.parse_args(['--val={one,two,three}']).val)
3738

3839

40+
def test_disable_implicit_null(self):
41+
class Bar:
42+
def __init__(self, x: str):
43+
pass
44+
45+
with mock_module(Bar):
46+
parser = ArgumentParser()
47+
parser.add_subclass_arguments(Bar, 'bar')
48+
cfg = parser.parse_args(['--bar=Bar', '--bar.x=Foo:'])
49+
self.assertEqual('Foo:', cfg.bar.init_args.x)
50+
51+
3952
@unittest.skipIf(not find_spec('omegaconf'), 'omegaconf package is required')
4053
def test_parser_mode_omegaconf(self):
4154
parser = ArgumentParser(exit_on_error=False, parser_mode='omegaconf')

0 commit comments

Comments
 (0)