Skip to content

Commit

Permalink
Merge branch 'main' into nested_attrs_dataclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
mauvilsa authored Dec 17, 2024
2 parents b470ebc + cd090e3 commit 8d5b125
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 4.34.1
current_version = 4.35.0
commit = True
tag = True
tag_name = v{new_version}
Expand Down
15 changes: 14 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ The semantic versioning only considers the public API as described in
paths are considered internals and can change in minor and patch releases.


v4.35.0 (2024-12-??)
v4.35.0 (2024-12-16)
--------------------

Added
^^^^^
- Support for ``print config`` argument to reuse the name of the config argument
by using ``%s`` (`#630 <https://github.com/omni-us/jsonargparse/pull/630>`__).

Changed
^^^^^^^
- Argument groups created from dataclass-like that have zero configurable
Expand All @@ -24,11 +29,19 @@ Changed
the help of the base class is printed (`#628
<https://github.com/omni-us/jsonargparse/pull/628>`__).

Fixed
^^^^^
- Account for change in ``ArgumentParser._parse_known_args`` since Python 3.12.8
and 3.13.1 (`#644 <https://github.com/omni-us/jsonargparse/pull/644>`__).

Deprecated
^^^^^^^^^^
- ``add_dataclass_arguments`` is deprecated and will be removed in v5.0.0.
Instead use ``add_class_arguments`` (`#634
<https://github.com/omni-us/jsonargparse/pull/634>`__).
- From v5.0.0 the print config argument will by default reuse the name of the
config argument as ``--print_%s`` instead of being always ``--print_config``
(`#630 <https://github.com/omni-us/jsonargparse/pull/630>`__).

v4.34.2 (2024-12-??)
--------------------
Expand Down
2 changes: 1 addition & 1 deletion jsonargparse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@
__all__ += _deprecated.__all__


__version__ = "4.34.1"
__version__ = "4.35.0"
3 changes: 3 additions & 0 deletions jsonargparse/_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ def _ensure_single_config_argument(container, action):
@staticmethod
def _add_print_config_argument(container, action):
if isinstance(action, ActionConfigFile) and getattr(container, "_print_config", None) is not None:
if "%s" in container._print_config:
container._print_config = container._print_config % action.dest
assert container._print_config.startswith("--")
container.add_argument(container._print_config, action=_ActionPrintConfig)

@staticmethod
Expand Down
10 changes: 8 additions & 2 deletions jsonargparse/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
__all__ = ["ActionsContainer", "ArgumentParser"]


_parse_known_has_intermixed = "intermixed" in inspect.signature(argparse.ArgumentParser._parse_known_args).parameters


class ActionsContainer(SignatureArguments, argparse._ActionsContainer):
"""Extension of argparse._ActionsContainer to support additional functionalities."""

Expand Down Expand Up @@ -246,7 +249,7 @@ def __init__(
formatter_class: Class for printing help messages.
logger: Configures the logger, see :class:`.LoggerProperty`.
version: Program version which will be printed by the --version argument.
print_config: Add this as argument to print config, set None to disable.
print_config: Name for print config argument, ``%s`` is replaced by config dest, set None to disable.
parser_mode: Mode for parsing config files: ``'yaml'``, ``'jsonnet'`` or ones added via :func:`.set_loader`.
dump_header: Header to include as comment when dumping a config object.
default_config_files: Default config file locations, e.g. ``['~/.config/myapp/*.yaml']``.
Expand Down Expand Up @@ -288,7 +291,10 @@ def parse_known_args(self, args=None, namespace=None):
with patch_namespace(), parser_context(
parent_parser=self, lenient_check=True
), ActionTypeHint.subclass_arg_context(self):
namespace, args = self._parse_known_args(args, namespace)
kwargs = {}
if _parse_known_has_intermixed:
kwargs["intermixed"] = False
namespace, args = self._parse_known_args(args, namespace, **kwargs)
except argparse.ArgumentError as ex:
self.error(str(ex), ex)

Expand Down
8 changes: 8 additions & 0 deletions jsonargparse_tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,14 @@ def test_print_config_empty_default_config_file(print_parser, tmp_cwd):
assert yaml.safe_load(out) == {"g1": {"v2": "2"}, "g2": {"v3": None}, "v1": 1}


def test_print_config_reuse_name():
parser = ArgumentParser(exit_on_error=False, print_config="--print_%s")
parser.add_argument("--conf", action="config")
parser.add_argument("--x", default=1)
out = get_parse_args_stdout(parser, ["--print_conf"])
assert yaml.safe_load(out) == {"x": 1}


def test_default_config_files(parser, subtests, tmp_cwd):
default_config_file = tmp_cwd / "defaults.yaml"
default_config_file.write_text("op1: from default config file\n")
Expand Down

0 comments on commit 8d5b125

Please sign in to comment.