Skip to content

Commit d1e4d50

Browse files
committed
Adjust help description to accomodate overriding
1 parent c5359c3 commit d1e4d50

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

tap/tap.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ def __init__(self,
9595
# Get annotations from self and all super classes up through tap
9696
self._annotations = self._get_annotations()
9797

98+
# Set the default description to be the docstring
99+
kwargs.setdefault('description', self.__doc__)
100+
98101
# Initialize the super class, i.e. ArgumentParser
99102
super(Tap, self).__init__(*args, **kwargs)
100103

@@ -110,8 +113,6 @@ def __init__(self,
110113
# Indicate that initialization is complete
111114
self._initialized = True
112115

113-
self.description = self.__doc__ or ''
114-
115116
def _add_argument(self, *name_or_flags, **kwargs) -> None:
116117
"""Adds an argument to self (i.e. the super class ArgumentParser).
117118

tests/test_integration.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,5 +1590,36 @@ def configure(self):
15901590
self.assertIn('<Sub Parser>', help_desc)
15911591
self.assertIn('<Root Parser>', help_desc)
15921592

1593+
def test_empty_docstring_should_be_empty(self):
1594+
class NoDesc(Tap):
1595+
field: str = 'a'
1596+
1597+
root_parser = NoDesc()
1598+
help_info = root_parser.format_help()
1599+
help_flag = '[-h]'
1600+
desc_start = help_info.index(help_flag) + len(help_flag)
1601+
desc_end = help_info.index('options:')
1602+
desc = help_info[desc_start: desc_end].strip()
1603+
self.assertEqual(desc, '')
1604+
1605+
def test_manual_description_still_works(self):
1606+
class NoDesc(Tap):
1607+
field: str = 'a'
1608+
1609+
expected_help_desc = 'I exist'
1610+
help_desc = NoDesc(description=expected_help_desc).format_help()
1611+
self.assertIn(expected_help_desc, help_desc)
1612+
1613+
def test_manual_description_overwrites_docstring(self):
1614+
class Desc(Tap):
1615+
"""I do not exist"""
1616+
field: str = 'a'
1617+
1618+
expected_help_desc = 'I exist'
1619+
help_desc = Desc(description=expected_help_desc).format_help()
1620+
self.assertIn(expected_help_desc, help_desc)
1621+
self.assertNotIn("I do not exist", help_desc)
1622+
1623+
15931624
if __name__ == '__main__':
15941625
unittest.main()

0 commit comments

Comments
 (0)