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

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

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

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.0.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.

koz-0.1.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: koz-0.1.0.tar.gz
  • Upload date:
  • Size: 11.6 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.0.tar.gz
Algorithm Hash digest
SHA256 c10f525c5022dd79d2bae884403ecf1fc5664a5d3d4d7b3d074ecb76ad070bdb
MD5 8c4ac5db9e7b3b2748a61b818a6e9b9e
BLAKE2b-256 55c6ca963644eb2e1d6c96bcad65f37fc18d7012b18cca6223f8568188a1828a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: koz-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c1feb0ed5781402ea7b931ca6c6cbadc102f20bb2467446dfba06f5e3bb4742a
MD5 50e92dbf1779e30a6a4b3a7e172df983
BLAKE2b-256 4e5386b624cf5a74101cf65ebb36f7bb42d4577d0ebf2ebad0c25c1b1c796e85

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