eradicate: Remove Junk Comments from Python Files

Outdated or unused code left as comments in Python files can clutter codebases, making them harder to read and maintain.

Eradicate solves this by automatically identifying and removing commented-out code from Python files.

Let’s see eradicate in action:

Example Python file:

# from math import *

def mean(nums: list):
    # print(nums)
    # TODO: check if nums is empty
    # Return mean
    return sum(nums) / len(nums)

# nums = [0, 1]
nums = [1, 2, 3]
mean(nums)

Preview changes:

$ eradicate main.py
--- before/main.py
+++ after/main.py
@@ -1,11 +1,8 @@
-# from math import *

 def mean(nums: list):
-    # print(nums)
     # TODO: check if nums is empty
     # Return mean
     return sum(nums) / len(nums)

-# nums = [0, 1]
 nums = [1, 2, 3]
 mean(nums)

Apply changes:

$ eradicate main.py -i

Results:

def mean(nums: list):
    # TODO: check if nums is empty
    # Return mean
    return sum(nums) / len(nums)

nums = [1, 2, 3]
mean(nums)

In this example, eradicate removes:

  1. The commented-out import statement # from math import *
  2. The commented-out debug print statement # print(nums)
  3. The commented-out variable assignment # nums = [0, 1]

However, it preserves the meaningful comments:

  1. The TODO comment # TODO: check if nums is empty
  2. The descriptive comment # Return mean

This cleanup improves the code’s readability by removing distracting, unused code comments while keeping important notes for developers.

You can use eradicate with pre-commit by adding the following to your .pre-commit-config.yaml file:

repos:
- repo: https://github.com/wemake-services/eradicate/
  rev: v2.2.0
  hooks:
  - id: eradicate

Link to eradicate.

Related Posts

Related Posts

Scroll to Top

Work with Khuyen Tran

Work with Khuyen Tran