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
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 07, 2022
A simple Python bytecode framework in pure Python

A simple Python bytecode framework in pure Python

3 Jan 23, 2022
Removes commented-out code from Python files

eradicate eradicate removes commented-out code from Python files. Introduction With modern revision control available, there is no reason to save comm

Steven Myint 146 Dec 13, 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
code of paper „Unknown Object Segmentation from Stereo Images“, IROS 2021

Unknown Object Segmentation from Stereo Images Unknown Object Segmentation from Stereo Images Maximilian Durner*, Wout Boerdijk*, Martin Sundermeyer,

DLR-RM 36 Dec 19, 2022
The python source code sorter

Sorts the contents of python modules so that statements are placed after the things they depend on, but leaves grouping to the programmer. Groups class members by type and enforces topological sortin

Ben Mather 302 Dec 29, 2022
A tool (and pre-commit hook) to automatically add trailing commas to calls and literals.

add-trailing-comma A tool (and pre-commit hook) to automatically add trailing commas to calls and literals. Installation pip install add-trailing-comm

Anthony Sottile 264 Dec 20, 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
Auto-generate PEP-484 annotations

PyAnnotate: Auto-generate PEP-484 annotations Insert annotations into your source code based on call arguments and return types observed at runtime. F

Dropbox 1.4k Dec 26, 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
Re-apply type annotations from .pyi stubs to your codebase.

retype Re-apply type annotations from .pyi stubs to your codebase. Usage Usage: retype [OPTIONS] [SRC]... Re-apply type annotations from .pyi stubs

Łukasz Langa 131 Nov 17, 2022
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 03, 2023
Codes of CVPR2022 paper: Fixing Malfunctional Objects With Learned Physical Simulation and Functional Prediction

Fixing Malfunctional Objects With Learned Physical Simulation and Functional Prediction Figure 1. Teaser. Introduction This paper studies the problem

Yining Hong 32 Dec 29, 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 fi

Batuhan Taskaya 385 Jan 06, 2023
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

106 Dec 17, 2022
a python refactoring library

rope, a python refactoring library ... Overview Rope is a python refactoring library. Notes Nick Smith 1.5k Dec 30, 2022

Removes unused imports and unused variables as reported by pyflakes

autoflake Introduction autoflake removes unused imports and unused variables from Python code. It makes use of pyflakes to do this. By default, autofl

Steven Myint 678 Jan 04, 2023
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

51 Dec 08, 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
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