Removes commented-out code from Python files

Overview

eradicate

Build status

eradicate removes commented-out code from Python files.

Introduction

With modern revision control available, there is no reason to save commented-out code to your repository. eradicate helps cleans up existing junk comments. It does this by detecting block comments that contain valid Python syntax that are likely to be commented out code. (It avoids false positives like the sentence this is not good, which is valid Python syntax, but is probably not code.)

Example

$ eradicate --in-place example.py

Before running eradicate.

#import os
# from foo import junk
#a = 3
a = 4
#foo(1, 2, 3)

def foo(x, y, z):
    # print('hello')
    print(x, y, z)

    # This is a real comment.
    #return True
    return False

After running eradicate.

a = 4

def foo(x, y, z):
    print(x, y, z)

    # This is a real comment.
    return False

Whitelisting

False positives can happen so there is a whitelist feature to fix them shorthand. You can either add entries to the default whitelist with --whitelist-extend or overwrite the default with --whitelist. Both arguments expect a string of # separated regex strings (whitespaces are preserved). E.g. eradicate --whitelist "foo#b a r" filename Those regex strings are matched case insensitive against the start of the comment itself.

For the default whitelist please see eradicate.py.

Related

There are different tools, plugins, and integrations for eradicate users:

Comments
  • added whitelisting

    added whitelisting

    Multiple issues with false positives motivated me to add a whitelisting feature.

    You can extend the default whitelist or overwrite it from command line. Whitelist entries are interpreted as regex.

    WHITELIST = [
        r'pylint',
        r'pyright',
        r'(?i)noqa',
        r'type:\s*ignore',
        r'fmt:\s*(on|off)',
        r'TODO',
        r'FIXME',
        r'XXX'
    ]
    

    Currently the regex check is case sensitive. A single entry can be made case insensitive by adding (?i) like above for noqa.

    Maybe case insensitivity by default is okay? What other defaults do you know that should be added?

    I'm currently working on the tests and will also update the README afterwards.

    Closes #19 PR for manually ignoring TODO which is covered by this PR

    Fixes #25 Fixes #24 Fixes #16 Fixes #15 Fixes #11 Resolves #26

    EDIT: Please see comment(s) below

    opened by Cielquan 17
  • eradicate and black

    eradicate and black

    Repost of https://github.com/sobolevn/flake8-eradicate/issues/8.

    Hi,

    Thanks for the tool. Unfortunately, eradicate recognizes # fmt: on and # fmt: off as commented out code whereas the comments identify blocks which should not be formatted with black. Is it possible to exclude these patterns?

    Best

    help wanted 
    opened by tobiasraabe 9
  • Change travis to gha

    Change travis to gha

    Like requested in #33 (comment) here is a translation of the travis config in GHA.

    I updated the tested python versions with additional ones and kept the rest as it was.

    EDIT: I do not intend to rewrite the config to a proper GHA config. Reason see: https://github.com/myint/eradicate/pull/33#issuecomment-1086034653

    opened by Cielquan 6
  • Following PEP 263 to be more precisely

    Following PEP 263 to be more precisely

    This PR aims to follow PEP 263 more precisely, especially for source code encoding hints. According to PEP 263, encoding hints allow the following regular expression:

    ^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)
    

    The regexp match # encoding: utf8 which I often use but eradicate 0.2.1 considered that the line contains code.

    $ eradicate tmp.py
     #!/usr/bin/python
    -# encoding: utf8
    
    ...
    
    opened by thombashi 6
  • Misses methods declaration with annotated return types

    Misses methods declaration with annotated return types

    Hi, thanks for this awesome plugin. I am using it in production, it works great. However, I have found out that it misses this case:

    -# class CommentedClass(object):
     #     def __init__(self, prop: int) -> None:
    -#         self.property = prop
     
     #     def __str__(self) -> str:
    -#         return self.__class__.__name__
     
    -#    def set_prop(self, prop: int):
    -#        self.prop = prop
     
    -#    def get_prop(self):
    -#        return self.prop
    

    Look, all method declarations with annotated return types are ignored in this diff. The same goes for the regular functions, not just methods. This may lead to some false negative behavior.

    P.S. I have made a flake8 plugin to check my codebase with eradicate, check it out: https://github.com/sobolevn/flake8-eradicate

    enhancement 
    opened by sobolevn 3
  • False Positives

    False Positives

    Hi, I've tried eradicate on the coala project and it came up with only false positives. Here's a visualization of the results: http://pastebin.com/qbfg1Ecc (I assure you it's the latest eradicator release from pypi working behind the scenes, just think this is better to read and contains diffs with context so you can see whats going on.)

    Especially if there's code between noncode lines I think it makes sense to exclude them, also there's a false negative in cindex.py which would be nice to have handled, I think something starting with a bracket may be easily declared as positive?

    enhancement 
    opened by sils 3
  • Upload wheels to PyPI

    Upload wheels to PyPI

    Wheels (.whl) for this package are currently missing from PyPI. Could wheels be uploaded for the current and future releases?

    Read more about the advantages of wheels to understand why generating wheel distributions are important.

    To create a wheel along with source distribution:

    (venv) $ pip install --upgrade pip setuptools wheel
    (venv) $ python setup.py sdist bdist_wheel
    
    # See dist/*.whl
    

    To upload wheels:

    (venv) $ pip install twine
    (venv) $ twine upload dist/*
    
    opened by johnthagen 2
  • Type ignore comments is a false positive.

    Type ignore comments is a false positive.

    Write # type: ignore at the beginning of the file will be considered by this plugin as dead code.

    Initially reported to https://github.com/sobolevn/flake8-eradicate/issues/68

    enhancement help wanted 
    opened by proofit404 2
  • Various non-code comments removed

    Various non-code comments removed

    I just tried out eradicate, and it removed those comments which aren't sourcecode:

    • # See # https://github.com/The-Compiler/qutebrowser/issues/263
    • # Qt >= 5.4 (okay, this one is probably hard to detect)
    • # anything (yet)
    • # configparser can't handle = in keys :(
    bug 
    opened by The-Compiler 2
  • setup.py: use setuptools instead of distutils

    setup.py: use setuptools instead of distutils

    as distutils will be deprecated in python 3.12 and already produces a warning when run with python 3.10.x, it might be a good time to switch to setuptools. It offers the same interface, so results will be the same

    Signed-off-by: Konrad Weihmann [email protected]

    opened by priv-kweihmann 1
  • Make eradicate compatible with pre-commit

    Make eradicate compatible with pre-commit

    Integrates pre-commit with eradicate. This makes it easy to configure and add eradicate as a pre-commit hook in your git repo.

    For example, the nbstripout repo just merged a similar hook for that tool (for example in PR 79).

    opened by rdturnermtl 1
  • Use of mutation testing in eradicate - Help needed

    Use of mutation testing in eradicate - Help needed

    Hello there!

    My name is Ana. I noted that you use the mutation testing tool mutpy in the project. I am a postdoctoral researcher at the University of Seville (Spain), and my colleagues and I are studying how mutation testing tools are used in practice. With this aim in mind, we have analysed over 3,500 public GitHub repositories using mutation testing tools, including yours! This work has recently been published in a journal paper available at https://link.springer.com/content/pdf/10.1007/s10664-022-10177-8.pdf.

    To complete this study, we are asking for your help to understand better how mutation testing is used in practice, please! We would be extremely grateful if you could contribute to this study by answering a brief survey of 21 simple questions (no more than 6 minutes). This is the link to the questionnaire https://forms.gle/FvXNrimWAsJYC1zB9.

    Drop me an e-mail if you have any questions or comments ([email protected]). Thank you very much in advance!!

    opened by belene 0
  • Commented out code not detected when code block is two lines long

    Commented out code not detected when code block is two lines long

    Copied from https://github.com/wemake-services/flake8-eradicate/issues/265:

    If you paste the following code into a new file, flake8-eradicate will only detect the first and the third code blocks. The second block, split over two lines is not detected.

    
    # create_output_path(*os.path.split(mypath),
    #                    overwrite=overwrite,
    #                    exist_ok=True)
    
    # create_output_path(*os.path.split(mypath), overwrite=overwrite,
    #                    exist_ok=True)
    
    # create_output_path(*os.path.split(mypath), overwrite=overwrite, exist_ok=True)
    
    

    Repro steps

    $ python3.8 -m venv ~/deleteme
    $ source ~/deleteme/bin/activate
    $ pip install flake8-eradicate
    $ flake8 test.py --max-line-length=81
    

    Expected result

    Commented out code found on lines 2-4, 6-7, and 9.

    Actual result

    test.py:3:1: E800 Found commented out code
    test.py:9:1: E800 Found commented out code
    
    opened by jontwo 0
  • Configuration file

    Configuration file

    Hello,

    if a user doesn't want to use flake8, the only option to provide configuration is to use the command line. Would it be possible to add support for a configuration file (e.g. pyproject.toml)?

    Regards, Kai

    opened by kasium 0
  • Allow doctest in the comments.

    Allow doctest in the comments.

    Some times it's useful to explain implementation details of the function with usage examples in the comment.

    I propose to allow doctest prefixed code in the comments.

    What do you think?

    Have a good day :tada:

    Best regards, Artem.

    enhancement 
    opened by proofit404 1
  • Location of commented code is incorrect

    Location of commented code is incorrect

    Eradicate seems to highlight the first character of the first line as the offending source location. This makes eradicate less useful when used within an editor as it's often not obvious that there are errors.

    It would be great if Eradicate supported giving a correct, or roughly correct, location of the offending source location.

    enhancement help wanted 
    opened by danpalmer 0
Releases(v2.1.0)
Owner
Steven Myint
Mars rover flight software developer
Steven Myint
Programmatically edit text files with Python. Useful for source to source transformations.

massedit formerly known as Python Mass Editor Implements a python mass editor to process text files using Python code. The modification(s) is (are) sh

null 106 Dec 17, 2022
Code generation and code search for Python and Javascript.

Codeon Code generation and code search for Python and Javascript. Similar to GitHub Copilot with one major difference: Code search is leveraged to mak

null 51 Dec 8, 2022
IDE allow you to refactor code, Baron allows you to write refactoring code.

Introduction Baron is a Full Syntax Tree (FST) library for Python. By opposition to an AST which drops some syntax information in the process of its c

Python Code Quality Authority 278 Dec 29, 2022
Find dead Python code

Vulture - Find dead code Vulture finds unused code in Python programs. This is useful for cleaning up and finding errors in large code bases. If you r

Jendrik Seipp 2.4k Dec 27, 2022
Safe code refactoring for modern Python.

Safe code refactoring for modern Python projects. Overview Bowler is a refactoring tool for manipulating Python at the syntax tree level. It enables s

Facebook Incubator 1.4k Jan 4, 2023
A library that modifies python source code to conform to pep8.

Pep8ify: Clean your code with ease Pep8ify is a library that modifies python source code to conform to pep8. Installation This library currently works

Steve Pulec 117 Jan 3, 2023
Turn your C++/Java code into a Python-like format for extra style points and to make everyone hates you

Turn your C++/Java code into a Python-like format for extra style points and to make everyone hates you

Tô Đức (Watson) 4 Feb 7, 2022
Simple, hassle-free, dependency-free, AST based source code refactoring toolkit.

refactor is an end-to-end refactoring framework that is built on top of the 'simple but effective refactorings' assumption. It is much easier to write a simple script with it rather than trying to figure out what sort of a regex you need in order to replace a pattern (if it is even matchable with regexes).

Batuhan Taskaya 385 Jan 6, 2023
A simple Python bytecode framework in pure Python

A simple Python bytecode framework in pure Python

null 3 Jan 23, 2022
Awesome autocompletion, static analysis and refactoring library for python

Jedi - an awesome autocompletion, static analysis and refactoring library for Python Jedi is a static analysis tool for Python that is typically used

Dave Halter 5.3k Dec 29, 2022
a python refactoring library

rope, a python refactoring library ... Overview Rope is a python refactoring library. Notes Nick Smith <[email protected]> takes over maintaining rope

null 1.5k Dec 30, 2022
A system for Python that generates static type annotations by collecting runtime types

MonkeyType MonkeyType collects runtime types of function arguments and return values, and can automatically generate stub files or even add draft type

Instagram 4.1k Dec 28, 2022
Tool for translation type comments to type annotations in Python

com2ann Tool for translation of type comments to type annotations in Python. The tool requires Python 3.8 to run. But the supported target code versio

Ivan Levkivskyi 123 Nov 12, 2022
Bottom-up approach to refactoring in python

Introduction RedBaron is a python library and tool powerful enough to be used into IPython solely that intent to make the process of writing code that

Python Code Quality Authority 653 Dec 30, 2022
AST based refactoring tool for Python.

breakfast AST based refactoring tool. (Very early days, not usable yet.) Why 'breakfast'? I don't know about the most important, but it's a good meal.

eric casteleijn 0 Feb 22, 2022
Refactoring Python Applications for Simplicity

Python Refactoring Refactoring Python Applications for Simplicity. You can open and read project files or use this summary ?? Concatenate String my_st

Mohammad Dori 3 Jul 15, 2022
Leap is an experimental package written to enable the utilization of C-like goto statements in Python functions

Leap is an experimental package written to enable the utilization of C-like goto statements in Python functions

null 6 Dec 26, 2022
Flake8 plugin to find commented out or dead code

flake8-eradicate flake8 plugin to find commented out (or so called "dead") code. This is quite important for the project in a long run. Based on eradi

wemake.services 277 Dec 27, 2022
Automatically pick a winner who Retweeted, Commented, and Followed your Twitter account!

AutomaticTwitterGiveaways automates selecting winners for "Retweet, Comment, Follow" type Twitter giveaways.

null 1 Jan 13, 2022
Removes all archived super productivity tasks. Just run the python script.

delete-archived-sp-tasks.py Removes all archived super productivity tasks. Just run the python script. This is helpful to do a cleanup every 3-6 month

Ben Herbst 1 Jan 9, 2022