Skip to content

Commit 4b6f805

Browse files
committed
Quick hack to get a coverage percentage of the API documentation.
We can't easily use the coverage extension because it counts things documented via the `undoc-members` flag as documented! Actual items that need documenting can also be printed if desired by setting the `undoc_print_objects` flag in conf.py. This is automatically done for RTD builds.
1 parent 09091e3 commit 4b6f805

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

docs/source/conf.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# documentation root, use os.path.abspath to make it absolute, like shown here.
1212
#
1313
import os
14+
from colorama import Fore, Style
1415
from pathlib import Path
1516
from m2r import MdInclude
1617
from recommonmark.transform import AutoStructify
@@ -225,3 +226,42 @@ def setup(app):
225226
img_path=img_path
226227
)
227228
)
229+
230+
app.connect('autodoc-process-docstring', doc_coverage)
231+
app.connect('build-finished', doc_report)
232+
233+
234+
members_to_watch = ['module', 'class', 'function', 'exception', 'method', 'attribute']
235+
doc_count = 0
236+
undoc_count = 0
237+
undoc_objects = []
238+
undoc_print_objects = False
239+
240+
241+
def doc_coverage(app, what, name, obj, options, lines):
242+
global doc_count
243+
global undoc_count
244+
global undoc_objects
245+
246+
if (what in members_to_watch and len(lines) == 0):
247+
# blank docstring detected
248+
undoc_count += 1
249+
undoc_objects.append(name)
250+
else:
251+
doc_count += 1
252+
253+
254+
def doc_report(app, exception):
255+
global doc_count
256+
global undoc_count
257+
global undoc_objects
258+
# print out report of documentation coverage
259+
total_docs = undoc_count + doc_count
260+
if total_docs != 0:
261+
print(f'{Fore.GREEN}Doc coverage of {doc_count/total_docs:.1%}{Style.RESET_ALL}')
262+
if undoc_print_objects or os.environ.get('READTHEDOCS'):
263+
print('\nItems lacking documentation')
264+
print('===========================')
265+
print(*undoc_objects, sep='\n')
266+
else:
267+
print(f'{Fore.YELLOW}No docs counted, run \'make clean\' then rebuild to get the count.{Style.RESET_ALL}')

0 commit comments

Comments
 (0)