Griffe
Griffe is a Python library (current version 2.0.2) that provides signatures for entire Python programs. It extracts the structure, frame, and skeleton of a project, enabling tasks like API documentation generation and detection of breaking API changes. The library maintains a regular release cadence, with frequent updates including bug fixes, features, and occasional deprecations or breaking changes between major versions.
Warnings
- breaking Version 2.0.0 removed several previously deprecated public APIs, including `ExportedName`, `infer_docstring_style`, `parse_auto`, `parse_google`, `parse_numpy`, `parse_sphinx`, `assert_git_repo`, `get_latest_tag`, `get_repo_root`, and `tmp_worktree`.
- deprecated The signature of the `on_alias` event changed in version 1.14.0. It changed from `on_alias(self, *, node: AST | ObjectNode, alias: Alias, agent: Visitor | Inspector, **kwargs)` (an analysis event) to `on_alias(self, *, alias: Alias, loader: GriffeLoader, **kwargs)` (a load event).
- gotcha Internal packages and modules, particularly those starting with an underscore (e.g., `_griffe`), may be refactored or moved without prior deprecation. Version 1.11.1 moved the private `_griffe` package under `griffe._internal`.
Install
-
pip install griffe -
pip install griffe[pypi]
Imports
- griffe
import griffe
Quickstart
import griffe
import os
# Create a dummy Python file for demonstration
with open('my_dummy_module.py', 'w') as f:
f.write('def my_function(param1: str, param2: int = 0) -> str:\n """A dummy function."""\n return f"{param1}-{param2}"\n\nclass MyClass:\n """A dummy class."""\n def __init__(self, name: str):\n self.name = name\n\n def greet(self) -> str:\n """Greets by name."""\n return f"Hello, {self.name}"\n')
# Load the module's API data
# Griffe will try to find sources and fall back to introspection if not found
# Using the current directory as a search path
package = griffe.load("my_dummy_module", search_paths=['.'])
# Access a module, class, or function
print(f"Package name: {package.name}")
my_function = package["my_function"]
print(f"Function: {my_function.name}, Parameters: {[p.name for p in my_function.parameters]}")
my_class = package["MyClass"]
print(f"Class: {my_class.name}, Methods: {[m.name for m in my_class.members.values() if m.is_function]}")
# Clean up the dummy file
os.remove('my_dummy_module.py')