Skip to content

Commit 487aafb

Browse files
authored
CLN: clean doc validation script (#42436)
1 parent 8d64fe9 commit 487aafb

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

scripts/validate_docstrings.py

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,26 @@
1717

1818
import argparse
1919
import doctest
20-
import glob
2120
import importlib
21+
import io
2222
import json
23-
import os
23+
import pathlib
2424
import subprocess
2525
import sys
2626
import tempfile
2727

28-
try:
29-
from io import StringIO
30-
except ImportError:
31-
from cStringIO import StringIO
28+
import matplotlib
29+
import matplotlib.pyplot as plt
30+
import numpy
31+
from numpydoc.validate import (
32+
Docstring,
33+
validate,
34+
)
3235

33-
# Template backend makes matplotlib to not plot anything. This is useful
34-
# to avoid that plot windows are open from the doctests while running the
35-
# script. Setting here before matplotlib is loaded.
36-
# We don't warn for the number of open plots, as none is actually being opened
37-
os.environ["MPLBACKEND"] = "Template"
38-
import matplotlib # isort:skip
36+
import pandas
3937

40-
matplotlib.rc("figure", max_open_warning=10000)
41-
42-
import numpy # isort:skip
43-
44-
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
45-
46-
sys.path.insert(0, os.path.join(BASE_PATH))
47-
import pandas # isort:skip
48-
49-
sys.path.insert(1, os.path.join(BASE_PATH, "doc", "sphinxext"))
50-
from numpydoc.validate import validate, Docstring # isort:skip
38+
# With template backend, matplotlib plots nothing
39+
matplotlib.use("template")
5140

5241

5342
PRIVATE_CLASSES = ["NDFrame", "IndexOpsMixin"]
@@ -157,7 +146,7 @@ def examples_errors(self):
157146
context = {"np": numpy, "pd": pandas}
158147
error_msgs = ""
159148
for test in finder.find(self.raw_doc, self.name, globs=context):
160-
f = StringIO()
149+
f = io.StringIO()
161150
runner.run(test, out=f.write)
162151
error_msgs += f.getvalue()
163152
return error_msgs
@@ -263,6 +252,7 @@ def pandas_validate(func_name: str):
263252
if doc.non_hyphenated_array_like():
264253
result["errors"].append(pandas_error("GL05"))
265254

255+
plt.close("all")
266256
return result
267257

268258

@@ -288,13 +278,14 @@ def validate_all(prefix, ignore_deprecated=False):
288278
result = {}
289279
seen = {}
290280

291-
api_doc_fnames = os.path.join(BASE_PATH, "doc", "source", "reference", "*.rst")
281+
base_path = pathlib.Path(__file__).parent.parent
282+
api_doc_fnames = pathlib.Path(base_path, "doc", "source", "reference")
292283
api_items = []
293-
for api_doc_fname in glob.glob(api_doc_fnames):
284+
for api_doc_fname in api_doc_fnames.glob("*.rst"):
294285
with open(api_doc_fname) as f:
295286
api_items += list(get_api_items(f))
296287

297-
for func_name, func_obj, section, subsection in api_items:
288+
for func_name, _, section, subsection in api_items:
298289
if prefix and not func_name.startswith(prefix):
299290
continue
300291
doc_info = pandas_validate(func_name)

0 commit comments

Comments
 (0)