Skip to content

Commit f826416

Browse files
committed
if no docstring, create error GL08 and don't validate
1 parent 35b0d1d commit f826416

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

scripts/validate_docstrings.py

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@
6969
}
7070
ALL_ERRORS = set(NUMPYDOC_ERROR_MSGS).union(set(ERROR_MSGS))
7171
duplicated_errors = set(NUMPYDOC_ERROR_MSGS).intersection(set(ERROR_MSGS))
72-
assert not duplicated_errors, (f"Errors {duplicated_errors} exist in both pandas "
73-
"and numpydoc, should they be removed from pandas?")
72+
assert not duplicated_errors, (
73+
f"Errors {duplicated_errors} exist in both pandas "
74+
"and numpydoc, should they be removed from pandas?"
75+
)
7476

7577

7678
def pandas_error(code, **kwargs):
@@ -245,7 +247,13 @@ def pandas_validate(func_name: str):
245247
# Some objects are instances, e.g. IndexSlice, which numpydoc can't validate
246248
doc_obj = get_doc_object(func_obj, doc=func_obj.__doc__)
247249
doc = PandasDocstring(func_name, doc_obj)
248-
result = validate(doc_obj)
250+
if func_obj.__doc__ is not None:
251+
result = validate(doc_obj)
252+
else:
253+
result = {
254+
"docstring": "",
255+
"errors": [("GL08", "The object does not have a docstring")],
256+
}
249257
mentioned_errs = doc.mentioned_private_classes
250258
if mentioned_errs:
251259
result["errors"].append(
@@ -257,7 +265,7 @@ def pandas_validate(func_name: str):
257265
pandas_error(
258266
"SA05",
259267
reference_name=rel_name,
260-
right_reference=rel_name[len("pandas."):],
268+
right_reference=rel_name[len("pandas.") :],
261269
)
262270
for rel_name in doc.see_also
263271
if rel_name.startswith("pandas.")
@@ -365,12 +373,13 @@ def print_validate_all_results(
365373
for func_name, res in result.items():
366374
error_messages = dict(res["errors"])
367375
actual_failures = set(error_messages)
368-
expected_failures = (ignore_errors.get(func_name, set())
369-
| ignore_errors.get(None, set()))
376+
expected_failures = ignore_errors.get(func_name, set()) | ignore_errors.get(
377+
None, set()
378+
)
370379
for err_code in actual_failures - expected_failures:
371380
sys.stdout.write(
372381
f'{prefix}{res["file"]}:{res["file_line"]}:'
373-
f'{err_code}:{func_name}:{error_messages[err_code]}\n'
382+
f"{err_code}:{func_name}:{error_messages[err_code]}\n"
374383
)
375384
exit_status += 1
376385
for err_code in ignore_errors.get(func_name, set()) - actual_failures:
@@ -384,8 +393,9 @@ def print_validate_all_results(
384393
return exit_status
385394

386395

387-
def print_validate_one_results(func_name: str,
388-
ignore_errors: dict[str, set[str]]) -> int:
396+
def print_validate_one_results(
397+
func_name: str, ignore_errors: dict[str, set[str]]
398+
) -> int:
389399
def header(title, width=80, char="#") -> str:
390400
full_line = char * width
391401
side_len = (width - len(title) - 2) // 2
@@ -396,8 +406,11 @@ def header(title, width=80, char="#") -> str:
396406

397407
result = pandas_validate(func_name)
398408

399-
result["errors"] = [(code, message) for code, message in result["errors"]
400-
if code not in ignore_errors.get(None, set())]
409+
result["errors"] = [
410+
(code, message)
411+
for code, message in result["errors"]
412+
if code not in ignore_errors.get(None, set())
413+
]
401414

402415
sys.stderr.write(header(f"Docstring ({func_name})"))
403416
sys.stderr.write(f"{result['docstring']}\n")
@@ -431,14 +444,16 @@ def _format_ignore_errors(raw_ignore_errors):
431444
raise ValueError(
432445
f"Object `{obj_name}` is present in more than one "
433446
"--ignore_errors argument. Please use it once and specify "
434-
"the errors separated by commas.")
447+
"the errors separated by commas."
448+
)
435449
ignore_errors[obj_name] = set(error_codes.split(","))
436450

437451
unknown_errors = ignore_errors[obj_name] - ALL_ERRORS
438452
if unknown_errors:
439453
raise ValueError(
440454
f"Object `{obj_name}` is ignoring errors {unknown_errors} "
441-
f"which are not known. Known errors are: {ALL_ERRORS}")
455+
f"which are not known. Known errors are: {ALL_ERRORS}"
456+
)
442457

443458
# global errors "PR02,ES01"
444459
else:
@@ -448,27 +463,19 @@ def _format_ignore_errors(raw_ignore_errors):
448463
if unknown_errors:
449464
raise ValueError(
450465
f"Unknown errors {unknown_errors} specified using --ignore_errors "
451-
"Known errors are: {ALL_ERRORS}")
466+
"Known errors are: {ALL_ERRORS}"
467+
)
452468

453469
return ignore_errors
454470

455471

456-
def main(
457-
func_name,
458-
output_format,
459-
prefix,
460-
ignore_deprecated,
461-
ignore_errors
462-
):
472+
def main(func_name, output_format, prefix, ignore_deprecated, ignore_errors):
463473
"""
464474
Main entry point. Call the validation for one or for all docstrings.
465475
"""
466476
if func_name is None:
467477
return print_validate_all_results(
468-
output_format,
469-
prefix,
470-
ignore_deprecated,
471-
ignore_errors
478+
output_format, prefix, ignore_deprecated, ignore_errors
472479
)
473480
else:
474481
return print_validate_one_results(func_name, ignore_errors)
@@ -524,10 +531,11 @@ def main(
524531
args = argparser.parse_args(sys.argv[1:])
525532

526533
sys.exit(
527-
main(args.function,
528-
args.format,
529-
args.prefix,
530-
args.ignore_deprecated,
531-
_format_ignore_errors(args.ignore_errors),
532-
)
534+
main(
535+
args.function,
536+
args.format,
537+
args.prefix,
538+
args.ignore_deprecated,
539+
_format_ignore_errors(args.ignore_errors),
540+
)
533541
)

0 commit comments

Comments
 (0)