Skip to content

Commit 1a097bf

Browse files
authored
auto_cli now supports class @Property (#701)
1 parent aac2bd1 commit 1a097bf

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Added
2121
<https://github.com/omni-us/jsonargparse/pull/699>`__).
2222
- ``shtab`` support for optionals parsed as positionals (`#700
2323
<https://github.com/omni-us/jsonargparse/pull/700>`__).
24+
- ``auto_cli`` now supports class ``@property`` (`#701
25+
<https://github.com/omni-us/jsonargparse/pull/701>`__).
2426

2527
Changed
2628
^^^^^^^

jsonargparse/_cli.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def _add_subcommands(
157157
remove_actions(subparser, (ActionConfigFile, _ActionPrintConfig))
158158

159159

160-
def has_parameter(component, name):
160+
def has_parameter(component, name) -> bool:
161161
return name in inspect.signature(component).parameters.keys()
162162

163163

@@ -170,7 +170,9 @@ def _add_component_to_parser(
170170
):
171171
kwargs: dict = dict(as_positional=as_positional, fail_untyped=fail_untyped, sub_configs=True)
172172
if inspect.isclass(component):
173-
class_methods = [k for k, v in inspect.getmembers(component) if callable(v) and k[0] != "_"]
173+
class_methods = [
174+
k for k, v in inspect.getmembers(component) if (callable(v) or isinstance(v, property)) and k[0] != "_"
175+
]
174176
if not class_methods:
175177
added_args = parser.add_class_arguments(component, as_group=False, **kwargs)
176178
if not parser.description:
@@ -182,12 +184,13 @@ def _add_component_to_parser(
182184
method_object = getattr(component, method)
183185
description = get_help_str(method_object, parser.logger)
184186
subparser = type(parser)(description=description)
185-
if not has_parameter(getattr(component, method), "config"):
186-
subparser.add_argument("--config", action=ActionConfigFile, help=config_help)
187-
added_subargs = subparser.add_method_arguments(component, method, as_group=False, **kwargs)
188-
added_args += [f"{method}.{a}" for a in added_subargs]
189-
if not added_subargs:
190-
remove_actions(subparser, (ActionConfigFile, _ActionPrintConfig))
187+
if not isinstance(method_object, property):
188+
if not has_parameter(method_object, "config"):
189+
subparser.add_argument("--config", action=ActionConfigFile, help=config_help)
190+
added_subargs = subparser.add_method_arguments(component, method, as_group=False, **kwargs)
191+
added_args += [f"{method}.{a}" for a in added_subargs]
192+
if not added_subargs:
193+
remove_actions(subparser, (ActionConfigFile, _ActionPrintConfig))
191194
subcommands.add_subcommand(method, subparser, help=get_help_str(method_object, parser.logger))
192195
else:
193196
added_args = parser.add_function_arguments(component, as_group=False, **kwargs)
@@ -203,6 +206,8 @@ def _run_component(component, cfg):
203206
subcommand_cfg = cfg.pop(subcommand, {})
204207
subcommand_cfg.pop("config", None)
205208
component_obj = component(**cfg)
209+
if isinstance(getattr(component, subcommand), property):
210+
return getattr(component_obj, subcommand)
206211
component = getattr(component_obj, subcommand)
207212
cfg = subcommand_cfg
208213
if inspect.iscoroutinefunction(component):

jsonargparse_tests/test_cli.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,25 @@ def test_method_with_config_parameter():
237237
assert "p1: 1, config: {}" == out.strip()
238238

239239

240+
class WithProperty:
241+
def __init__(self, num: int = 1):
242+
self._num = num
243+
244+
@property
245+
def prop(self):
246+
"""Description of property"""
247+
return self._num + 1
248+
249+
250+
def test_class_property_value():
251+
assert 3 == auto_cli(WithProperty, args=["--num=2", "prop"])
252+
if docstring_parser_support:
253+
help_str = get_cli_stdout(WithProperty, args=["--help"])
254+
assert "Description of property" in help_str
255+
help_str = get_cli_stdout(WithProperty, args=["prop", "--help"])
256+
assert "Description of property" in help_str
257+
258+
240259
# function and class tests
241260

242261

0 commit comments

Comments
 (0)