Skip to main content

A pre-commit hook to detect and prevent print statements in Python code

Project description

Print Statements Hook

A Python-based pre-commit hook that uses static analysis to detect and prevent print() statements from being committed to your codebase.

Using print() for debugging is common, but leaving them in production code can lead to cluttered logs and potential information leaks. This hook helps you maintain clean code by catching them before they are committed.

Features

  • Static Analysis: Uses Python's built-in ast module to scan code without executing it.
  • Fast: Scans files quickly, even in large projects.
  • Pre-commit Integration: Easy to add to your existing .pre-commit-config.yaml.
  • Informative: Points out the exact file, line, and column where the print statement was found.

Installation

You can install the package from PyPI:

pip install print-statements-hook

Or install it locally for development or testing:

pip install .

Usage

As a Pre-commit Hook

Add the following to your .pre-commit-config.yaml file:

repos:
  - repo: https://github.com/kuldeepmodh/print-statements-hook
    rev: v0.1.0  # Use the latest tag or commit hash
    hooks:
      - id: check-print-statements

Then run:

pre-commit install

Manual Usage

You can also run the script manually on specific files or directories:

# Check specific files
check-print-statements file1.py file2.py

# Check current directory recursively
check-print-statements

# Exclude specific files or directories
check-print-statements --exclude tests --exclude some_legacy_file.py

Development

Running Tests

This project uses pytest for testing.

pip install pytest
PYTHONPATH=. pytest

How It Works

The hook parses your Python files into an Abstract Syntax Tree (AST). It then walks through the tree looking for Call nodes where the function being called is named print. If any are found, it reports the location and exits with a non-zero status code, preventing the git commit.

Versioning

This project follows Semantic Versioning. You can check the installed version using:

check-print-statements --version

Releasing New Versions

Versioning is automated using bump-my-version and GitHub Actions. To release a new version:

  1. Use bump-my-version to bump the version (e.g., from 0.1.0 to 0.2.0):

    # This will update all files, create a commit, and add a Git tag
    bump-my-version bump minor  # Use 'patch', 'minor', or 'major'
    
  2. Push the commit and the tag to GitHub:

    git push origin main
    git push origin --tags
    
  3. The GitHub Actions release workflow will automatically build the package, publish it to PyPI, and create a GitHub Release.

Users can then reference the new specific version tag in their .pre-commit-config.yaml or install the new version via pip install --upgrade print-statements-hook.

License

MIT License. See LICENSE for details.

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

print_statements_hook-0.1.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

print_statements_hook-0.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

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