Skip to content
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
5 changes: 3 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ 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>`__).
- String parsing regressions (`#673
<https://github.com/omni-us/jsonargparse/pull/673>`__, `#674
<https://github.com/omni-us/jsonargparse/pull/674>`__).


v4.36.0 (2025-01-17)
Expand Down
18 changes: 9 additions & 9 deletions jsonargparse/_loaders_dumpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
return False
if value == "null":
return None
if value.isdigit() or (value.startswith("-") and value[1:].isdigit()):
return int(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)
try:
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
):
return float(value)
except ValueError:

Check notice

Code scanning / CodeQL

Empty except Note

'except' clause does nothing but pass and there is no explanatory comment.
pass
return not_loaded


Expand Down
15 changes: 12 additions & 3 deletions jsonargparse/_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,11 +602,20 @@ def _check_type(self, value, append=False, cfg=None):
val["__path__"] = config_path
value[num] = val
except (TypeError, ValueError) as ex:
elem = "" if not islist else f" element {num+1}"
error = indent_text(str(ex))
raise TypeError(f'Parser key "{self.dest}"{elem}:\n{error}') from ex
if self._is_valid_string(val):
value[num] = val
else:
elem = "" if not islist else f" element {num+1}"
error = indent_text(str(ex))
raise TypeError(f'Parser key "{self.dest}"{elem}:\n{error}') from ex
return value if islist else value[0]

def _is_valid_string(self, value):
typehint = self._typehint
return isinstance(value, str) and (
typehint is str or (get_typehint_origin(typehint) == Union and str in typehint.__args__)
)

def instantiate_classes(self, value):
islist = _is_action_value_list(self)
if not islist:
Expand Down
6 changes: 0 additions & 6 deletions jsonargparse_tests/test_loaders_dumpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ 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
9 changes: 9 additions & 0 deletions jsonargparse_tests/test_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ def test_str_yaml_constructor_error(parser):
assert "{{something}}" == parser.parse_args(["--val={{something}}"]).val


@parser_modes
def test_str_edge_cases(parser):
parser.add_argument("--val", type=str)
assert parser.parse_args(["--val=e123"]).val == "e123"
assert parser.parse_args(["--val=123e"]).val == "123e"
val = "1" * 5000
assert parser.parse_args([f"--val={val}"]).val == val


def test_bool_parse(parser):
parser.add_argument("--val", type=bool)
assert None is parser.get_defaults().val
Expand Down
Loading