Skip to content

Commit b6505ff

Browse files
authored
Merge pull request #139 from kddubey/tapify-description
Optional descriptions can now be manually specified (e.g., with `tapify(..., description="My Description")`).
2 parents 8b974d7 + 6bc2284 commit b6505ff

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

tap/tapify.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,17 +300,21 @@ def tapify(
300300
known_only: bool = False,
301301
command_line_args: Optional[List[str]] = None,
302302
explicit_bool: bool = False,
303+
description: Optional[str] = None,
303304
**func_kwargs,
304305
) -> OutputType:
305306
"""Tapify initializes a class or runs a function by parsing arguments from the command line.
306307
307308
:param class_or_function: The class or function to run with the provided arguments.
308309
:param known_only: If true, ignores extra arguments and only parses known arguments.
309-
:param command_line_args: A list of command line style arguments to parse (e.g., ['--arg', 'value']). If None,
310+
:param command_line_args: A list of command line style arguments to parse (e.g., `['--arg', 'value']`). If None,
310311
arguments are parsed from the command line (default behavior).
311-
:param explicit_bool: Booleans can be specified on the command line as "--arg True" or "--arg False" rather than
312-
"--arg". Additionally, booleans can be specified by prefixes of True and False with any
312+
:param explicit_bool: Booleans can be specified on the command line as `--arg True` or `--arg False` rather than
313+
`--arg`. Additionally, booleans can be specified by prefixes of True and False with any
313314
capitalization as well as 1 or 0.
315+
:param description: The description displayed in the help message—the same description passed in
316+
`argparse.ArgumentParser(description=...)`. By default, it's extracted from `class_or_function`'s
317+
docstring.
314318
:param func_kwargs: Additional keyword arguments for the function. These act as default values when parsing the
315319
command line arguments and overwrite the function defaults but are overwritten by the parsed
316320
command line arguments.
@@ -320,8 +324,9 @@ def tapify(
320324
param_to_description = {param.arg_name: param.description for param in docstring.params}
321325
tap_data = _tap_data(class_or_function, param_to_description, func_kwargs)
322326
tap_class = _tap_class(tap_data.args_data)
323-
# Create a Tap object with a description from the docstring of the class or function
324-
description = "\n".join(filter(None, (docstring.short_description, docstring.long_description)))
327+
# Create a Tap object
328+
if description is None:
329+
description = "\n".join(filter(None, (docstring.short_description, docstring.long_description)))
325330
tap = tap_class(description=description, explicit_bool=explicit_bool)
326331

327332
# If any func_kwargs remain, they are not used in the function, so raise an error

tests/test_tapify.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,15 @@ def concat(a: int, b: int, c: int) -> str:
12171217
"""
12181218
return f"{a} {b} {c}"
12191219

1220+
def concat_without_docstring_description(a: int, b: int, c: int) -> str:
1221+
"""
1222+
:param a: The first number.
1223+
:param b: The second number.
1224+
:param c: The third number.
1225+
"""
1226+
# For this function, docstring.short_description and docstring.long_description are None
1227+
return f"{a} {b} {c}"
1228+
12201229
def concat_with_positionals(a: int, b: int, /, c: int) -> str:
12211230
"""Concatenate three numbers.
12221231
@@ -1305,6 +1314,7 @@ def __eq__(self, other: str) -> bool:
13051314
else:
13061315
pydantic_data_models = []
13071316

1317+
expected_description = "Concatenate three numbers."
13081318
for class_or_function in [
13091319
concat,
13101320
concat_with_positionals,
@@ -1315,10 +1325,14 @@ def __eq__(self, other: str) -> bool:
13151325
f = io.StringIO()
13161326
with contextlib.redirect_stdout(f):
13171327
with self.assertRaises(SystemExit):
1318-
tapify(class_or_function, command_line_args=["-h"])
1328+
if class_or_function == concat_without_docstring_description:
1329+
tapify(class_or_function, command_line_args=["-h"], description=expected_description)
1330+
else:
1331+
tapify(class_or_function, command_line_args=["-h"])
13191332

1333+
# TODO: change the assertIn checks to instead check exact match like in test_to_tap_class
13201334
stdout = f.getvalue()
1321-
self.assertIn("Concatenate three numbers.", stdout)
1335+
self.assertIn(expected_description, stdout)
13221336
self.assertIn("--a A (int, required) The first number.", stdout)
13231337
self.assertIn("--b B (int, required) The second number.", stdout)
13241338
self.assertIn("--c C (int, required) The third number.", stdout)

0 commit comments

Comments
 (0)