forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DOC: Ensure pandas warnings & exceptions are always documented in API…
… docs (pandas-dev#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
- Loading branch information
1 parent
ba4859f
commit 0e6c804
Showing
4 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
""" | ||
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 pathlib | ||
import sys | ||
from typing import Sequence | ||
|
||
API_PATH = pathlib.Path("doc/source/reference/general_utility_functions.rst").resolve() | ||
|
||
|
||
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() |