Implementation:Huggingface Transformers Check Docstrings
| Knowledge Sources | |
|---|---|
| Domains | Code_Quality, Documentation |
| Last Updated | 2026-02-13 20:00 GMT |
Overview
Concrete tool for validating that docstrings of public classes and functions have argument sections matching their actual signatures.
Description
The check_docstrings.py utility (2028 lines) uses AST parsing and inspect to extract function signatures and their default values, then parses docstring argument blocks using regex patterns. It compares documented arguments against actual parameters, checking for missing/extra args, mismatched types, and incorrect default values. Supports @auto_docstring decorator detection for auto-generated docs. Handles special cases like ModelOutput subclasses and ProcessorMixin. Can auto-fix issues with --fix_and_overwrite. This is one of the largest repository quality tools, critical for maintaining documentation accuracy across the entire library.
Usage
Run as part of make check-repo to validate docstring accuracy, or with --fix_and_overwrite to auto-correct defaults and formatting.
Code Reference
Source Location
- Repository: Huggingface_Transformers
- File: utils/check_docstrings.py
- Lines: 1-2028
Signature
def has_auto_docstring_decorator(obj: Any) -> bool:
"""Check if a class/function has the @auto_docstring decorator."""
def stringify_default(default: Any) -> str:
"""Convert a default value to its string representation for docstrings."""
def replace_default_in_arg_description(
description: str,
arg_name: str,
new_default: str,
) -> str:
"""Replace the default value in a docstring argument description."""
def check_docstrings(overwrite: bool = False) -> List[str]:
"""Check all public object docstrings and return list of errors."""
Import
python utils/check_docstrings.py
python utils/check_docstrings.py --fix_and_overwrite
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| src/transformers/ | Directory | Yes | Source files with public objects to check |
| --fix_and_overwrite | flag | No | Auto-fix docstring issues |
Outputs
| Name | Type | Description |
|---|---|---|
| Error report | stdout | List of docstring inconsistencies |
| Updated files | Python files | Modified files (if --fix_and_overwrite) |
Usage Examples
Checking Docstrings
# Check all docstrings
python utils/check_docstrings.py
# Auto-fix docstring defaults
python utils/check_docstrings.py --fix_and_overwrite