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
astmodule 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
printstatement 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:
-
Use
bump-my-versionto bump the version (e.g., from0.1.0to0.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'
-
Push the commit and the tag to GitHub:
git push origin main git push origin --tags
-
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file print_statements_hook-0.1.0.tar.gz.
File metadata
- Download URL: print_statements_hook-0.1.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
936f7b83bc1e6574341d012b1820de2711588d6c39f9c321be4cbd62c5fa8ac4
|
|
| MD5 |
dcc2709d363f493380f023f2ea4cff9b
|
|
| BLAKE2b-256 |
46cee6714619b1827024eec8ad457248b1bd0a9adb5f6bcb6042b09f3f5a02a8
|
File details
Details for the file print_statements_hook-0.1.0-py3-none-any.whl.
File metadata
- Download URL: print_statements_hook-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84b4e703f120db8ac599c6a93cfc473533b7a00a79a7e3514bac06a19a5b4b94
|
|
| MD5 |
3e039bd90b7116991749d245def5e023
|
|
| BLAKE2b-256 |
91bf1955e9256a3596882926205e6fca171822a6900fb6cde61f8d089c7b9c6c
|