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
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Fixed
<https://github.com/omni-us/jsonargparse/pull/833>`__).
- Targets of links applied on parse not being instantiated (`#834
<https://github.com/omni-us/jsonargparse/pull/834>`__).
- Validation of defaults getting stuck for path with ``-`` (stdin) default
(`#837 <https://github.com/omni-us/jsonargparse/pull/837>`__).


v4.45.0 (2025-12-26)
Expand Down
4 changes: 3 additions & 1 deletion jsonargparse/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __call__(self, class_type: type[ClassType], *args, **kwargs) -> ClassType:
defaults_cache: ContextVar[Optional[Namespace]] = ContextVar("defaults_cache", default=None)
lenient_check: ContextVar[Union[bool, str]] = ContextVar("lenient_check", default=False)
parsing_defaults: ContextVar[bool] = ContextVar("parsing_defaults", default=False)
validating_defaults: ContextVar[bool] = ContextVar("validating_defaults", default=False)
load_value_mode: ContextVar[Optional[str]] = ContextVar("load_value_mode", default=None)
class_instantiators: ContextVar[Optional[InstantiatorsDictType]] = ContextVar("class_instantiators", default=None)
nested_links: ContextVar[list[dict]] = ContextVar("nested_links", default=[])
Expand All @@ -72,6 +73,7 @@ def __call__(self, class_type: type[ClassType], *args, **kwargs) -> ClassType:
"defaults_cache": defaults_cache,
"lenient_check": lenient_check,
"parsing_defaults": parsing_defaults,
"validating_defaults": validating_defaults,
"load_value_mode": load_value_mode,
"class_instantiators": class_instantiators,
"nested_links": nested_links,
Expand Down Expand Up @@ -207,7 +209,7 @@ def validate_default(container: ActionsContainer, action: argparse.Action):

if isinstance(container, ArgumentGroup):
container = container.parser # type: ignore[assignment]
with parser_context(parent_parser=container):
with parser_context(parent_parser=container, validating_defaults=True):
default = action.default
action.default = None
action.default = action._check_type_(default) # type: ignore[attr-defined]
Expand Down
3 changes: 2 additions & 1 deletion jsonargparse/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,8 @@ def get_defaults(self, skip_validation: bool = False, **kwargs) -> Namespace:
cfg["__default_config__"] = default_config_file
self._logger.debug("Parsed default configuration from path: %s", default_config_file)

ActionTypeHint.add_sub_defaults(self, cfg)
with parser_context(validating_defaults=True):
ActionTypeHint.add_sub_defaults(self, cfg)

return cfg

Expand Down
3 changes: 3 additions & 0 deletions jsonargparse/_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
nested_links,
parent_parser,
parser_context,
validating_defaults,
)
from ._loaders_dumpers import (
get_loader_exceptions,
Expand Down Expand Up @@ -897,6 +898,8 @@ def adapt_typehints(
prev_val = prev_val + [None] * (len(val) - len(prev_val) if val_is_list else 1)
list_path = None
if enable_path and type(val) is str:
if validating_defaults.get():
return val
with suppress(TypeError):
from ._optionals import _get_config_read_mode

Expand Down
9 changes: 9 additions & 0 deletions jsonargparse_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ def logger() -> logging.Logger:
return logger


def patch_parsing_settings(fn):
@wraps(fn)
def _patch_parsing_settings(*args, **kwargs):
with patch.dict("jsonargparse._common.parsing_settings"):
return fn(*args, **kwargs)

return _patch_parsing_settings


@contextmanager
def capture_logs(logger: logging.Logger) -> Iterator[StringIO]:
with ExitStack() as stack:
Expand Down
2 changes: 1 addition & 1 deletion jsonargparse_tests/test_parsing_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


@pytest.fixture(autouse=True)
def patch_parsing_settings():
def auto_patch_parsing_settings():
with patch.dict("jsonargparse._common.parsing_settings"):
yield

Expand Down
22 changes: 20 additions & 2 deletions jsonargparse_tests/test_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import pytest

from jsonargparse import ArgumentError, Namespace
from jsonargparse import ArgumentError, Namespace, set_parsing_settings
from jsonargparse._optionals import fsspec_support, url_support
from jsonargparse._paths import _current_path_dir, _parse_url
from jsonargparse.typing import Path, Path_drw, Path_fc, Path_fr, path_type
Expand All @@ -21,6 +21,7 @@
is_posix,
json_or_yaml_dump,
json_or_yaml_load,
patch_parsing_settings,
responses_activate,
responses_available,
skip_if_fsspec_unavailable,
Expand Down Expand Up @@ -524,6 +525,16 @@
assert json_or_yaml_load(parser.dump(cfg)) == {"paths": ["path1", "path2"]}


def test_path_fr_default_stdin(parser):
parser.add_argument("--path", type=Path_fr, default="-")

defaults = parser.get_defaults()
assert defaults.path == Path_fr("-")
Comment thread Dismissed

defaults = parser.parse_args([])
assert defaults.path == Path_fr("-")


# enable_path tests


Expand Down Expand Up @@ -622,7 +633,10 @@
ctx.match("Expected a path but no-such-file either not accessible or invalid")


def test_enable_path_list_path_fr_default_stdin(parser, tmp_cwd, mock_stdin, subtests):
@pytest.mark.parametrize("validate_defaults", [False, True])
@patch_parsing_settings
def test_enable_path_list_path_fr_default_stdin(parser, tmp_cwd, validate_defaults, mock_stdin, subtests):
set_parsing_settings(validate_defaults=validate_defaults)
(tmp_cwd / "file1").touch()
(tmp_cwd / "file2").touch()

Expand All @@ -633,6 +647,10 @@
default="-",
)

with subtests.test("defaults"):
defaults = parser.get_defaults()
assert defaults.list == "-"

with subtests.test("without args"):
with mock_stdin("file1\nfile2\n"):
cfg = parser.parse_args([])
Expand Down
Loading