-
-
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
Unify CLIs #537
Changes from 7 commits
73f9a59
d1d43c1
02f5e5d
007c2ca
f10e786
5c79fe6
3008354
e87a4d1
ec69d5e
6e8491a
66400a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"""The CLI for numpydoc.""" | ||
|
||
import argparse | ||
import ast | ||
from typing import List, Sequence, Union | ||
|
||
from .docscrape_sphinx import get_doc_object | ||
from .hooks import validate_docstrings | ||
from .validate import 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 main(argv: Union[Sequence[str], None] = None) -> int: | ||
"""CLI for numpydoc.""" | ||
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="Test numpydoc docstring generation for a given object.", | ||
stefmolin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
help="generate the 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 the docstring with numpydoc.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we clarify here the distinction between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it works for non-functions as well. How about this?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what to do about the second part of your comment. If you run There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Validate that an object's docstring conforms to the numpydoc standard" (added "that") There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or, "Determine (or check?) whether an object's docstring..." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Otherwise just: "Validate an object's docstring against the numpydoc standard" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I may prefer the last version, but anything gramatically sound is fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This was more of a question: should there be the option of disabling certain rules? Why are these not exactly equivalent in their functionality? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
While I agree this should eventually be the case, this definitely seems out of scope for this PR. At least the separate subcommands will not draw attention to arguments that have no effect like before (see #459). |
||
help="validate the object and report errors", | ||
) | ||
validate.add_argument("import_path", help="e.g. numpy.ndarray") | ||
validate.set_defaults(func=validate_object) | ||
|
||
lint_parser = validate_docstrings.get_parser(parent=subparsers) | ||
stefmolin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lint_parser.set_defaults(func=validate_docstrings.run_hook) | ||
|
||
args = vars(ap.parse_args(argv)) | ||
func = args.pop("func", render_object) | ||
return func(**args) |
Uh oh!
There was an error while loading. Please reload this page.