mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Migrate to py3 style type annotation: sphinx.ext.coverage
This commit is contained in:
parent
eb07a0fee1
commit
0832291883
@ -14,30 +14,25 @@ import inspect
|
|||||||
import pickle
|
import pickle
|
||||||
import re
|
import re
|
||||||
from os import path
|
from os import path
|
||||||
|
from typing import Any, Dict, IO, List, Pattern, Set, Tuple
|
||||||
|
|
||||||
import sphinx
|
import sphinx
|
||||||
|
from sphinx.application import Sphinx
|
||||||
from sphinx.builders import Builder
|
from sphinx.builders import Builder
|
||||||
from sphinx.locale import __
|
from sphinx.locale import __
|
||||||
from sphinx.util import logging
|
from sphinx.util import logging
|
||||||
from sphinx.util.inspect import safe_getattr
|
from sphinx.util.inspect import safe_getattr
|
||||||
|
|
||||||
if False:
|
|
||||||
# For type annotation
|
|
||||||
from typing import Any, Dict, IO, List, Pattern, Set, Tuple # NOQA
|
|
||||||
from sphinx.application import Sphinx # NOQA
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
# utility
|
# utility
|
||||||
def write_header(f, text, char='-'):
|
def write_header(f: IO, text: str, char: str = '-') -> None:
|
||||||
# type:(IO, str, str) -> None
|
|
||||||
f.write(text + '\n')
|
f.write(text + '\n')
|
||||||
f.write(char * len(text) + '\n')
|
f.write(char * len(text) + '\n')
|
||||||
|
|
||||||
|
|
||||||
def compile_regex_list(name, exps):
|
def compile_regex_list(name: str, exps: str) -> List[Pattern]:
|
||||||
# type: (str, str) -> List[Pattern]
|
|
||||||
lst = []
|
lst = []
|
||||||
for exp in exps:
|
for exp in exps:
|
||||||
try:
|
try:
|
||||||
@ -55,8 +50,7 @@ class CoverageBuilder(Builder):
|
|||||||
epilog = __('Testing of coverage in the sources finished, look at the '
|
epilog = __('Testing of coverage in the sources finished, look at the '
|
||||||
'results in %(outdir)s' + path.sep + 'python.txt.')
|
'results in %(outdir)s' + path.sep + 'python.txt.')
|
||||||
|
|
||||||
def init(self):
|
def init(self) -> None:
|
||||||
# type: () -> None
|
|
||||||
self.c_sourcefiles = [] # type: List[str]
|
self.c_sourcefiles = [] # type: List[str]
|
||||||
for pattern in self.config.coverage_c_path:
|
for pattern in self.config.coverage_c_path:
|
||||||
pattern = path.join(self.srcdir, pattern)
|
pattern = path.join(self.srcdir, pattern)
|
||||||
@ -82,12 +76,10 @@ class CoverageBuilder(Builder):
|
|||||||
self.py_ignorexps = compile_regex_list('coverage_ignore_pyobjects',
|
self.py_ignorexps = compile_regex_list('coverage_ignore_pyobjects',
|
||||||
self.config.coverage_ignore_pyobjects)
|
self.config.coverage_ignore_pyobjects)
|
||||||
|
|
||||||
def get_outdated_docs(self):
|
def get_outdated_docs(self) -> str:
|
||||||
# type: () -> str
|
|
||||||
return 'coverage overview'
|
return 'coverage overview'
|
||||||
|
|
||||||
def write(self, *ignored):
|
def write(self, *ignored) -> None:
|
||||||
# type: (Any) -> None
|
|
||||||
self.py_undoc = {} # type: Dict[str, Dict[str, Any]]
|
self.py_undoc = {} # type: Dict[str, Dict[str, Any]]
|
||||||
self.build_py_coverage()
|
self.build_py_coverage()
|
||||||
self.write_py_coverage()
|
self.write_py_coverage()
|
||||||
@ -96,8 +88,7 @@ class CoverageBuilder(Builder):
|
|||||||
self.build_c_coverage()
|
self.build_c_coverage()
|
||||||
self.write_c_coverage()
|
self.write_c_coverage()
|
||||||
|
|
||||||
def build_c_coverage(self):
|
def build_c_coverage(self) -> None:
|
||||||
# type: () -> None
|
|
||||||
# Fetch all the info from the header files
|
# Fetch all the info from the header files
|
||||||
c_objects = self.env.domaindata['c']['objects']
|
c_objects = self.env.domaindata['c']['objects']
|
||||||
for filename in self.c_sourcefiles:
|
for filename in self.c_sourcefiles:
|
||||||
@ -118,8 +109,7 @@ class CoverageBuilder(Builder):
|
|||||||
if undoc:
|
if undoc:
|
||||||
self.c_undoc[filename] = undoc
|
self.c_undoc[filename] = undoc
|
||||||
|
|
||||||
def write_c_coverage(self):
|
def write_c_coverage(self) -> None:
|
||||||
# type: () -> None
|
|
||||||
output_file = path.join(self.outdir, 'c.txt')
|
output_file = path.join(self.outdir, 'c.txt')
|
||||||
with open(output_file, 'w') as op:
|
with open(output_file, 'w') as op:
|
||||||
if self.config.coverage_write_headline:
|
if self.config.coverage_write_headline:
|
||||||
@ -138,8 +128,7 @@ class CoverageBuilder(Builder):
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def build_py_coverage(self):
|
def build_py_coverage(self) -> None:
|
||||||
# type: () -> None
|
|
||||||
objects = self.env.domaindata['py']['objects']
|
objects = self.env.domaindata['py']['objects']
|
||||||
modules = self.env.domaindata['py']['modules']
|
modules = self.env.domaindata['py']['modules']
|
||||||
|
|
||||||
@ -230,8 +219,7 @@ class CoverageBuilder(Builder):
|
|||||||
|
|
||||||
self.py_undoc[mod_name] = {'funcs': funcs, 'classes': classes}
|
self.py_undoc[mod_name] = {'funcs': funcs, 'classes': classes}
|
||||||
|
|
||||||
def write_py_coverage(self):
|
def write_py_coverage(self) -> None:
|
||||||
# type: () -> None
|
|
||||||
output_file = path.join(self.outdir, 'python.txt')
|
output_file = path.join(self.outdir, 'python.txt')
|
||||||
failed = []
|
failed = []
|
||||||
with open(output_file, 'w') as op:
|
with open(output_file, 'w') as op:
|
||||||
@ -266,16 +254,14 @@ class CoverageBuilder(Builder):
|
|||||||
write_header(op, 'Modules that failed to import')
|
write_header(op, 'Modules that failed to import')
|
||||||
op.writelines(' * %s -- %s\n' % x for x in failed)
|
op.writelines(' * %s -- %s\n' % x for x in failed)
|
||||||
|
|
||||||
def finish(self):
|
def finish(self) -> None:
|
||||||
# type: () -> None
|
|
||||||
# dump the coverage data to a pickle file too
|
# dump the coverage data to a pickle file too
|
||||||
picklepath = path.join(self.outdir, 'undoc.pickle')
|
picklepath = path.join(self.outdir, 'undoc.pickle')
|
||||||
with open(picklepath, 'wb') as dumpfile:
|
with open(picklepath, 'wb') as dumpfile:
|
||||||
pickle.dump((self.py_undoc, self.c_undoc), dumpfile)
|
pickle.dump((self.py_undoc, self.c_undoc), dumpfile)
|
||||||
|
|
||||||
|
|
||||||
def setup(app):
|
def setup(app: Sphinx) -> Dict[str, Any]:
|
||||||
# type: (Sphinx) -> Dict[str, Any]
|
|
||||||
app.add_builder(CoverageBuilder)
|
app.add_builder(CoverageBuilder)
|
||||||
app.add_config_value('coverage_ignore_modules', [], False)
|
app.add_config_value('coverage_ignore_modules', [], False)
|
||||||
app.add_config_value('coverage_ignore_functions', [], False)
|
app.add_config_value('coverage_ignore_functions', [], False)
|
||||||
|
Loading…
Reference in New Issue
Block a user