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
  • โšก High Performance: Optimized for large codebases with hybrid analysis strategies
  • ๐ŸŽจ Beautiful Output: Rich terminal interface with progress indicators and summaries
  • ๐Ÿ›ก๏ธ Error Recovery: Graceful handling of syntax errors and edge cases
  • ๐Ÿ—๏ธ Production Ready: Complete implementation with comprehensive error handling

๐Ÿš€ 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.1.3.tar.gz (11.6 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.1.3-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for uzpy-1.1.3.tar.gz
Algorithm Hash digest
SHA256 b47334d42dabfd1b5d2008ab455d929b1264d0a6909758b475adb514d1ca76bc
MD5 b892bf8fe6302ae02d4647600f20114d
BLAKE2b-256 d5f993f9a36adb730c19325fcc513419cb843747e790fb07b5b81a2bce5f3ba6

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for uzpy-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 172d65481c55f4ee2e55491f1f58ffa7180894ee43dfd3e66bc281b7e0a85a06
MD5 ab2b8ca0268fc5b533d59edde4e35777
BLAKE2b-256 ec908804a2f81dbd4f3f079b52a7d3886cc62016ccfa44dcdda443fa6db10403

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