Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added verification for strings that contain digits and a starting or ending 'e' #673

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Fixed
<https://github.com/omni-us/jsonargparse/pull/668>`__)
- Functions that create types now have ``TypeAlias`` return type to avoid mypy
errors (`#671 <https://github.com/omni-us/jsonargparse/pull/671>`__).
- Bug when parsing strings with digits and a starting or ending 'e' (`#672
<https://github.com/omni-us/jsonargparse/pull/673>`__).


v4.36.0 (2025-01-17)
Expand Down
7 changes: 6 additions & 1 deletion jsonargparse/_loaders_dumpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ def load_basic(value):
return None
if value.isdigit() or (value.startswith("-") and value[1:].isdigit()):
return int(value)
if value.replace(".", "", 1).replace("e", "", 1).replace("-", "", 2).isdigit() and ("e" in value or "." in value):
if (
not value.startswith("e")
and not value.endswith("e")
and value.replace(".", "", 1).replace("e", "", 1).replace("-", "", 2).isdigit()
and ("e" in value or "." in value)
):
return float(value)
return not_loaded

Expand Down
6 changes: 6 additions & 0 deletions jsonargparse_tests/test_loaders_dumpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ def test_dump_header_invalid(parser):
parser.dump_header = True


def test_load_value_digits_and_e():
with parser_context(load_value_mode="yaml"):
assert "e123" == load_value("e123")
assert "123e" == load_value("123e")


@skip_if_no_pyyaml
def test_load_value_dash():
with parser_context(load_value_mode="yaml"):
Expand Down
Loading