Skip to content

Commit 1fb156f

Browse files
committed
CLN: clean doc validation script
1 parent 0a9cfcf commit 1fb156f

File tree

1 file changed

+24
-35
lines changed

1 file changed

+24
-35
lines changed

scripts/validate_docstrings.py

+24-35
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,28 @@
1515
"""
1616
from __future__ import annotations
1717

18-
import argparse
18+
from argparse import ArgumentParser
1919
import doctest
20-
import glob
21-
import importlib
22-
import json
23-
import os
24-
import subprocess
20+
from importlib import import_module
21+
from io import StringIO
22+
from json import dumps
23+
from pathlib import Path
24+
from subprocess import run
2525
import sys
26-
import tempfile
26+
from tempfile import NamedTemporaryFile
2727

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

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
35+
import pandas
3936

37+
matplotlib.rcParams["backend"] = "template"
4038
matplotlib.rc("figure", max_open_warning=10000)
4139

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
51-
5240

5341
PRIVATE_CLASSES = ["NDFrame", "IndexOpsMixin"]
5442
ERROR_MSGS = {
@@ -130,7 +118,7 @@ def get_api_items(api_doc_fd):
130118
position = None
131119
continue
132120
item = line.strip()
133-
func = importlib.import_module(current_module)
121+
func = import_module(current_module)
134122
for part in item.split("."):
135123
func = getattr(func, part)
136124

@@ -182,11 +170,11 @@ def validate_pep8(self):
182170
)
183171

184172
error_messages = []
185-
with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8") as file:
173+
with NamedTemporaryFile(mode="w", encoding="utf-8") as file:
186174
file.write(content)
187175
file.flush()
188176
cmd = ["python", "-m", "flake8", "--quiet", "--statistics", file.name]
189-
response = subprocess.run(cmd, capture_output=True, text=True)
177+
response = run(cmd, capture_output=True, text=True)
190178
stdout = response.stdout
191179
stdout = stdout.replace(file.name, "")
192180
messages = stdout.strip("\n")
@@ -288,13 +276,14 @@ def validate_all(prefix, ignore_deprecated=False):
288276
result = {}
289277
seen = {}
290278

291-
api_doc_fnames = os.path.join(BASE_PATH, "doc", "source", "reference", "*.rst")
279+
base_path = Path(__file__).parent.parent
280+
api_doc_fnames = Path(base_path, "doc", "source", "reference")
292281
api_items = []
293-
for api_doc_fname in glob.glob(api_doc_fnames):
282+
for api_doc_fname in api_doc_fnames.glob("*.rst"):
294283
with open(api_doc_fname) as f:
295284
api_items += list(get_api_items(f))
296285

297-
for func_name, func_obj, section, subsection in api_items:
286+
for func_name, _, section, subsection in api_items:
298287
if prefix and not func_name.startswith(prefix):
299288
continue
300289
doc_info = pandas_validate(func_name)
@@ -330,7 +319,7 @@ def print_validate_all_results(
330319
result = validate_all(prefix, ignore_deprecated)
331320

332321
if output_format == "json":
333-
sys.stdout.write(json.dumps(result))
322+
sys.stdout.write(dumps(result))
334323
return 0
335324

336325
prefix = "##[error]" if output_format == "actions" else ""
@@ -398,7 +387,7 @@ def main(func_name, prefix, errors, output_format, ignore_deprecated):
398387
"if not provided, all docstrings are validated and returned "
399388
"as JSON"
400389
)
401-
argparser = argparse.ArgumentParser(description="validate pandas docstrings")
390+
argparser = ArgumentParser(description="validate pandas docstrings")
402391
argparser.add_argument("function", nargs="?", default=None, help=func_help)
403392
argparser.add_argument(
404393
"--format",

0 commit comments

Comments
 (0)