Skip to content

Commit 66ce10d

Browse files
authored
Fix omegaconf+ parser mode failing when there are inf, -inf or nan values (#773)
1 parent ceb7202 commit 66ce10d

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Fixed
2929
<https://github.com/omni-us/jsonargparse/pull/771>`__).
3030
- Misleading error message when a namespace is used in a list comprehension
3131
(`#772 <https://github.com/omni-us/jsonargparse/pull/772>`__).
32+
- ``omegaconf+`` parser mode failing when there are ``inf``, ``-inf`` or ``nan``
33+
values (`#773 <https://github.com/omni-us/jsonargparse/pull/773>`__).
3234

3335

3436
v4.41.0 (2025-09-04)

jsonargparse/_optionals.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,7 @@ def omegaconf_apply(parser, cfg):
296296
from ._common import parser_context
297297

298298
with parser_context(path_dump_preserve_relative=True):
299-
cfg_dict = parser.dump(
300-
cfg, format="json_compact", skip_validation=True, skip_none=False, skip_link_targets=False
301-
)
299+
cfg_dict = parser.dump(cfg, skip_validation=True, skip_none=False, skip_link_targets=False)
302300
cfg_omegaconf = OmegaConf.create(cfg_dict)
303301
cfg_dict = OmegaConf.to_container(cfg_omegaconf, resolve=True)
304302
return parser._apply_actions(cfg_dict)

jsonargparse_tests/test_omegaconf.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,20 @@ def test_omegaconf_global_path_preserve_relative(parser, tmp_cwd):
176176
with parser_context(path_dump_preserve_relative=True):
177177
dump = yaml.safe_load(parser.dump(cfg))["nested"]["path"]
178178
assert dump == {"relative": "file", "cwd": str(tmp_cwd / subdir)}
179+
180+
181+
@skip_if_omegaconf_unavailable
182+
def test_omegaconf_inf_nan(parser):
183+
parser.parser_mode = "omegaconf+"
184+
parser.add_argument("--a", type=float, default=0.0)
185+
parser.add_argument("--b", type=float, default=1.0)
186+
parser.add_argument("--c", type=float, default=float("nan"))
187+
parser.add_argument("--d", type=float, default=float("inf"))
188+
parser.add_argument("--e", type=float, default=float("-inf"))
189+
190+
cfg = parser.parse_args(["--a=2.5", "--b=${a}"])
191+
assert cfg.a == 2.5
192+
assert cfg.b == 2.5
193+
assert math.isnan(cfg.c)
194+
assert cfg.d == float("inf")
195+
assert cfg.e == float("-inf")

0 commit comments

Comments
 (0)