Skip to main content

Python monkeypatch detection and analysis tool

Project description

koz

Python monkeypatch detection and analysis tool.

Overview

koz is a static analysis tool that detects and catalogs monkeypatch modifications in Python codebases. It uses AST parsing to identify patches and generates structured output for patch management.

Features

  • Detects three types of monkeypatching:
    • @patch_to decorator from fastcore.basic
    • Direct attribute assignment monkeypatching
    • functools.wraps decorator in __init__ methods
  • Generates JSON/TOML output with comprehensive metadata
  • Integrates with git history for author and timestamp information
  • Fast AST-based analysis without code execution

Installation

pip install -e .

Usage

Command Line

# Analyze current directory
koz analyze

# Analyze specific directory
koz analyze /path/to/project

# Use a configuration file to filter files
koz analyze --config koz_config.yaml

# Output to TOML
koz analyze --format toml --output patches.toml

# Output to JSON
koz analyze --format json --output patches.json

# Combine options
koz analyze /path/to/project --config koz_config.yaml --format json

Configuration File

You can use a YAML configuration file to filter which files/folders to analyze. Create a koz_config.yaml file:

# Include patterns: list of regex patterns for files/folders to include
include:
  - "src/.*"           # Include all files in src/ directory
  - "lib/.*"           # Include all files in lib/ directory

# Exclude patterns: list of regex patterns for files/folders to exclude
exclude:
  - ".*test_.*"        # Exclude test files
  - ".*/tests/.*"      # Exclude tests/ directory
  - ".*/__pycache__/.*"  # Exclude __pycache__ directories

Notes:

  • Patterns are evaluated as regular expressions
  • Exclude patterns are evaluated first
  • If include patterns are specified, files must match at least one include pattern
  • If no config file is provided, all files (except default exclusions) are analyzed
  • See examples/koz_config.yaml for a complete example

Detected Patterns

1. fastcore.basic.patch_to Decorator

The most common pattern in fastai/fastcore projects:

from fastcore.basic import patch_to

class MyClass:
    def __init__(self):
        self.value = 0

@patch_to(MyClass)
def new_method(self):
    """This method is added to MyClass via patch_to."""
    return self.value * 2

2. Direct Attribute Assignment

def custom_method(self):
    return "custom"

MyClass.method = custom_method

3. functools.wraps Pattern

from functools import wraps

class Wrapper:
    def __init__(self):
        @wraps(TargetClass.method)
        def wrapped(self):
            return "wrapped"
        TargetClass.method = wrapped

Development

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest tests/ -v

# Run linter
ruff check src/ tests/

# Format code
black src/ tests/

# Type check
mypy src/

# Install pre-commit hooks
pre-commit install

License

Unlicense

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

koz-0.1.1.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

koz-0.1.1-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file koz-0.1.1.tar.gz.

File metadata

  • Download URL: koz-0.1.1.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for koz-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1810fd9f0ce15e128c0822bf5a4e73a7ce347f22e3284bdc6f5273abe10a363d
MD5 5e8689576f9e1579c34d6740c8ecd7d3
BLAKE2b-256 7c6937110705776cde9a36636cb5ddfa0759ec9108e9bdd00e2ef1ce5d9c6aaa

See more details on using hashes here.

File details

Details for the file koz-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: koz-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for koz-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 469251e10d8b054aef08622210ee9cba79f6c06dacd93b5e67c1e570c1d4fc13
MD5 ec99d652304d6a06ef1d43976aa5c9e2
BLAKE2b-256 d1043e8f93c4fc37c318b19b7afe7f458a12c365416d9f4538545869a6bc64ad

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