File tree Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -95,6 +95,9 @@ def __init__(self,
95
95
# Get annotations from self and all super classes up through tap
96
96
self ._annotations = self ._get_annotations ()
97
97
98
+ # Set the default description to be the docstring
99
+ kwargs .setdefault ('description' , self .__doc__ )
100
+
98
101
# Initialize the super class, i.e. ArgumentParser
99
102
super (Tap , self ).__init__ (* args , ** kwargs )
100
103
@@ -110,8 +113,6 @@ def __init__(self,
110
113
# Indicate that initialization is complete
111
114
self ._initialized = True
112
115
113
- self .description = self .__doc__ or ''
114
-
115
116
def _add_argument (self , * name_or_flags , ** kwargs ) -> None :
116
117
"""Adds an argument to self (i.e. the super class ArgumentParser).
117
118
Original file line number Diff line number Diff line change @@ -1590,5 +1590,36 @@ def configure(self):
1590
1590
self .assertIn ('<Sub Parser>' , help_desc )
1591
1591
self .assertIn ('<Root Parser>' , help_desc )
1592
1592
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
+
1593
1624
if __name__ == '__main__' :
1594
1625
unittest .main ()
You can’t perform that action at this time.
0 commit comments