Caffe2 - Python API
A deep learning, cross platform ML framework
test_docs_coverage.py
1 import torch
2 import unittest
3 import os
4 import re
5 import ast
6 import _ast
7 
8 
9 path = os.path.dirname(os.path.realpath(__file__))
10 rstpath = os.path.join(path, '../docs/source/')
11 pypath = os.path.join(path, '../torch/_torch_docs.py')
12 r1 = re.compile(r'\.\. autofunction:: (\w*)')
13 
14 
15 class TestDocCoverage(unittest.TestCase):
16 
17  def test_torch(self):
18  # get symbols documented in torch.rst
19  whitelist = [
20  'set_printoptions', 'get_rng_state', 'is_storage', 'initial_seed',
21  'set_default_tensor_type', 'load', 'save', 'set_default_dtype',
22  'is_tensor', 'compiled_with_cxx11_abi', 'set_rng_state',
23  'manual_seed'
24  ]
25  everything = set()
26  filename = os.path.join(rstpath, 'torch.rst')
27  with open(filename, 'r') as f:
28  lines = f.readlines()
29  for l in lines:
30  l = l.strip()
31  name = r1.findall(l)
32  if name:
33  everything.add(name[0])
34  everything -= set(whitelist)
35  # get symbols in functional.py and _torch_docs.py
36  whitelist2 = ['product', 'inf', 'math', 'reduce', 'warnings', 'torch', 'annotate']
37  everything2 = set()
38  with open(pypath, 'r') as f:
39  body = ast.parse(f.read()).body
40  for i in body:
41  if not isinstance(i, _ast.Expr):
42  continue
43  i = i.value
44  if not isinstance(i, _ast.Call):
45  continue
46  if i.func.id != 'add_docstr':
47  continue
48  i = i.args[0]
49  if i.value.id != 'torch':
50  continue
51  i = i.attr
52  everything2.add(i)
53  for p in dir(torch.functional):
54  if not p.startswith('_') and p[0].islower():
55  everything2.add(p)
56  everything2 -= set(whitelist2)
57  # assert they are equal
58  for p in everything:
59  self.assertIn(p, everything2, 'in torch.rst but not in python')
60  for p in everything2:
61  self.assertIn(p, everything, 'in python but not in torch.rst')
62 
63 
64 if __name__ == '__main__':
65  unittest.main()