A Python module for analyzing functions and extracting metadata including parameters, type hints, and docstrings
Project description
version: 0.1.0
Func Analyzer
A Python library for dynamic function analysis, schema generation, and CLI integration.
Features
- Dynamic Pydantic Model Generation: Automatically create Pydantic models from function signatures
- Intelligent Annotation Cleaning: Convert complex type annotations to clean, readable strings
- Docstring Parsing: Extract parameter descriptions from Google, NumPy, and Sphinx docstring styles
- CLI-Ready Metadata: Generate comprehensive function metadata for CLI auto-generation
- Schema Generation: Create JSON schemas from function signatures with descriptions
Installation
# From local development
pip install -e .
# Or clone and install
git clone https://github.com/crimson206/func-analyzer
cd func-analyzer
pip install -e .
Quick Start
Basic Function Analysis
from func_analyzer import analyze_function
def sample_user_info(name: str, age: int, email: str = None, active: bool = True) -> dict:
"""
Create user information dictionary.
Args:
name: User's full name
age: User's age in years
email: User's email address (optional)
active: Whether user account is active
Returns:
Dictionary containing user information
"""
return {"name": name, "age": age, "email": email, "active": active}
# Analyze function
info = analyze_function(sample_user_info)
print(info)
Dynamic Model Generation
from func_analyzer import create_function_model
# Create Pydantic model from function
UserModel = create_function_model(sample_user_info)
# Use the model
user_data = UserModel(name="John Doe", age=30, email="john@example.com")
print(user_data.model_dump())
Annotation Cleaning
from func_analyzer import clean_annotation_string
# Clean complex annotations
annotations = [
"typing.List[str]",
"typing.Optional[typing.Dict[str, typing.Any]]",
"<class 'str'>",
"Union[str, int, float]"
]
for annotation in annotations:
cleaned = clean_annotation_string(annotation)
print(f"{annotation} -> {cleaned}")
Core Components
Function Analysis
The analyze_function() function extracts comprehensive metadata:
- Function name and module
- Parameter information (name, type, default, description)
- Return type annotation
- Docstring content
- Async/generator status
Dynamic Model Creation
create_function_model() generates Pydantic models with:
- Type annotations from function signature
- Parameter descriptions from docstrings
- Default values
- Field validation
Annotation Cleaning
clean_annotation_string() handles:
typing.prefix removal<class 'type'>string conversion- Complex nested type simplification
- Union type formatting
Examples
See the examples/ directory for comprehensive usage examples:
example_basic_analysis.py- Basic function analysisexample_annotation_cleaning.py- Annotation cleaning with testsexample_docstring_parsing.py- Docstring parsing examples
CLI Integration
The extracted metadata is designed for CLI auto-generation:
# Example CLI generation data
{
'name': 'sample_user_info',
'parameters': [
{
'name': 'name',
'annotation': 'str',
'description': "User's full name",
'default': None
},
{
'name': 'age',
'annotation': 'int',
'description': "User's age in years",
'default': None
}
]
}
Development
Running Examples
# Run all examples
cd examples
python example_basic_analysis.py
python example_annotation_cleaning.py
Testing
# Run tests
python -m pytest tests/
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file func_analyzer-0.1.1.tar.gz.
File metadata
- Download URL: func_analyzer-0.1.1.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
596dc97ba4dbace9410fafb64c5a95a4c37e01a0b42a21a7f0af2a305402f105
|
|
| MD5 |
a1bb89f535875a6528922121e2b26450
|
|
| BLAKE2b-256 |
1272d5c357a209ccb92330aa1f3313e3238179aea71960795c13521537d663b6
|
File details
Details for the file func_analyzer-0.1.1-py3-none-any.whl.
File metadata
- Download URL: func_analyzer-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c074656af5adea2f028a2d7b80756ab02395c25d446bf16098e62ec2ecc4fa1
|
|
| MD5 |
2dfb871a410d80449eee6fee6a1f005a
|
|
| BLAKE2b-256 |
e659636837af75b413a01bb484262748680e7c526a934d219be0cea5ff1c32ff
|