Skip to content

Commit f8d477c

Browse files
committed
test that explicit bool works for tapify
1 parent a4c0b2d commit f8d477c

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/tap/tapify.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ def tapify(
300300
known_only: bool = False,
301301
command_line_args: Optional[list[str]] = None,
302302
explicit_bool: bool = False,
303+
underscores_to_dashes: bool = False,
303304
description: Optional[str] = None,
304305
**func_kwargs,
305306
) -> OutputType:
@@ -312,6 +313,7 @@ def tapify(
312313
:param explicit_bool: Booleans can be specified on the command line as `--arg True` or `--arg False` rather than
313314
`--arg`. Additionally, booleans can be specified by prefixes of True and False with any
314315
capitalization as well as 1 or 0.
316+
:param underscores_to_dashes: If True, convert underscores in flag names to dashes.
315317
:param description: The description displayed in the help message—the same description passed in
316318
`argparse.ArgumentParser(description=...)`. By default, it's extracted from `class_or_function`'s
317319
docstring.
@@ -327,7 +329,7 @@ def tapify(
327329
# Create a Tap object
328330
if description is None:
329331
description = "\n".join(filter(None, (docstring.short_description, docstring.long_description)))
330-
tap = tap_class(description=description, explicit_bool=explicit_bool)
332+
tap = tap_class(description=description, explicit_bool=explicit_bool, underscores_to_dashes=underscores_to_dashes)
331333

332334
# If any func_kwargs remain, they are not used in the function, so raise an error
333335
known_only = known_only or tap_data.known_only

tests/test_tapify.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,5 +1521,41 @@ def test_tapify_has_kwargs_replace_default(self) -> None:
15211521
self.assertEqual(output, "1_5_c=3-d=4")
15221522

15231523

1524+
class TestTapifyUnderscoresToDashes(unittest.TestCase):
1525+
def setUp(self) -> None:
1526+
class MyClass:
1527+
def __init__(self, my_arg: str):
1528+
self.my_arg = my_arg
1529+
1530+
def __eq__(self, other: str) -> bool:
1531+
return self.my_arg == other
1532+
1533+
@dataclass
1534+
class DataClassTarget:
1535+
my_arg: str
1536+
1537+
def __eq__(self, other: str) -> bool:
1538+
return self.my_arg == other
1539+
1540+
def my_function(my_arg: str) -> str:
1541+
return my_arg
1542+
1543+
self.class_or_functions = [my_function, MyClass, DataClassTarget]
1544+
1545+
def test_underscores_to_dashes(self) -> None:
1546+
for target in self.class_or_functions:
1547+
# With underscores_to_dashes True and using dashes in the args.
1548+
instance = tapify(target, command_line_args=["--my-arg", "value"], underscores_to_dashes=True)
1549+
self.assertEqual(instance, "value")
1550+
1551+
# With underscores_to_dashes False and using underscore in the args.
1552+
instance = tapify(target, command_line_args=["--my_arg", "value"], underscores_to_dashes=False)
1553+
self.assertEqual(instance, "value")
1554+
1555+
# Using underscore when dashes are expected causes a parse error.
1556+
with self.assertRaises(SystemExit):
1557+
tapify(target, command_line_args=["--my_arg", "value"], underscores_to_dashes=True)
1558+
1559+
15241560
if __name__ == "__main__":
15251561
unittest.main()

0 commit comments

Comments
 (0)