Skip to main content

autoflake8

Build

Introduction

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

By default, autoflake8 only removes unused imports for modules that are part of the standard library. (Other modules may have side effects that make them unsafe to remove automatically.) Removal of unused variables is also disabled by default.

autoflake8 also removes useless pass statements.

It's a maintained fork of autoflake.

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

Advanced usage

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

Below is the full listing of options::

% poetry run autoflake8 --help
usage: autoflake [-h] [-c] [-r] [--exclude globs] [--expand-star-imports] [--remove-duplicate-keys] [--remove-unused-variables] [--version] [-v] [-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)
  -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.)::

$ poetry run scripts/test_fuzz.py

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.

Metadata

Release files for autoflake8 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for autoflake8 0.1.0
File Size Uploaded Provenance Actions
autoflake8-0.1.0.tar.gz 14.9 kB Without provenance Details for autoflake8-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for autoflake8 0.1.0
File Interpreter ABI Platform Actions
autoflake8-0.1.0-py3-none-any.whl Python 3 none any Details for autoflake8-0.1.0-py3-none-any.whl

Total release size: 32.5 kB

Release files / autoflake8-0.1.0.tar.gz

Download URL autoflake8-0.1.0.tar.gz
Size 14.9 kB
Tags Source
SHA-256 checksum
How to use checksums
20373468135708d3e6a4f81436138d9624153f7ce4aa76675230e19f6e8e93a0
BLAKE2b-256 checksum
How to use checksums
1a00a01be3adc4f029fc442b77281d68056a5f0f327382cbdcdc1d5c89f2c90e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.8 CPython/3.9.6 Linux/5.8.0-1040-azure

Release files / autoflake8-0.1.0-py3-none-any.whl

Download URL autoflake8-0.1.0-py3-none-any.whl
Size 17.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
465b96e42bff042c4f52c5d52332472fd58b68e67eb69684d8e2bb3098e1402d
BLAKE2b-256 checksum
How to use checksums
5e884e0f6ca6696adc4c06544ecaeaf0b44f21b327846d0ddb69495eb23d2aa9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.8 CPython/3.9.6 Linux/5.8.0-1040-azure

Security for autoflake8 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page