Skip to content

Commit ec382f1

Browse files
PabloLIONmauvilsa
andauthored
Fix/type action restriction (#687)
--------- Co-authored-by: Mauricio Villegas <[email protected]>
1 parent c3d3df7 commit ec382f1

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

CHANGELOG.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ Fixed
2828
- Regression parsing strings with omegaconf as the parser mode (`#686
2929
<https://github.com/omni-us/jsonargparse/pull/686>`__).
3030
- Help incorrectly showing environment variable name for ``--print_shtab``.
31-
31+
- ``add_argument`` raises error when type is assigned with ``action=None``
32+
(`#687 <https://github.com/omni-us/jsonargparse/issues/687>`__).
3233

3334
v4.37.0 (2025-02-14)
3435
--------------------

jsonargparse/_core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def add_argument(self, *args, enable_path: bool = False, **kwargs):
122122
enable_path: Whether to try parsing path/subconfig when argument is a complex type.
123123
"""
124124
parser = self.parser if hasattr(self, "parser") else self
125-
if "action" in kwargs:
125+
if kwargs.get("action") is not None:
126126
if ActionParser._is_valid_action_parser(parser, kwargs["action"]):
127127
return ActionParser._move_parser_actions(parser, args, kwargs)
128128
ActionConfigFile._ensure_single_config_argument(self, kwargs["action"])

jsonargparse/_typehints.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def normalize_default(self, default):
280280

281281
@staticmethod
282282
def prepare_add_argument(args, kwargs, enable_path, container, logger, sub_add_kwargs=None):
283-
if "action" in kwargs:
283+
if kwargs.get("action") is not None:
284284
raise ValueError("Providing both type and action not allowed.")
285285
typehint = kwargs.pop("type")
286286
if args[0].startswith("--") and ActionTypeHint.supports_append(typehint):

jsonargparse_tests/test_typehints.py

+5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ def test_add_argument_failure_given_type_and_action(parser):
6868
ctx.match("Providing both type and action not allowed")
6969

7070

71+
def test_add_argument_given_type_and_null_action(parser):
72+
parser.add_argument("--op1", type=Optional[bool], action=None)
73+
assert parser.get_defaults().op1 is None
74+
75+
7176
@pytest.mark.parametrize("typehint", [Namespace, Optional[Namespace], Union[int, Namespace], List[Namespace]])
7277
def test_namespace_unsupported_as_type(parser, typehint):
7378
with pytest.raises(ValueError, match="Namespace .* not supported as a type"):

0 commit comments

Comments
 (0)