询问docstring覆盖范围的代码库。
interrogate的Python项目详细描述
interrogate:解释一下
询问docstring覆盖率的代码库。在
我为什么需要这个?
interrogate检查代码基中是否缺少docstring。在
文档应该和代码本身一样重要。而且它应该在代码中存在。Pythonstandardizeddocstrings,允许开发人员在库中导航,就像在对象上调用help(),并使用功能强大的工具,如Sphinx、pydoc、Docutils自动生成HTML、LaTeX、pdf等
回车:interrogate。在
interrogate将告诉您哪些方法、函数、类和模块具有docstring,哪些没有。使用interrogate执行以下操作:
- 了解代码的文档化程度
- 将其添加到CI/CD检查中,以强制记录新添加的代码
- 为代码质量和可维护性评估一个新的代码库。在
我们开始吧。在
要求
interrogate支持Python3.6及更高版本。在
用法
在Python项目中尝试:
^{pr2}$添加详细信息以查看摘要:
$ interrogate -v [PATH] ================== Coverage for /Users/lynn/dev/interrogate/ ==================== ------------------------------------ Summary ------------------------------------ | Name | Total | Miss | Cover | Cover% | |---------------------------------------|---------|--------|---------|----------| | src/interrogate/__init__.py | 1 | 0 | 1 | 100% | | src/interrogate/__main__.py | 1 | 0 | 1 | 100% | | src/interrogate/badge_gen.py | 5 | 0 | 5 | 100% | | src/interrogate/cli.py | 2 | 0 | 2 | 100% | | src/interrogate/config.py | 8 | 0 | 8 | 100% | | src/interrogate/coverage.py | 25 | 0 | 25 | 100% | | src/interrogate/utils.py | 10 | 0 | 10 | 100% | | src/interrogate/visit.py | 16 | 0 | 16 | 100% | | tests/functional/__init__.py | 1 | 0 | 1 | 100% | | tests/functional/test_cli.py | 7 | 0 | 7 | 100% | | tests/functional/test_coverage.py | 6 | 0 | 6 | 100% | | tests/unit/__init__.py | 1 | 0 | 1 | 100% | | tests/unit/test_badge_gen.py | 6 | 0 | 6 | 100% | | tests/unit/test_config.py | 10 | 0 | 10 | 100% | | tests/unit/test_utils.py | 13 | 0 | 13 | 100% | |---------------------------------------|---------|--------|---------|----------| | TOTAL | 112 | 0 | 112 | 100.0% | ---------------- RESULT: PASSED (minimum: 95.0%, actual: 100.0%) ----------------
添加偶数more详细信息:
$ interrogate -vv [PATH] ================== Coverage for /Users/lynn/dev/interrogate/ ==================== ------------------------------- Detailed Coverage ------------------------------- | Name | Status | |---------------------------------------------------------------------|---------| | src/interrogate/__init__.py (module) | COVERED | |---------------------------------------------------------------------|---------| | src/interrogate/__main__.py (module) | COVERED | |---------------------------------------------------------------------|---------| | src/interrogate/badge_gen.py (module) | COVERED | | save_badge (L33) | COVERED | | get_badge (L50) | COVERED | | get_color (L66) | COVERED | | create (L79) | COVERED | |---------------------------------------------------------------------|---------| | src/interrogate/cli.py (module) | COVERED | | main (L218) | COVERED | |---------------------------------------------------------------------|---------| | src/interrogate/config.py (module) | COVERED | | InterrogateConfig (L17) | COVERED | | find_project_root (L52) | COVERED | | find_project_config (L80) | COVERED | | parse_pyproject_toml (L91) | COVERED | | sanitize_list_values (L107) | COVERED | | parse_setup_cfg (L130) | COVERED | | read_config_file (L164) | COVERED | |---------------------------------------------------------------------|---------| | src/interrogate/coverage.py (module) | COVERED | | BaseInterrogateResult (L21) | COVERED | | BaseInterrogateResult.perc_covered (L35) | COVERED | | InterrogateFileResult (L49) | COVERED | | InterrogateFileResult.combine (L62) | COVERED | | InterrogateResults (L76) | COVERED | | InterrogateResults.combine (L88) | COVERED | | InterrogateCoverage (L96) | COVERED | | InterrogateCoverage._add_common_exclude (L115) | COVERED | | InterrogateCoverage._filter_files (L122) | COVERED | | InterrogateCoverage.get_filenames_from_paths (L139) | COVERED | | InterrogateCoverage._filter_nodes (L166) | COVERED | | InterrogateCoverage._get_file_coverage (L192) | COVERED | | InterrogateCoverage._get_coverage (L218) | COVERED | | InterrogateCoverage.get_coverage (L235) | COVERED | | InterrogateCoverage._get_filename (L240) | COVERED | | InterrogateCoverage._get_detailed_row (L251) | COVERED | | InterrogateCoverage._create_detailed_table (L268) | COVERED | | InterrogateCoverage._create_detailed_table._sort_nodes (L275) | COVERED | | InterrogateCoverage._print_detailed_table (L297) | COVERED | | InterrogateCoverage._create_summary_table (L315) | COVERED | | InterrogateCoverage._print_summary_table (L349) | COVERED | | InterrogateCoverage._sort_results (L367) | COVERED | | InterrogateCoverage._get_header_base (L397) | COVERED | | InterrogateCoverage.print_results (L406) | COVERED | |---------------------------------------------------------------------|---------| | src/interrogate/utils.py (module) | COVERED | | parse_regex (L22) | COVERED | | smart_open (L41) | COVERED | | get_common_base (L61) | COVERED | | OutputFormatter (L81) | COVERED | | OutputFormatter.should_markup (L91) | COVERED | | OutputFormatter.set_detailed_markup (L106) | COVERED | | OutputFormatter.set_summary_markup (L130) | COVERED | | OutputFormatter._interrogate_line_formatter (L159) | COVERED | | OutputFormatter.get_table_formatter (L222) | COVERED | |---------------------------------------------------------------------|---------| | src/interrogate/visit.py (module) | COVERED | | CovNode (L15) | COVERED | | CoverageVisitor (L42) | COVERED | | CoverageVisitor._has_doc (L56) | COVERED | | CoverageVisitor._visit_helper (L63) | COVERED | | CoverageVisitor._is_nested (L109) | COVERED | | CoverageVisitor._is_private (L118) | COVERED | | CoverageVisitor._is_semiprivate (L126) | COVERED | | CoverageVisitor._is_ignored_common (L136) | COVERED | | CoverageVisitor._has_property_decorators (L153) | COVERED | | CoverageVisitor._is_func_ignored (L167) | COVERED | | CoverageVisitor._is_class_ignored (L188) | COVERED | | CoverageVisitor.visit_Module (L192) | COVERED | | CoverageVisitor.visit_ClassDef (L199) | COVERED | | CoverageVisitor.visit_FunctionDef (L208) | COVERED | | CoverageVisitor.visit_AsyncFunctionDef (L217) | COVERED | |---------------------------------------------------------------------|---------| | tests/functional/__init__.py (module) | COVERED | |---------------------------------------------------------------------|---------| | tests/functional/test_cli.py (module) | COVERED | | runner (L22) | COVERED | | test_run_no_paths (L30) | COVERED | | test_run_shortflags (L71) | COVERED | | test_run_longflags (L97) | COVERED | | test_run_multiple_flags (L115) | COVERED | | test_generate_badge (L126) | COVERED | |---------------------------------------------------------------------|---------| | tests/functional/test_coverage.py (module) | COVERED | | test_coverage_simple (L42) | COVERED | | test_coverage_errors (L55) | COVERED | | test_print_results (L83) | COVERED | | test_print_results_ignore_module (L117) | COVERED | | test_print_results_single_file (L144) | COVERED | |---------------------------------------------------------------------|---------| | tests/unit/__init__.py (module) | COVERED | |---------------------------------------------------------------------|---------| | tests/unit/test_badge_gen.py (module) | COVERED | | test_save_badge (L25) | COVERED | | test_save_badge_windows (L47) | COVERED | | test_get_badge (L61) | COVERED | | test_get_color (L85) | COVERED | | test_create (L102) | COVERED | |---------------------------------------------------------------------|---------| | tests/unit/test_config.py (module) | COVERED | | test_find_project_root (L29) | COVERED | | test_find_project_config (L45) | COVERED | | test_parse_pyproject_toml (L54) | COVERED | | test_sanitize_list_values (L84) | COVERED | | test_parse_setup_cfg (L89) | COVERED | | test_parse_setup_cfg_raises (L114) | COVERED | | test_read_config_file_none (L125) | COVERED | | test_read_config_file (L184) | COVERED | | test_read_config_file_raises (L198) | COVERED | |---------------------------------------------------------------------|---------| | tests/unit/test_utils.py (module) | COVERED | | test_parse_regex (L32) | COVERED | | test_smart_open (L39) | COVERED | | test_get_common_base (L69) | COVERED | | test_get_common_base_windows (L100) | COVERED | | test_output_formatter_should_markup (L132) | COVERED | | test_output_formatter_set_detailed_markup (L163) | COVERED | | test_output_formatter_set_summary_markup (L206) | COVERED | | test_output_formatter_interrogate_line_formatter (L258) | COVERED | | test_output_formatter_interrogate_line_formatter_windows (L319) | COVERED | | test_output_formatter_get_table_formatter (L343) | COVERED | | test_output_formatter_get_table_formatter_py38 (L381) | COVERED | | test_output_formatter_get_table_formatter_raises (L395) | COVERED | |---------------------------------------------------------------------|---------| ------------------------------------ Summary ------------------------------------ | Name | Total | Miss | Cover | Cover% | |---------------------------------------|---------|--------|---------|----------| | src/interrogate/__init__.py | 1 | 0 | 1 | 100% | | src/interrogate/__main__.py | 1 | 0 | 1 | 100% | | src/interrogate/badge_gen.py | 5 | 0 | 5 | 100% | | src/interrogate/cli.py | 2 | 0 | 2 | 100% | | src/interrogate/config.py | 8 | 0 | 8 | 100% | | src/interrogate/coverage.py | 25 | 0 | 25 | 100% | | src/interrogate/utils.py | 10 | 0 | 10 | 100% | | src/interrogate/visit.py | 16 | 0 | 16 | 100% | | tests/functional/__init__.py | 1 | 0 | 1 | 100% | | tests/functional/test_cli.py | 7 | 0 | 7 | 100% | | tests/functional/test_coverage.py | 6 | 0 | 6 | 100% | | tests/unit/__init__.py | 1 | 0 | 1 | 100% | | tests/unit/test_badge_gen.py | 6 | 0 | 6 | 100% | | tests/unit/test_config.py | 10 | 0 | 10 | 100% | | tests/unit/test_utils.py | 13 | 0 | 13 | 100% | |---------------------------------------|---------|--------|---------|----------| | TOTAL | 112 | 0 | 112 | 100.0% | ---------------- RESULT: PASSED (minimum: 95.0%, actual: 100.0%) ----------------
其他用法
生成一个shields.io徽章(就像这个!):
$ interrogate --generate-badge PATH RESULT: PASSED (minimum: 80.0%, actual: 100.0%) Generated badge to /Users/lynn/dev/interrogate/docs/_static/interrogate_badge.svg
将其添加到您的tox.ini文件中以强制执行覆盖级别:
[testenv:doc]deps=interrogateskip_install=truecommands= interrogate --quiet --fail-under 95 src tests
或与pre-commit一起使用:
repos:-repo:https://github.com/econchick/interrogaterev:1.3.2# or master if you're boldhooks:-id:interrogateargs:[--quiet,--fail-under=95]
直接在代码中使用它:
>>> frominterrogateimportcoverage>>> cov=coverage.InterrogateCoverage(paths=["src"])>>> results=cov.get_coverage()>>> resultsInterrogateResults(total=68, covered=65, missing=3)
将interrogate与GitHub Actions一起使用。查看由Jack McKew编写并维护的action(谢谢你,杰克!)。在
配置
在pyproject.toml内配置(interrogate将自动检测pyproject.toml文件并为命令行选项选择默认值):
$ interrogate -c pyproject.toml [OPTIONS][PATHS]...
[tool.interrogate]ignore-init-method=trueignore-init-module=falseignore-magic=falseignore-semiprivate=falseignore-private=falseignore-property-decorators=falseignore-module=falsefail-under=95exclude=["setup.py", "docs", "build"]ignore-regex=["^get$", "^mock_.*", ".*BaseClass.*"]verbose=0quiet=falsewhitelist-regex=[]color=true
或者在您的setup.cfg中配置(interrogate将自动检测setup.cfg文件并为命令行选项选择默认值):
$ interrogate -c setup.cfg [OPTIONS][PATHS]...
[tool:interrogate]ignore-init-method=trueignore-init-module=falseignore-magic=falseignore-semiprivate=falseignore-private=falseignore-property-decorators=falseignore-module=falsefail-under=95exclude=setup.py,docs,buildignore-regex=^get$,^mock_.*,.*BaseClass.*verbose=0quiet=falsewhitelist-regex=color=true
警告
除非是非常简单的用例,否则不建议使用setup.cfg。.cfg文件使用与pyproject.toml不同的解析器,这可能会导致难以跟踪的问题。如果可能,建议使用pyproject.toml来定义询问配置。在
要查看所有可用选项,请运行interrogate --help:
interrogate -h
Usage: interrogate [OPTIONS] [PATHS]...
Measure and report on documentation coverage in Python modules.
Options:
--version Show the version and exit.
-v, --verbose Level of verbosity [default: 0]
-q, --quiet Do not print output [default: False]
-f, --fail-under INT | FLOAT Fail when coverage % is less than a given
amount. [default: 80.0]
-e, --exclude PATH Exclude PATHs of files and/or directories.
Multiple `-e/--exclude` invocations
supported.
-i, --ignore-init-method Ignore `__init__` method of classes.
[default: False]
-I, --ignore-init-module Ignore `__init__.py` modules. [default:
False]
-m, --ignore-magic Ignore all magic methods of classes.
[default: False]
NOTE: This does not include the `__init__`
method. To ignore `__init__` methods, use
`--ignore-init-method`.
-M, --ignore-module Ignore module-level docstrings. [default:
False]
-n, --ignore-nested-functions Ignore nested functions and methods.
[default: False]
-p, --ignore-private Ignore private classes, methods, and
functions starting with two underscores.
[default: False]
NOTE: This does not include magic methods;
use `--ignore-magic` and/or `--ignore-init-
method` instead.
-P, --ignore-property-decorators
Ignore methods with property setter/getter
decorators. [default: False]
-s, --ignore-semiprivate Ignore semiprivate classes, methods, and
functions starting with a single underscore.
[default: False]
-r, --ignore-regex STR Regex identifying class, method, and
function names to ignore. Multiple
`-r/--ignore-regex` invocations supported.
-w, --whitelist-regex STR Regex identifying class, method, and
function names to include. Multiple
`-w/--whitelist-regex` invocations
supported.
-o, --output FILE Write output to a given FILE. [default:
stdout]
--color / --no-color Toggle color output on/off when printing to
stdout. [default: True]
-g, --generate-badge PATH Generate a 'shields.io' status badge (an SVG
image) in at a given file or directory. Will
not generate a badge if results did not
change from an existing badge of the same
path.
-h, --help Show this message and exit.
-c, --config FILE Read configuration from `pyproject.toml` or
`setup.cfg`.
学分
interrogate的灵感来源于^{tt23}$,这是从亚历克赛“数据贪婪”斯特里科夫的^{tt24}$派生出来的,它的灵感来自2004年的recipe from James Harlow。在
可爱的徽标是由JustineW通过the Noun Project购买的(但也可以在Creative Commons License下获得,带有属性)。在
- 项目
标签: