Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

dhruvmanila/remove-print-statements

Repository files navigation

Warning

This project is no longer maintained. Please use Ruff instead with the following configuration:

[tool.ruff]
extend-select = ["T201"]

Rule reference: https://docs.astral.sh/ruff/rules/print

remove-print-statements

test codecov pre-commit.ci status PyPi Status Python versions MIT License

A command-line tool (and pre-commit hook) to remove all the print statements from your Python project.

Do you use print statements for debugging? We all do, and there's nothing wrong with it. After the bug has been resolved, we need to manually open all the files which we added the print statements in, only if we remember all of them after hours of debugging, and remove them. A better way would be to use some sort of find and replace from the editor or command-line, but that's still a lot of manual work. Worst case, it gets pushed and deployed to production.

Who wants to do all the manual work in the age of automation? No one. So, install this tool and forget about removing the print statements manually forever. You could either run this tool manually or add it as a pre-commit hook. You could even preview the print statements along with it's location without removing it. How nice is that!

Installation

You can install remove-print-statements from the Python Package Index (PyPI) with pipx or equivalent.

pipx install remove-print-statements

Or, try it out using pipx:

pipx run remove-print-statements --help

Or, with pre-commit in the repos section of your .pre-commit-config.yaml file (docs):

- repo: https://github.com/dhruvmanila/remove-print-statements
  rev: ''  # Replace with latest tag on GitHub
  hooks:
  - id: remove-print-statements
    # args: ['--verbose']   # Show all the print statements to be removed

Usage

Run it on a given set of files:

remove-print-statements foo.py bar.py ...
# or use globbing
remove-print-statements *.py

You can ignore files as well. To specify multiple files to ignore, use the flag multiple times otherwise it's difficult to know the difference between the files to ignore and the ones to check.

remove-print-statements *.py --ignore foo.py --ignore bar.py

You can preview the print statements which would be removed without modifying the source files using both --dry-run and --verbose flags like so:

$ remove-print-statements --dry-run --verbose test.py
test.py
  7 print("module")
  18 print("property")
  27 print("method")
  29 print("for loop")

1 file would be transformed, 4 print statements would be removed

remove-print-statements is a command-line tool that rewrites the files in place. It focuses on upgrading your code and not on making it look nice. Run remove-print-statements before formatters like Black.

remove-print-statements does not have any ability to recurse through directories. Use the pre-commit integration, globbing, or another technique for applying to many files such as with git ls-files | xargs.

Single statement

If there's only a single statement in the body of a function, for/while loop, if statement, etc., which is a print statement, then it will be replaced with the pass keyword.

def foo():
-    print()
+    pass


for _ in range(5):
-    print()
+    pass

if __name__ == "__main__":
-    print()
+    pass

Exit status

remove-print-statements command returns exit statuses as follows:

Status Description
0 No print statements / changes made successfully
1 Files would be updated (dry run)
123 Some error happened

Development

packaging: poetry code style: black pre-commit: enabled

Release

  1. Run poetry lock
  2. Bump version in pyproject.toml and __version__ variable
  3. Commit and push the changes with message release: <version>
  4. Publish a new release on GitHub which will trigger an automated workflow to publish on PyPi

License

remove-print-statements is licensed under the MIT License.

See LICENSE for details.

About

A command-line tool (and pre-commit hook) to remove print statements from your Python project.

Topics

Resources

License

Stars

Watchers

Forks

Languages