Skip to content

Fix/type action restriction #687

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

Merged
merged 6 commits into from
Mar 5, 2025
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: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ Fixed
- Regression parsing strings with omegaconf as the parser mode (`#686
<https://github.com/omni-us/jsonargparse/pull/686>`__).
- Help incorrectly showing environment variable name for ``--print_shtab``.

- ``add_argument`` raises error when type is assigned with ``action=None``
(`#687 <https://github.com/omni-us/jsonargparse/issues/687>`__).

v4.37.0 (2025-02-14)
--------------------
Expand Down
2 changes: 1 addition & 1 deletion jsonargparse/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def add_argument(self, *args, enable_path: bool = False, **kwargs):
enable_path: Whether to try parsing path/subconfig when argument is a complex type.
"""
parser = self.parser if hasattr(self, "parser") else self
if "action" in kwargs:
if kwargs.get("action") is not None:
if ActionParser._is_valid_action_parser(parser, kwargs["action"]):
return ActionParser._move_parser_actions(parser, args, kwargs)
ActionConfigFile._ensure_single_config_argument(self, kwargs["action"])
Expand Down
2 changes: 1 addition & 1 deletion jsonargparse/_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def normalize_default(self, default):

@staticmethod
def prepare_add_argument(args, kwargs, enable_path, container, logger, sub_add_kwargs=None):
if "action" in kwargs:
if kwargs.get("action") is not None:
raise ValueError("Providing both type and action not allowed.")
typehint = kwargs.pop("type")
if args[0].startswith("--") and ActionTypeHint.supports_append(typehint):
Expand Down
5 changes: 5 additions & 0 deletions jsonargparse_tests/test_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def test_add_argument_failure_given_type_and_action(parser):
ctx.match("Providing both type and action not allowed")


def test_add_argument_given_type_and_null_action(parser):
parser.add_argument("--op1", type=Optional[bool], action=None)
assert parser.get_defaults().op1 is None


@pytest.mark.parametrize("typehint", [Namespace, Optional[Namespace], Union[int, Namespace], List[Namespace]])
def test_namespace_unsupported_as_type(parser, typehint):
with pytest.raises(ValueError, match="Namespace .* not supported as a type"):
Expand Down
Loading