Repository: omni-us/jsonargparse
Pull request #673 (2025-02-06 20:26:43)

Added verification for strings that contain digits and a starting or ending 'e'
<!--
Thank you very much for contributing! If you like this project, please ⭐ star it
https://github.com/omni-us/jsonargparse/
-->

## What does this PR do?

It adds two conditions of the string passed to the **load_basic** function:
- The first ensures the string do not start with an 'e'
- The second ensures the string do not end with an 'e'

<!--
Concisely describe what this pull request does. If available, include links to
issues web pages where the need for this change has been motivated.
-->

## Before submitting

<!--
Use the following list as tasks to be completed before marking a pull request as
"Ready for review". Fill with an "x" all tasks done. Use "n/a" if you think a
task is not relevant or leave empty when in doubt.
-->

- [x] Did you read the [contributing guideline](https://github.com/omni-us/jsonargparse/blob/main/CONTRIBUTING.rst)?
- [x] Did you update **the documentation**? (readme and public docstrings)
- [x] Did you write **unit tests** such that there is 100% coverage on related code? (required for bug fixes and new features)
- [x] Did you verify that new and existing **tests pass locally**?
- [x] Did you make sure that all changes preserve **backward compatibility**?
- [x] Did you update **the CHANGELOG**? (not for typos, docs, test updates, or minor internal changes/refactors)


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/omni-us/jsonargparse/pull/673?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 100.00%. Comparing base [(`19d7f61`)](https://app.codecov.io/gh/omni-us/jsonargparse/commit/19d7f61daf3e4d1930dc3217462ce69953de013d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us) to head [(`dd677d2`)](https://app.codecov.io/gh/omni-us/jsonargparse/commit/dd677d2befc2003d6534a8dc9b6f666bf7174fac?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us).

:white_check_mark: All tests successful. No failed tests found.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##              main      #673   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           22        22           
  Lines         6678      6678           
=========================================
  Hits          6678      6678           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/omni-us/jsonargparse/pull/673?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=omni-us).


Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 22f3d6b..8aab1a3 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -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)
diff --git a/jsonargparse/_loaders_dumpers.py b/jsonargparse/_loaders_dumpers.py
index 6358e9a..b8254be 100644
--- a/jsonargparse/_loaders_dumpers.py
+++ b/jsonargparse/_loaders_dumpers.py
@@ -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
 


Test patch:
diff --git a/jsonargparse_tests/test_loaders_dumpers.py b/jsonargparse_tests/test_loaders_dumpers.py
index 56c9e6b..9ba357d 100644
--- a/jsonargparse_tests/test_loaders_dumpers.py
+++ b/jsonargparse_tests/test_loaders_dumpers.py
@@ -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"):