Skip to content

Commit 26b17d4

Browse files
authored
Added verification for strings that contain digits and a starting or ending 'e' (#673)
1 parent 19d7f61 commit 26b17d4

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Fixed
3131
<https://github.com/omni-us/jsonargparse/pull/668>`__)
3232
- Functions that create types now have ``TypeAlias`` return type to avoid mypy
3333
errors (`#671 <https://github.com/omni-us/jsonargparse/pull/671>`__).
34+
- Bug when parsing strings with digits and a starting or ending 'e' (`#672
35+
<https://github.com/omni-us/jsonargparse/pull/673>`__).
3436

3537

3638
v4.36.0 (2025-01-17)

jsonargparse/_loaders_dumpers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ def load_basic(value):
3030
return None
3131
if value.isdigit() or (value.startswith("-") and value[1:].isdigit()):
3232
return int(value)
33-
if value.replace(".", "", 1).replace("e", "", 1).replace("-", "", 2).isdigit() and ("e" in value or "." in value):
33+
if (
34+
not value.startswith("e")
35+
and not value.endswith("e")
36+
and value.replace(".", "", 1).replace("e", "", 1).replace("-", "", 2).isdigit()
37+
and ("e" in value or "." in value)
38+
):
3439
return float(value)
3540
return not_loaded
3641

jsonargparse_tests/test_loaders_dumpers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ def test_dump_header_invalid(parser):
9191
parser.dump_header = True
9292

9393

94+
def test_load_value_digits_and_e():
95+
with parser_context(load_value_mode="yaml"):
96+
assert "e123" == load_value("e123")
97+
assert "123e" == load_value("123e")
98+
99+
94100
@skip_if_no_pyyaml
95101
def test_load_value_dash():
96102
with parser_context(load_value_mode="yaml"):

0 commit comments

Comments
 (0)