5
5
import re
6
6
7
7
8
- def get_docstring_args (docstring ):
8
+ def get_docstring_args (fd ):
9
9
"""
10
- Extract parameter arguments from docstring
10
+ Extract docstring parameters from function definition
11
11
"""
12
+ docstring = ast .get_docstring (fd )
12
13
args = re .findall (r"(\w+)\s+\:" , docstring )
13
- args = [a for a in args if a != "self" ]
14
+ args = set ( [a for a in args ])
14
15
return args
15
16
16
17
18
+ def get_signature_args (fd ):
19
+ """
20
+ Extract signature arguments from function definition
21
+ """
22
+ return set ([a .arg for a in fd .args .args if a .arg != "self" ])
23
+
24
+
25
+ def check_args (doc_args , sig_args , file_name , func_name , class_name = None ):
26
+ """
27
+ Compare docstring arguments and signature argments
28
+ """
29
+ diff_args = signature_args .difference (docstring_args )
30
+ if len (diff_args ) > 0 :
31
+ msg = "Found one or more arguments/parameters with missing docstring in \n "
32
+ msg += f"file: { file_name } \n "
33
+ if class_name is not None :
34
+ msg += f"class: { class_name } \n "
35
+ msg += f"function/method: { func_name } \n "
36
+ msg += f"parameter(s): { diff_args } \n "
37
+ raise RuntimeError (msg )
38
+
39
+
17
40
ignore = ["__init__.py" , "__pycache__" ]
18
41
19
42
stumpy_path = pathlib .Path (__file__ ).parent / "stumpy"
@@ -30,17 +53,9 @@ def get_docstring_args(docstring):
30
53
node for node in module .body if isinstance (node , ast .FunctionDef )
31
54
]
32
55
for fd in function_definitions :
33
- docstring_args = set (get_docstring_args (ast .get_docstring (fd )))
34
- signature_args = set ([a .arg for a in fd .args .args ])
35
- diff_args = signature_args .difference (docstring_args )
36
- if len (diff_args ) > 0 :
37
- print ("Found one or more parameters with missing docstring:" )
38
- print (f" File: { filepath .name } " )
39
- print (f" Function: { fd .name } " )
40
- print (f" Parameters: { diff_args } " )
41
- # print(ast.get_docstring(fd))
42
- # print(docstring_args)
43
- # print(signature_args)
56
+ docstring_args = get_docstring_args (fd )
57
+ signature_args = get_signature_args (fd )
58
+ check_args (docstring_args , signature_args , filepath .name , fd .name )
44
59
45
60
# Check Class Methods
46
61
class_definitions = [
@@ -49,15 +64,8 @@ def get_docstring_args(docstring):
49
64
for cd in class_definitions :
50
65
methods = [node for node in cd .body if isinstance (node , ast .FunctionDef )]
51
66
for fd in methods :
52
- docstring_args = set (get_docstring_args (ast .get_docstring (fd )))
53
- signature_args = set ([a .arg for a in fd .args .args if a .arg != "self" ])
54
- diff_args = signature_args .difference (docstring_args )
55
- if len (diff_args ) > 0 :
56
- print ("Found one or more parameters with missing docstring:" )
57
- print (f" File: { filepath .name } " )
58
- print (f" Class: { cd .name } " )
59
- print (f" Method: { fd .name } " )
60
- print (f" Parameters: { diff_args } " )
61
- # print(ast.get_docstring(fd))
62
- # print(docstring_args)
63
- # print(signature_args)
67
+ docstring_args = get_docstring_args (fd )
68
+ signature_args = get_signature_args (fd )
69
+ check_args (
70
+ docstring_args , signature_args , filepath .name , fd .name , cd .name
71
+ )
0 commit comments