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

For consistency add_subclass_arguments now sets default None instead of SUPPRESS #568

Merged
merged 4 commits into from
Sep 16, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ Changed
^^^^^^^
- Removed shtab experimental warning (`#561
<https://github.com/omni-us/jsonargparse/pull/561>`__).
- For consistency ``add_subclass_arguments`` now sets default ``None`` instead
of ``SUPPRESS`` (`lightning#20103
<https://github.com/Lightning-AI/pytorch-lightning/issue/20103>`__).


v4.32.1 (2024-08-23)
Expand Down
4 changes: 2 additions & 2 deletions jsonargparse/_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
)
from ._common import defaults_cache, parent_parser
from ._link_arguments import ActionLink
from ._namespace import Namespace
from ._namespace import Namespace, NSKeyError
from ._optionals import import_ruyaml
from ._type_checking import ArgumentParser, ruyamlCommentedMap
from ._typehints import ActionTypeHint, type_to_str
Expand Down Expand Up @@ -92,7 +92,7 @@ def _format_usage(self, *args, **kwargs) -> str:
for key in parser.required_args:
try:
default = parser.get_default(key)
except KeyError:
except NSKeyError:
default = None
if default is None and f"[--{key} " in usage:
usage = re.sub(f"\\[(--{key} [^\\]]+)]", r"\1", usage, count=1)
Expand Down
4 changes: 2 additions & 2 deletions jsonargparse/_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dataclasses
import inspect
import re
from argparse import SUPPRESS, ArgumentParser
from argparse import ArgumentParser
from contextlib import suppress
from typing import Any, Callable, List, Optional, Set, Tuple, Type, Union

Expand Down Expand Up @@ -549,7 +549,7 @@ def add_subclass_arguments(
}
)
if "default" not in kwargs:
kwargs["default"] = SUPPRESS
kwargs["default"] = None
self._add_signature_parameter(
group, None, param, added_args, skip, sub_configs=True, instantiate=instantiate, **kwargs
)
Expand Down
36 changes: 33 additions & 3 deletions jsonargparse_tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
strip_meta,
)
from jsonargparse._formatters import get_env_var
from jsonargparse._namespace import NSKeyError
from jsonargparse._optionals import jsonnet_support, jsonschema_support, ruyaml_support
from jsonargparse.typing import Path_fc, Path_fr, path_type
from jsonargparse_tests.conftest import (
Expand Down Expand Up @@ -760,9 +761,7 @@ def test_default_config_files(parser, subtests, tmp_cwd):
with subtests.test("get_default"):
assert parser.get_default("op1") == "from default config file"
parser.add_subclass_arguments(Calendar, "cal")
with pytest.raises(KeyError) as ctx:
parser.get_default("cal")
ctx.match("does not specify a default")
assert parser.get_default("cal") is None

with subtests.test("set invalid"):
with pytest.raises(ValueError) as ctx:
Expand Down Expand Up @@ -885,6 +884,37 @@ def test_set_get_defaults_multiple(parser, subparser, subtests):
pytest.raises(KeyError, lambda: parser.get_default("v4"))


def test_set_get_default_suppress_optional(parser):
parser.add_argument("--p1", type=int, default=SUPPRESS)
with pytest.raises(NSKeyError, match="does not specify a default"):
parser.get_default("p1")
assert parser.get_defaults() == Namespace()
help_str = get_parser_help(parser)
assert "--p1 P1 (type: int)" in help_str


def test_set_get_default_suppress_required(parser):
parser.add_argument("--p2", type=int, default=SUPPRESS, required=True)
with pytest.raises(NSKeyError, match="does not specify a default"):
parser.get_default("p2")
assert parser.get_defaults() == Namespace()
help_str = get_parser_help(parser)
assert "--p2 P2 (required, type: int)" in help_str


def test_set_get_default_suppress_default_config_file(parser, tmp_cwd):
default_config_file = tmp_cwd / "defaults.yaml"
default_config_file.write_text("p3: 1.2\n")

parser.default_config_files = [default_config_file]
parser.add_argument("--p3", type=float, default=SUPPRESS)
parser.add_argument("--p4", type=int, default=SUPPRESS)

assert parser.get_default("p3") == 1.2
with pytest.raises(NSKeyError, match="does not specify a default"):
parser.get_default("p4")


def test_add_config_action_class(parser):
parser.add_argument("--cfg", action=ActionConfigFile)
parser.add_argument("--op", type=int)
Expand Down
8 changes: 4 additions & 4 deletions jsonargparse_tests/test_subclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,9 +1338,9 @@ def test_add_subclass_required_group(parser):
def test_add_subclass_not_required_group(parser):
parser.add_subclass_arguments(Calendar, "cal", required=False)
cfg = parser.parse_args([])
assert cfg == Namespace()
assert cfg == Namespace(cal=None)
init = parser.instantiate_classes(cfg)
assert init == Namespace()
assert init == Namespace(cal=None)


class ListUnionA:
Expand Down Expand Up @@ -1723,8 +1723,8 @@ def test_subclass_error_indentation_invalid_init_arg(parser):
Given value: abc
"""
).strip()
expected = textwrap.indent(expected, " ")
assert expected in err
expected = textwrap.indent(expected, " ")
assert "\n".join(expected.splitlines()) in "\n".join(err.splitlines())


class ErrorIndentation2:
Expand Down