pytorch/test/test_docs_coverage.py
Edward Yang 173f224570 Turn on F401: Unused import warning. (#18598)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/18598
ghimport-source-id: c74597e5e7437e94a43c163cee0639b20d0d0c6a

Stack from [ghstack](https://github.com/ezyang/ghstack):
* **#18598 Turn on F401: Unused import warning.**

This was requested by someone at Facebook; this lint is turned
on for Facebook by default.  "Sure, why not."

I had to noqa a number of imports in __init__.  Hypothetically
we're supposed to use __all__ in this case, but I was too lazy
to fix it.  Left for future work.

Be careful!  flake8-2 and flake8-3 behave differently with
respect to import resolution for # type: comments.  flake8-3 will
report an import unused; flake8-2 will not.  For now, I just
noqa'd all these sites.

All the changes were done by hand.

Signed-off-by: Edward Z. Yang <ezyang@fb.com>

Differential Revision: D14687478

fbshipit-source-id: 30d532381e914091aadfa0d2a5a89404819663e3
2019-03-30 09:01:17 -07:00

82 lines
3.0 KiB
Python

import torch
import unittest
import os
import re
import textwrap
path = os.path.dirname(os.path.realpath(__file__))
rstpath = os.path.join(path, '../docs/source/')
pypath = os.path.join(path, '../torch/_torch_docs.py')
r1 = re.compile(r'\.\. autofunction:: (\w*)')
r2 = re.compile(r'\.\. auto(?:method|attribute):: (\w*)')
class TestDocCoverage(unittest.TestCase):
@staticmethod
def parse_rst(filename, regex):
filename = os.path.join(rstpath, filename)
ret = set()
with open(filename, 'r') as f:
lines = f.readlines()
for l in lines:
l = l.strip()
name = regex.findall(l)
if name:
ret.add(name[0])
return ret
def test_torch(self):
# get symbols documented in torch.rst
in_rst = self.parse_rst('torch.rst', r1)
# get symbols in functional.py and _torch_docs.py
whitelist = {
# below are some jit functions
'wait', 'fork', 'parse_type_comment', 'import_ir_module',
'import_ir_module_from_buffer', 'merge_type_from_type_comment',
# below are symbols mistakely binded to torch.*, but should
# go to torch.nn.functional.* instead
'avg_pool1d', 'conv_transpose2d', 'conv_transpose1d', 'conv3d',
'relu_', 'pixel_shuffle', 'conv2d', 'selu_', 'celu_', 'threshold_',
'cosine_similarity', 'rrelu_', 'conv_transpose3d', 'conv1d', 'pdist',
'adaptive_avg_pool1d', 'conv_tbc'
}
has_docstring = set(
a for a in dir(torch)
if getattr(torch, a).__doc__ and not a.startswith('_') and
'function' in type(getattr(torch, a)).__name__)
self.assertEqual(
has_docstring & whitelist, whitelist,
textwrap.dedent('''
The whitelist in test_docs_coverage.py contains something
that don't have docstring or not in torch.*. If you just
removed something from torch.*, please remove it from whiltelist
in test_docs_coverage.py'''))
has_docstring -= whitelist
# assert they are equal
self.assertEqual(
has_docstring, in_rst,
textwrap.dedent('''
List of functions documented in torch.rst and in python are different.
Do you forget to add new thing to torch.rst, or whitelist things you
don't want to document?''')
)
def test_tensor(self):
in_rst = self.parse_rst('tensors.rst', r2)
classes = [torch.FloatTensor, torch.LongTensor, torch.ByteTensor]
has_docstring = set(x for c in classes for x in dir(c) if not x.startswith('_') and getattr(c, x).__doc__)
self.assertEqual(
has_docstring, in_rst,
textwrap.dedent('''
List of tensor methods documented in tensor.rst and in python are
different. Do you forget to add new thing to tensor.rst, or whitelist
things you don't want to document?''')
)
if __name__ == '__main__':
unittest.main()