-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Combine CLIs into a single one, exposing as an entrypoint. * Update tests for CLI changes * Update python -m references in the docs * Update test.yml workflow * Use older type annotations * Show help when no subcommand is provided * Refactor lint subparser and hook tests * Rename test to match subcommand
- Loading branch information
Showing
9 changed files
with
235 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
- id: numpydoc-validation | ||
name: numpydoc-validation | ||
description: This hook validates that docstrings in committed files adhere to numpydoc standards. | ||
entry: validate-docstrings | ||
entry: numpydoc lint | ||
require_serial: true | ||
language: python | ||
types: [python] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
"""The CLI for numpydoc.""" | ||
|
||
import argparse | ||
import ast | ||
from pathlib import Path | ||
from typing import List, Sequence, Union | ||
|
||
from .docscrape_sphinx import get_doc_object | ||
from .hooks import utils, validate_docstrings | ||
from .validate import ERROR_MSGS, Validator, validate | ||
|
||
|
||
def render_object(import_path: str, config: Union[List[str], None] = None) -> int: | ||
"""Test numpydoc docstring generation for a given object.""" | ||
# TODO: Move Validator._load_obj to a better place than validate | ||
print(get_doc_object(Validator._load_obj(import_path), config=dict(config or []))) | ||
return 0 | ||
|
||
|
||
def validate_object(import_path: str) -> int: | ||
"""Run numpydoc docstring validation for a given object.""" | ||
exit_status = 0 | ||
results = validate(import_path) | ||
for err_code, err_desc in results["errors"]: | ||
exit_status += 1 | ||
print(":".join([import_path, err_code, err_desc])) | ||
return exit_status | ||
|
||
|
||
def get_parser() -> argparse.ArgumentParser: | ||
""" | ||
Build an argument parser. | ||
Returns | ||
------- | ||
argparse.ArgumentParser | ||
The argument parser. | ||
""" | ||
ap = argparse.ArgumentParser(prog="numpydoc", description=__doc__) | ||
subparsers = ap.add_subparsers(title="subcommands") | ||
|
||
def _parse_config(s): | ||
key, _, value = s.partition("=") | ||
value = ast.literal_eval(value) | ||
return key, value | ||
|
||
render = subparsers.add_parser( | ||
"render", | ||
description="Generate an expanded RST-version of the docstring.", | ||
help="generate the RST docstring with numpydoc", | ||
) | ||
render.add_argument("import_path", help="e.g. numpy.ndarray") | ||
render.add_argument( | ||
"-c", | ||
"--config", | ||
type=_parse_config, | ||
action="append", | ||
help="key=val where val will be parsed by literal_eval, " | ||
"e.g. -c use_plots=True. Multiple -c can be used.", | ||
) | ||
render.set_defaults(func=render_object) | ||
|
||
validate = subparsers.add_parser( | ||
"validate", | ||
description="Validate an object's docstring against the numpydoc standard.", | ||
help="validate the object's docstring and report errors", | ||
) | ||
validate.add_argument("import_path", help="e.g. numpy.ndarray") | ||
validate.set_defaults(func=validate_object) | ||
|
||
project_root_from_cwd, config_file = utils.find_project_root(["."]) | ||
config_options = validate_docstrings.parse_config(project_root_from_cwd) | ||
ignored_checks = [ | ||
f"- {check}: {ERROR_MSGS[check]}" | ||
for check in set(ERROR_MSGS.keys()) - config_options["checks"] | ||
] | ||
ignored_checks_text = "\n " + "\n ".join(ignored_checks) + "\n" | ||
|
||
lint_parser = subparsers.add_parser( | ||
"lint", | ||
description="Run numpydoc validation on files with option to ignore individual checks.", | ||
help="validate all docstrings in file(s) using the abstract syntax tree", | ||
formatter_class=argparse.RawTextHelpFormatter, | ||
) | ||
lint_parser.add_argument( | ||
"files", type=str, nargs="+", help="File(s) to run numpydoc validation on." | ||
) | ||
lint_parser.add_argument( | ||
"--config", | ||
type=str, | ||
help=( | ||
"Path to a directory containing a pyproject.toml or setup.cfg file.\n" | ||
"The hook will look for it in the root project directory.\n" | ||
"If both are present, only pyproject.toml will be used.\n" | ||
"Options must be placed under\n" | ||
" - [tool:numpydoc_validation] for setup.cfg files and\n" | ||
" - [tool.numpydoc_validation] for pyproject.toml files." | ||
), | ||
) | ||
lint_parser.add_argument( | ||
"--ignore", | ||
type=str, | ||
nargs="*", | ||
help=( | ||
f"""Check codes to ignore.{ | ||
' Currently ignoring the following from ' | ||
f'{Path(project_root_from_cwd) / config_file}: {ignored_checks_text}' | ||
'Values provided here will be in addition to the above, unless an alternate config is provided.' | ||
if ignored_checks else '' | ||
}""" | ||
), | ||
) | ||
lint_parser.set_defaults(func=validate_docstrings.run_hook) | ||
|
||
return ap | ||
|
||
|
||
def main(argv: Union[Sequence[str], None] = None) -> int: | ||
"""CLI for numpydoc.""" | ||
ap = get_parser() | ||
|
||
args = vars(ap.parse_args(argv)) | ||
|
||
try: | ||
func = args.pop("func") | ||
return func(**args) | ||
except KeyError: | ||
ap.exit(status=2, message=ap.format_help()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.