@@ -25,9 +25,6 @@ def get_param_descr(fn: Callable, param_name: str) -> str:
25
25
fn (Callable): The function of method with a __doc__ docstring implemented.
26
26
param_name (str): The parameter for which to extract the description.
27
27
28
- Raises:
29
- ValueError: If docstring is not found or parameter is not found in docstring.
30
-
31
28
Returns:
32
29
str: The description of the parameter.
33
30
"""
@@ -38,32 +35,27 @@ def get_param_descr(fn: Callable, param_name: str) -> str:
38
35
f'Could not find parameter { param_name } in docstring of { fn } ' )
39
36
lines = docstring .splitlines ()
40
37
41
- # collect all lines beginning after args, also get indentation space_chars
38
+ # Find Args section
42
39
for i , line in enumerate (lines ):
43
40
if line .lstrip ().startswith ('Args:' ):
44
41
break
42
+ else : # No Args section found
43
+ raise ValueError (f'No Args section found in docstring of { fn } ' )
45
44
46
45
lines = lines [i + 1 :]
47
-
48
- first_already_found = False
49
- return_lines = []
50
-
51
- # allow but don't require type in docstring
52
46
first_line_args_regex = rf'^{ param_name } ( \(.*\))?: '
53
- for line in lines :
54
- if not first_already_found and re .match (first_line_args_regex ,
55
- line .lstrip ()):
56
- new_line = re .sub (first_line_args_regex , '' , line .strip ())
57
- return_lines .append (new_line )
58
- first_already_found = True
59
- first_indentation_level = len (line ) - len (line .lstrip ())
60
- continue
61
-
62
- if first_already_found :
63
- indentation_level = len (line ) - len (line .lstrip ())
64
- if indentation_level <= first_indentation_level :
65
- return ' ' .join (return_lines )
66
- else :
67
- return_lines .append (line .strip ())
47
+ for i , line in enumerate (lines ):
48
+ stripped = line .lstrip ()
49
+ match = re .match (first_line_args_regex , stripped )
50
+ if match :
51
+ description = [re .sub (first_line_args_regex , '' , stripped )]
52
+ # Collect any additional lines that are more indented
53
+ current_indent = len (line ) - len (stripped )
54
+ for next_line in lines [i + 1 :]:
55
+ next_indent = len (next_line ) - len (next_line .lstrip ())
56
+ if next_indent <= current_indent :
57
+ break
58
+ description .append (next_line .strip ())
59
+ return ' ' .join (description )
68
60
raise ValueError (
69
61
f'Could not find parameter { param_name } in docstring of { fn } ' )
0 commit comments