Skip to content

Commit f798277

Browse files
authored
chore: enhance documentation coverage script (#545)
to include just the docstring coverage by default, provide `-c`/`--code-samples` option for the code sample coverage.
1 parent 0a4153c commit f798277

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

scripts/get_code_sample_coverage.py renamed to scripts/get_documentation_coverage.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import importlib
1717
import inspect
1818
import sys
19-
from typing import Dict, List
19+
import typing
2020

2121
import bigframes
2222
import bigframes.pandas as bpd
@@ -50,6 +50,11 @@
5050
"remote",
5151
]
5252

53+
COVERAGE_GENERATORS = {
54+
"documentation": lambda docstr: docstr,
55+
"code samples": lambda docstr: docstr and "**Examples:**" in docstr,
56+
}
57+
5358
for module_name in ML_MODULE_NAMES:
5459
module = importlib.import_module(f"bigframes.ml.{module_name}")
5560
classes_ = [
@@ -58,9 +63,15 @@
5863
CLASSES.extend(classes_)
5964

6065

61-
def get_code_samples_summary() -> Dict[str, Dict[str, List[str]]]:
66+
def get_coverage_summary(
67+
func: typing.Callable,
68+
) -> typing.Dict[str, typing.Dict[str, typing.List[str]]]:
6269
"""Get Summary of the code samples coverage in BigFrames APIs.
6370
71+
Args:
72+
func (callable):
73+
Function to accept documentation and return whether it satisfies
74+
coverage.
6475
Returns:
6576
Summary: A dictionary of the format
6677
{
@@ -73,7 +84,7 @@ def get_code_samples_summary() -> Dict[str, Dict[str, List[str]]]:
7384
}
7485
}
7586
"""
76-
summary: Dict[str, Dict[str, List[str]]] = dict()
87+
summary: typing.Dict[str, typing.Dict[str, typing.List[str]]] = dict()
7788

7889
for class_ in CLASSES:
7990
class_key = f"{class_.__module__}.{class_.__name__}"
@@ -104,29 +115,39 @@ def predicate(impl):
104115
impl = getattr(class_, name)
105116

106117
docstr = inspect.getdoc(impl)
107-
code_samples_present = docstr and "**Examples:**" in docstr
108-
key = PRESENT if code_samples_present else NOT_PRESENT
118+
coverage_present = func(docstr)
119+
key = PRESENT if coverage_present else NOT_PRESENT
109120
summary[class_key][key].append(name)
110121

111122
return summary
112123

113124

114125
if __name__ == "__main__":
115126
parser = argparse.ArgumentParser(
116-
description="Get a summary of code samples coverage in BigFrames APIs."
127+
description="Get a summary of documentation coverage in BigFrames APIs."
128+
)
129+
parser.add_argument(
130+
"-c",
131+
"--code-samples",
132+
type=bool,
133+
action=argparse.BooleanOptionalAction,
134+
default=False,
135+
help="Whether to calculate code samples coverage. By default the tool"
136+
" calculates the documentation (docstring) coverage.",
117137
)
118138
parser.add_argument(
119139
"-d",
120140
"--details",
121141
type=bool,
122142
action=argparse.BooleanOptionalAction,
123143
default=False,
124-
help="Whether to print APIs with and without code samples.",
144+
help="Whether to print APIs with and without the coverage.",
125145
)
126146

127147
args = parser.parse_args(sys.argv[1:])
128148

129-
summary = get_code_samples_summary()
149+
scenario = "code samples" if args.code_samples else "documentation"
150+
summary = get_coverage_summary(COVERAGE_GENERATORS[scenario])
130151

131152
total_with_code_samples = 0
132153
total = 0
@@ -140,8 +161,8 @@ def predicate(impl):
140161
coverage = 100 * apis_with_code_samples / apis_total
141162
print(f"{class_}: {coverage:.1f}% ({apis_with_code_samples}/{apis_total})")
142163
if args.details:
143-
print(f"===> APIs WITH code samples: {class_summary[PRESENT]}")
144-
print(f"===> APIs WITHOUT code samples: {class_summary[NOT_PRESENT]}")
164+
print(f"===> APIs WITH {scenario}: {class_summary[PRESENT]}")
165+
print(f"===> APIs WITHOUT {scenario}: {class_summary[NOT_PRESENT]}")
145166

146167
coverage = 100 * total_with_code_samples / total
147168
print(f"Total: {coverage:.1f}% ({total_with_code_samples}/{total})")

0 commit comments

Comments
 (0)