All Projects → fsouza → autoflake8

fsouza / autoflake8

Licence: MIT License
Tool to automatically fix some issues reported by flake8 (forked from autoflake).

Programming Languages

python
139335 projects - #7 most used programming language
shell
77523 projects

Labels

Projects that are alternatives of or similar to autoflake8

flake8-walrus
flake8 plugin which forbids assignment expressions (the walrus operator)
Stars: ✭ 36 (+157.14%)
Mutual labels:  flake8
jupyterlab-flake8
Jupyterlab python linter for notebooks and text files using flake8
Stars: ✭ 105 (+650%)
Mutual labels:  flake8
rubric
Linter Config Initializer for Python
Stars: ✭ 21 (+50%)
Mutual labels:  flake8
Elements Of Python Style
Goes beyond PEP8 to discuss what makes Python code feel great. A Strunk & White for Python.
Stars: ✭ 3,308 (+23528.57%)
Mutual labels:  flake8
flake8-mypy
A plugin for flake8 integrating Mypy.
Stars: ✭ 103 (+635.71%)
Mutual labels:  flake8
flake8-simplify
❄ A flake8 plugin that helps you to simplify code
Stars: ✭ 97 (+592.86%)
Mutual labels:  flake8
flake8-cognitive-complexity
An extension for flake8 that validates cognitive functions complexity
Stars: ✭ 44 (+214.29%)
Mutual labels:  flake8
flake8-alfred
Alfred is a flake8 plugin to warn on unsafe/obsolete symbols.
Stars: ✭ 26 (+85.71%)
Mutual labels:  flake8
flake8-annotations
Flake8 Type Annotation Checking
Stars: ✭ 117 (+735.71%)
Mutual labels:  flake8
flake8-type-checking
Flake8 plugin for managing type-checking imports & forward references.
Stars: ✭ 38 (+171.43%)
Mutual labels:  flake8
Wemake Python Styleguide
The strictest and most opinionated python linter ever!
Stars: ✭ 1,714 (+12142.86%)
Mutual labels:  flake8
dlint
Dlint is a tool for encouraging best coding practices and helping ensure Python code is secure.
Stars: ✭ 130 (+828.57%)
Mutual labels:  flake8
flake8-assertive
Flake8 unittest assert method checker
Stars: ✭ 30 (+114.29%)
Mutual labels:  flake8
flake8-tidy-imports
❄️ A flake8 plugin that helps you write tidier imports.
Stars: ✭ 40 (+185.71%)
Mutual labels:  flake8
flake8-docstrings
Integration of pydocstyle and flake8 for combined linting and reporting
Stars: ✭ 73 (+421.43%)
Mutual labels:  flake8
python-lint
GitHub Action for Lint your code
Stars: ✭ 57 (+307.14%)
Mutual labels:  flake8
google classroom
Google Classroom Data Pipeline
Stars: ✭ 17 (+21.43%)
Mutual labels:  flake8
flake8-broken-line
🚨 Flake8 plugin to forbid backslashes (\) for line breaks
Stars: ✭ 85 (+507.14%)
Mutual labels:  flake8
flake8-sql
Flake8 plugin that checks SQL code against opinionated style rules
Stars: ✭ 25 (+78.57%)
Mutual labels:  flake8
flake8-import-order-spoqa
Spoqa's import order style for flake8-import-order
Stars: ✭ 15 (+7.14%)
Mutual labels:  flake8

autoflake8

Build

Introduction

autoflake8 removes unused imports and unused variables from Python code. It makes use of pyflakes to do this.

autoflake8 also removes useless pass statements.

It's a maintained fork of autoflake.

Differences from autoflake

This fork of autoflake removed some features from autoflake and modified certain behaviors. The main motivations for those changes is the idea that autoflake8 is built for users of flake8 and it's assumed that if you're using autoflake8, you're also using flake8. This motivated the removal of the flags --imports and --remove-all-unused-imports: if you want to preserve an import for its side-effect, use # noqa.

Additionally, autoflake8 also supports load from stdin and printing to stdout, which makes it very easy for users to integrate with their custom editors.

Finally, a big difference is that autoflake8 exits with status 1 when it detects issues/rewrite files. For editor integration, the new flag --exit-zero-even-if-changed can be used. When that flag is defined, autoflake8 will return status 0 even when it modifies files.

In terms of future plans, we also plan to eventually stop using regular expressions and rely on actual AST rewriting to fix issues reported by flake8.

Example

Running autoflake8 on the below example:

$ autoflake8 --in-place --remove-unused-variables example.py
import math
import re
import os
import random
import multiprocessing
import grp, pwd, platform
import subprocess, sys


def foo():
    from abc import ABCMeta, WeakSet
    try:
        import multiprocessing
        print(multiprocessing.cpu_count())
    except ImportError as exception:
        print(sys.version)
    return math.pi

Results in:

import math
import sys


def foo():
    try:
        import multiprocessing
        print(multiprocessing.cpu_count())
    except ImportError:
        print(sys.version)
    return math.pi

Installation

$ pip install --upgrade autoflake8

Using as a pre-commit hook

autoflake8 can be used as a pre-commit hook. See pre-commit for instructions.

Sample .pre-commit-config.yaml:

-   repo: https://github.com/fsouza/autoflake8
    rev: v0.3.2
    hooks:
    -   id: autoflake8

Advanced usage

To remove unused variables, use the --remove-unused-variables option.

Below is the full listing of options:

usage: autoflake8 [-h] [-c] [-r] [--exclude globs] [--expand-star-imports] [--remove-duplicate-keys] [--remove-unused-variables] [--version] [-v] [--exit-zero-even-if-changed] [-i | -s] files [files ...]

positional arguments:
  files                 files to format

optional arguments:
  -h, --help            show this help message and exit
  -c, --check           return error code if changes are needed
  -r, --recursive       drill down directories recursively
  --exclude globs       exclude file/directory names that match these comma-separated globs
  --expand-star-imports
                        expand wildcard star imports with undefined names; this only triggers if there is only one star import in the file; this is skipped if there are any uses of `__all__` or `del` in the file
  --remove-duplicate-keys
                        remove all duplicate keys in objects
  --remove-unused-variables
                        remove unused variables
  --version             show program's version number and exit
  -v, --verbose         print more verbose logs (you can repeat `-v` to make it more verbose)
  --exit-zero-even-if-changed
  -i, --in-place        make changes to files instead of printing diffs
  -s, --stdout          print changed text to stdout. defaults to true when formatting stdin, or to false otherwise

Tests

To run the unit tests:

$ poetry run pytest

There is also a fuzz test, which runs against any collection of given Python files. It tests autoflake8 against the files and checks how well it does by running pyflakes on the file before and after. The test fails if the pyflakes results change for the worse. (This is done in memory. The actual files are left untouched):

$ scripts/fuzz.sh

Excluding specific lines

It might be the case that you have some imports for their side effects, even if you are not using them directly in that file.

That is common, for example, in Flask based applications. In where you import Python modules (files) that imported a main app, to have them included in the routes.

For example:

from .endpoints import role, token, user, utils

To prevent that, without having to exclude the entire file, you can add a # noqa comment at the end of the line, like:

from .endpoints import role, token, user, utils  # noqa

That line will instruct autoflake8 to let that specific line as is.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].