-
-
Notifications
You must be signed in to change notification settings - Fork 169
Unify CLIs #537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Unify CLIs #537
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
73f9a59
Combine CLIs into a single one, exposing as an entrypoint.
stefmolin d1d43c1
Update tests for CLI changes
stefmolin 02f5e5d
Update python -m references in the docs
stefmolin 007c2ca
Update test.yml workflow
stefmolin f10e786
Use older type annotations
stefmolin 5c79fe6
Merge branch 'main' into cli-alignment
stefmolin 3008354
Merge branch 'main' into cli-alignment
stefmolin e87a4d1
Show help when no subcommand is provided
stefmolin ec69d5e
Refactor lint subparser and hook tests
stefmolin 6e8491a
Merge branch 'main' into cli-alignment
stefmolin 66400a3
Rename test to match subcommand
stefmolin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.