Skip to main content

A tool to track where Python constructs are used and update docstrings

Project description

uzpy

A production-ready Python tool that automatically analyzes code usage patterns and updates docstrings with "Used in:" documentation.

Python 3.11+ License: MIT Status: Production Ready

uzpy scans Python codebases to find where each function, class, and method is used, then automatically updates their docstrings with comprehensive usage information. This helps developers understand code dependencies and maintain better documentation.

โœจ Features

  • ๐Ÿ” Smart Analysis: Uses Tree-sitter for fast, error-resilient Python parsing
  • ๐ŸŽฏ Accurate References: Combines Rope and Jedi for comprehensive usage detection
  • ๐Ÿ“ Safe Modifications: Preserves code formatting using LibCST with lossless editing
  • ๐Ÿ›ก๏ธ Error Recovery: Graceful handling of syntax errors and edge cases

๐Ÿš€ Installation

# Install dependencies with uv (recommended)
uv venv && source .venv/bin/activate
uv pip install -e .

# Or with pip
pip install tree-sitter tree-sitter-python rope jedi libcst fire rich loguru pathspec
pip install -e .

๐Ÿ“– Quick Start

# Analyze and update docstrings in a project
python -m uzpy -e src/myproject/

# Preview changes without modification  
python -m uzpy -e src/myproject/ --dry-run --verbose

# Analyze a single file
python -m uzpy -e src/myproject/module.py --dry-run

# Get help
python -m uzpy --help

๐Ÿ’ก Usage Examples

Basic Analysis

# Analyze and update docstrings in a project directory
python -m uzpy -e src/myproject/

# Analyze a single file
python -m uzpy -e src/myproject/utils.py

Preview Mode

# See what would change without modifying files
python -m uzpy -e src/myproject/ --dry-run --verbose

# Get detailed analysis information
python -m uzpy -e src/myproject/ --dry-run --verbose

Real-World Examples

# Analyze your entire src directory
python -m uzpy -e src/ --verbose

# Check a specific module before refactoring
python -m uzpy -e src/core/database.py --dry-run

# Update documentation for API modules
python -m uzpy -e src/api/ --verbose

๐Ÿ”ง How It Works

uzpy uses a sophisticated four-phase pipeline:

  1. ๐Ÿ” Discovery Phase: Finds all Python files while respecting gitignore patterns
  2. ๐Ÿ“Š Parsing Phase: Uses Tree-sitter to extract functions, classes, and methods with their docstrings
  3. ๐Ÿ”— Analysis Phase: Employs a hybrid approach combining Rope and Jedi to find usage patterns
  4. ๐Ÿ“ Modification Phase: Uses LibCST to safely update docstrings while preserving formatting

Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   File Discovery โ”‚โ”€โ”€โ”€โ–ถโ”‚  Tree-sitter     โ”‚โ”€โ”€โ”€โ–ถโ”‚  Hybrid Analyzerโ”‚โ”€โ”€โ”€โ–ถโ”‚  LibCST Modifierโ”‚
โ”‚   (gitignore +   โ”‚    โ”‚  Parser          โ”‚    โ”‚  (Rope + Jedi)  โ”‚    โ”‚  (docstring     โ”‚
โ”‚   pathspec)      โ”‚    โ”‚  (AST + constructs)โ”‚   โ”‚  (usage finding)โ”‚    โ”‚   updates)      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

What Gets Updated

uzpy automatically adds "Used in:" sections to docstrings:

Before:

def calculate_total(items):
    """Calculate the total price of items."""
    return sum(item.price for item in items)

After:

def calculate_total(items):
    """Calculate the total price of items.

    Used in:
    - src/main.py
    - src/billing/invoice.py
    - tests/test_calculations.py"""
    return sum(item.price for item in items)

Example Output

When you run uzpy, you'll see beautiful terminal output like this:

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ uzpy Configuration     โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ Edit Path              โ”‚ src/myproject/     โ”‚
โ”‚ Reference Path         โ”‚ .                  โ”‚
โ”‚ Dry Run                โ”‚ No                 โ”‚
โ”‚ Verbose                โ”‚ Yes                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ File Discovery Summary         โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ Edit Files      โ”‚ 23  โ”‚ utils.py, models.py, ... โ”‚
โ”‚ Reference Files โ”‚ 156 โ”‚ main.py, tests.py, ...   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ Construct Parsing Summary        โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ Module   โ”‚ 23   โ”‚ 12/23 โ”‚
โ”‚ Class    โ”‚ 45   โ”‚ 31/45 โ”‚
โ”‚ Function โ”‚ 128  โ”‚ 89/128โ”‚
โ”‚ Method   โ”‚ 267  โ”‚ 156/267โ”‚
โ”‚ Total    โ”‚ 463  โ”‚ 288/463โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ”ง CLI Reference

Main Command

python -m uzpy [OPTIONS]

Options

Option Short Description Default
--edit -e Path to analyze and modify Required
--ref -r Reference path to search for usage Same as edit
--verbose -v Enable detailed logging False
--dry-run Show changes without modifying files False
--methods-include Include method definitions True
--classes-include Include class definitions True
--functions-include Include function definitions True
--exclude-patterns Comma-separated glob patterns to exclude None

๐Ÿ› ๏ธ Development

uzpy is built with modern Python practices and comprehensive testing.

Setup Development Environment

# Clone the repository  
git clone https://github.com/yourusername/uzpy.git
cd uzpy

# Setup with uv (recommended)
uv venv && source .venv/bin/activate
uv pip install -e .

# Or with pip
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install -e .

Development Workflow

# Run tests
python -m pytest

# Lint and format code
ruff check --fix
ruff format

# Full development pipeline (from CLAUDE.md)
fd -e py -x autoflake {}; fd -e py -x pyupgrade --py311-plus {}; fd -e py -x ruff check --output-format=github --fix --unsafe-fixes {}; fd -e py -x ruff format --respect-gitignore --target-version py311 {}; python -m pytest;

Architecture Overview

uzpy is designed with modularity and extensibility in mind:

  • src/uzpy/cli.py - Command-line interface using Fire and Rich
  • src/uzpy/discovery.py - File discovery with gitignore support
  • src/uzpy/parser/ - Tree-sitter based Python parsing
  • src/uzpy/analyzer/ - Hybrid reference analysis (Rope + Jedi)
  • src/uzpy/modifier/ - LibCST-based safe code modification

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Tree-sitter for fast, error-resilient parsing
  • Rope for accurate cross-file analysis
  • Jedi for fast symbol resolution
  • LibCST for safe code modification
  • Rich for beautiful terminal output

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uzpy-1.2.2.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uzpy-1.2.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file uzpy-1.2.2.tar.gz.

File metadata

  • Download URL: uzpy-1.2.2.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for uzpy-1.2.2.tar.gz
Algorithm Hash digest
SHA256 302b70cd6509db31de7a9869b8ce899488aa035addbca3019d4a630530992eeb
MD5 bee9b3a93dacf0b3230eb86b0f304c7a
BLAKE2b-256 6e4160175dbe3281d85a3111e1d97e5bc726f1318054dba043002e78ffc1b0fc

See more details on using hashes here.

File details

Details for the file uzpy-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: uzpy-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for uzpy-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 44d8531d7c912740c7fe2d80027f5187207d674cfd6b2d9750a365587684704f
MD5 7cfaf9908842059a12d076076e1a39a9
BLAKE2b-256 a8c3de60707ead72dda5c8efb1bf8206304ae9b853a76203c89e833dd441ac8c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page