Skip to content

DOC: Remove manual doctesting from validate_docstrings #56871

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 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ fi
### DOCSTRINGS ###
if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then

MSG='Validate docstrings (EX01, EX02, EX04, GL01, GL02, GL03, GL04, GL05, GL06, GL07, GL09, GL10, PR03, PR04, PR05, PR06, PR08, PR09, PR10, RT01, RT02, RT04, RT05, SA02, SA03, SA04, SS01, SS02, SS03, SS04, SS05, SS06)' ; echo $MSG
$BASE_DIR/scripts/validate_docstrings.py --format=actions --errors=EX01,EX02,EX04,GL01,GL02,GL03,GL04,GL05,GL06,GL07,GL09,GL10,PR03,PR04,PR05,PR06,PR08,PR09,PR10,RT01,RT02,RT04,RT05,SA02,SA03,SA04,SS01,SS02,SS03,SS04,SS05,SS06
MSG='Validate docstrings (EX01, EX04, GL01, GL02, GL03, GL04, GL05, GL06, GL07, GL09, GL10, PR03, PR04, PR05, PR06, PR08, PR09, PR10, RT01, RT02, RT04, RT05, SA02, SA03, SA04, SS01, SS02, SS03, SS04, SS05, SS06)' ; echo $MSG
$BASE_DIR/scripts/validate_docstrings.py --format=actions --errors=EX01,EX04,GL01,GL02,GL03,GL04,GL05,GL06,GL07,GL09,GL10,PR03,PR04,PR05,PR06,PR08,PR09,PR10,RT01,RT02,RT04,RT05,SA02,SA03,SA04,SS01,SS02,SS03,SS04,SS05,SS06
RET=$(($RET + $?)) ; echo $MSG "DONE"

MSG='Partially validate docstrings (EX03)' ; echo $MSG
Expand Down
6 changes: 0 additions & 6 deletions scripts/tests/test_validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,6 @@ def test_bad_docstrings(self, capsys, klass, func, msgs) -> None:
for msg in msgs:
assert msg in " ".join([err[1] for err in result["errors"]])

def test_leftover_files_raises(self) -> None:
with pytest.raises(Exception, match="The following files"):
validate_docstrings.pandas_validate(
self._import_path(klass="BadDocstrings", func="leftover_files")
)

def test_validate_all_ignore_functions(self, monkeypatch) -> None:
monkeypatch.setattr(
validate_docstrings,
Expand Down
40 changes: 0 additions & 40 deletions scripts/validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import argparse
import doctest
import importlib
import io
import json
import os
import pathlib
Expand All @@ -28,15 +27,12 @@

import matplotlib
import matplotlib.pyplot as plt
import numpy
from numpydoc.docscrape import get_doc_object
from numpydoc.validate import (
Validator,
validate,
)

import pandas

# With template backend, matplotlib plots nothing
matplotlib.use("template")

Expand All @@ -63,7 +59,6 @@
"GL05": "Use 'array-like' rather than 'array_like' in docstrings.",
"SA05": "{reference_name} in `See Also` section does not need `pandas` "
"prefix, use {right_reference} instead.",
"EX02": "Examples do not pass tests:\n{doctest_log}",
"EX03": "flake8 error: line {line_number}, col {col_number}: {error_code} "
"{error_message}",
"EX04": "Do not import {imported_library}, as it is imported "
Expand Down Expand Up @@ -167,32 +162,6 @@ def name(self):
def mentioned_private_classes(self):
return [klass for klass in PRIVATE_CLASSES if klass in self.raw_doc]

@property
def examples_errors(self):
flags = doctest.NORMALIZE_WHITESPACE | doctest.IGNORE_EXCEPTION_DETAIL
finder = doctest.DocTestFinder()
runner = doctest.DocTestRunner(optionflags=flags)
context = {"np": numpy, "pd": pandas}
error_msgs = ""
current_dir = set(os.listdir())
for test in finder.find(self.raw_doc, self.name, globs=context):
f = io.StringIO()
runner.run(test, out=f.write)
error_msgs += f.getvalue()
leftovers = set(os.listdir()).difference(current_dir)
if leftovers:
for leftover in leftovers:
path = pathlib.Path(leftover).resolve()
if path.is_dir():
path.rmdir()
elif path.is_file():
path.unlink(missing_ok=True)
raise Exception(
f"The following files were leftover from the doctest: "
f"{leftovers}. Please use # doctest: +SKIP"
)
return error_msgs

@property
def examples_source_code(self):
lines = doctest.DocTestParser().get_examples(self.raw_doc)
Expand Down Expand Up @@ -290,12 +259,6 @@ def pandas_validate(func_name: str):

result["examples_errs"] = ""
if doc.examples:
result["examples_errs"] = doc.examples_errors
if result["examples_errs"]:
result["errors"].append(
pandas_error("EX02", doctest_log=result["examples_errs"])
)

for error_code, error_message, line_number, col_number in doc.validate_pep8():
result["errors"].append(
pandas_error(
Expand Down Expand Up @@ -429,9 +392,6 @@ def header(title, width=80, char="#") -> str:
if result["errors"]:
sys.stderr.write(f'{len(result["errors"])} Errors found for `{func_name}`:\n')
for err_code, err_desc in result["errors"]:
if err_code == "EX02": # Failing examples are printed at the end
sys.stderr.write("\tExamples do not pass tests\n")
continue
sys.stderr.write(f"\t{err_desc}\n")
else:
sys.stderr.write(f'Docstring for "{func_name}" correct. :)\n')
Expand Down