Skip to content

Commit 483f057

Browse files
authored
DOC: Ensure pandas warnings & exceptions are always documented in API docs (#43029)
* DOC: Ensure all pandas.errors are documents in general_utility_functions.rst * Use ast to check for the errors * Use pathlib to ensure Windows support
1 parent b706e4a commit 483f057

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

.pre-commit-config.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,8 @@ repos:
164164
entry: python scripts/no_bool_in_generic.py
165165
language: python
166166
files: ^pandas/core/generic\.py$
167+
- id: pandas-errors-documented
168+
name: Ensure pandas errors are documented in doc/source/reference/general_utility_functions.rst
169+
entry: python scripts/pandas_errors_documented.py
170+
language: python
171+
files: ^pandas/errors/__init__.py$

doc/source/reference/general_utility_functions.rst

+3
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ Exceptions and warnings
3535
.. autosummary::
3636
:toctree: api/
3737

38+
errors.AbstractMethodError
3839
errors.AccessorRegistrationWarning
3940
errors.DtypeWarning
4041
errors.DuplicateLabelError
4142
errors.EmptyDataError
4243
errors.InvalidIndexError
44+
errors.IntCastingNaNError
4345
errors.MergeError
4446
errors.NullFrequencyError
4547
errors.NumbaUtilError
48+
errors.OptionError
4649
errors.OutOfBoundsDatetime
4750
errors.OutOfBoundsTimedelta
4851
errors.ParserError

pandas/errors/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class IntCastingNaNError(ValueError):
1616
"""
17-
raised when attempting an astype operation on an array with NaN to an integer
17+
Raised when attempting an astype operation on an array with NaN to an integer
1818
dtype.
1919
"""
2020

scripts/pandas_errors_documented.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Check that doc/source/reference/general_utility_functions.rst documents
3+
all exceptions and warnings in pandas/errors/__init__.py.
4+
5+
This is meant to be run as a pre-commit hook - to run it manually, you can do:
6+
7+
pre-commit run pandas-errors-documented --all-files
8+
"""
9+
from __future__ import annotations
10+
11+
import argparse
12+
import ast
13+
import pathlib
14+
import sys
15+
from typing import Sequence
16+
17+
API_PATH = pathlib.Path("doc/source/reference/general_utility_functions.rst").resolve()
18+
19+
20+
def get_defined_errors(content: str) -> set[str]:
21+
errors = set()
22+
for node in ast.walk(ast.parse(content)):
23+
if isinstance(node, ast.ClassDef):
24+
errors.add(node.name)
25+
elif isinstance(node, ast.ImportFrom):
26+
for alias in node.names:
27+
errors.add(alias.name)
28+
return errors
29+
30+
31+
def main(argv: Sequence[str] | None = None) -> None:
32+
parser = argparse.ArgumentParser()
33+
parser.add_argument("path")
34+
args = parser.parse_args(argv)
35+
with open(args.path, encoding="utf-8") as f:
36+
file_errors = get_defined_errors(f.read())
37+
with open(API_PATH) as f:
38+
doc_errors = {
39+
line.split(".")[1].strip() for line in f.readlines() if "errors" in line
40+
}
41+
missing = file_errors.difference(doc_errors)
42+
if missing:
43+
sys.stdout.write(
44+
f"The follow exceptions and/or warnings are not documented "
45+
f"in {API_PATH}: {missing}"
46+
)
47+
sys.exit(1)
48+
sys.exit(0)
49+
50+
51+
if __name__ == "__main__":
52+
main()

0 commit comments

Comments
 (0)