Skip to content

DOC: Ensure pandas warnings & exceptions are always documented in API docs #43029

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 5 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,8 @@ repos:
entry: python scripts/no_bool_in_generic.py
language: python
files: ^pandas/core/generic\.py$
- id: pandas-errors-documented
name: Ensure pandas errors are documented in doc/source/reference/general_utility_functions.rst
entry: python scripts/pandas_errors_documented.py
language: python
files: ^pandas/errors/__init__.py$
3 changes: 3 additions & 0 deletions doc/source/reference/general_utility_functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ Exceptions and warnings
.. autosummary::
:toctree: api/

errors.AbstractMethodError
errors.AccessorRegistrationWarning
errors.DtypeWarning
errors.DuplicateLabelError
errors.EmptyDataError
errors.InvalidIndexError
errors.IntCastingNaNError
errors.MergeError
errors.NullFrequencyError
errors.NumbaUtilError
errors.OptionError
errors.OutOfBoundsDatetime
errors.OutOfBoundsTimedelta
errors.ParserError
Expand Down
2 changes: 1 addition & 1 deletion pandas/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class IntCastingNaNError(ValueError):
"""
raised when attempting an astype operation on an array with NaN to an integer
Raised when attempting an astype operation on an array with NaN to an integer
dtype.
"""

Expand Down
51 changes: 51 additions & 0 deletions scripts/pandas_errors_documented.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Check that doc/source/reference/general_utility_functions.rst documents
all exceptions and warnings in pandas/errors/__init__.py.

This is meant to be run as a pre-commit hook - to run it manually, you can do:

pre-commit run pandas-errors-documented --all-files
"""
from __future__ import annotations

import argparse
import ast
import sys
from typing import Sequence

API_PATH = "doc/source/reference/general_utility_functions.rst"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this work if someone runs this hook on Windows?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call!



def get_defined_errors(content: str) -> set[str]:
errors = set()
for node in ast.walk(ast.parse(content)):
if isinstance(node, ast.ClassDef):
errors.add(node.name)
elif isinstance(node, ast.ImportFrom):
for alias in node.names:
errors.add(alias.name)
return errors


def main(argv: Sequence[str] | None = None) -> None:
parser = argparse.ArgumentParser()
parser.add_argument("path")
args = parser.parse_args(argv)
with open(args.path, encoding="utf-8") as f:
file_errors = get_defined_errors(f.read())
with open(API_PATH) as f:
doc_errors = {
line.split(".")[1].strip() for line in f.readlines() if "errors" in line
}
missing = file_errors.difference(doc_errors)
if missing:
sys.stdout.write(
f"The follow exceptions and/or warnings are not documented "
f"in {API_PATH}: {missing}"
)
sys.exit(1)
sys.exit(0)


if __name__ == "__main__":
main()