| 1 | # autoflake |
| 2 | |
| 3 | [](https://github.com/PyCQA/autoflake/actions/workflows/main.yaml) |
| 4 | |
| 5 | ## Introduction |
| 6 | |
| 7 | _autoflake_ removes unused imports and unused variables from Python code. It |
| 8 | makes use of [pyflakes](https://pypi.org/pypi/pyflakes) to do this. |
| 9 | |
| 10 | By default, autoflake only removes unused imports for modules that are part of |
| 11 | the standard library. (Other modules may have side effects that make them |
| 12 | unsafe to remove automatically.) Removal of unused variables is also disabled |
| 13 | by default. |
| 14 | |
| 15 | autoflake also removes useless ``pass`` statements by default. |
| 16 | |
| 17 | ## Example |
| 18 | |
| 19 | Running autoflake on the below example |
| 20 | |
| 21 | ``` |
| 22 | $ autoflake --in-place --remove-unused-variables example.py |
| 23 | ``` |
| 24 | |
| 25 | ```python |
| 26 | import math |
| 27 | import re |
| 28 | import os |
| 29 | import random |
| 30 | import multiprocessing |
| 31 | import grp, pwd, platform |
| 32 | import subprocess, sys |
| 33 | |
| 34 | |
| 35 | def foo(): |
| 36 | from abc import ABCMeta, WeakSet |
| 37 | try: |
| 38 | import multiprocessing |
| 39 | print(multiprocessing.cpu_count()) |
| 40 | except ImportError as exception: |
| 41 | print(sys.version) |
| 42 | return math.pi |
| 43 | ``` |
| 44 | |
| 45 | results in |
| 46 | |
| 47 | ```python |
| 48 | import math |
| 49 | import sys |
| 50 | |
| 51 | |
| 52 | def foo(): |
| 53 | try: |
| 54 | import multiprocessing |
| 55 | print(multiprocessing.cpu_count()) |
| 56 | except ImportError: |
| 57 | print(sys.version) |
| 58 | return math.pi |
| 59 | ``` |
| 60 | |
| 61 | |
| 62 | ## Installation |
| 63 | |
| 64 | ``` |
| 65 | $ pip install --upgrade autoflake |
| 66 | ``` |
| 67 | |
| 68 | |
| 69 | ## Advanced usage |
| 70 | |
| 71 | To allow autoflake to remove additional unused imports (other than |
| 72 | than those from the standard library), use the ``--imports`` option. It |
| 73 | accepts a comma-separated list of names: |
| 74 | |
| 75 | ``` |
| 76 | $ autoflake --imports=django,requests,urllib3 <filename> |
| 77 | ``` |
| 78 | |
| 79 | To remove all unused imports (whether or not they are from the standard |
| 80 | library), use the ``--remove-all-unused-imports`` option. |
| 81 | |
| 82 | To remove unused variables, use the ``--remove-unused-variables`` option. |
| 83 | |
| 84 | Below is the full listing of options: |
| 85 | |
| 86 | ``` |
| 87 | usage: autoflake [-h] [-c | -cd] [-r] [-j n] [--exclude globs] [--imports IMPORTS] [--expand-star-imports] [--remove-all-unused-imports] [--ignore-init-module-imports] [--remove-duplicate-keys] [--remove-unused-variables] |
| 88 | [--remove-rhs-for-unused-variables] [--ignore-pass-statements] [--ignore-pass-after-docstring] [--version] [--quiet] [-v] [--stdin-display-name STDIN_DISPLAY_NAME] [--config CONFIG_FILE] [-i | -s] |
| 89 | files [files ...] |
| 90 | |
| 91 | Removes unused imports and unused variables as reported by pyflakes. |
| 92 | |
| 93 | positional arguments: |
| 94 | files files to format |
| 95 | |
| 96 | options: |
| 97 | -h, --help show this help message and exit |
| 98 | -c, --check return error code if changes are needed |
| 99 | -cd, --check-diff return error code if changes are needed, also display file diffs |
| 100 | -r, --recursive drill down directories recursively |
| 101 | -j n, --jobs n number of parallel jobs; match CPU count if value is 0 (default: 0) |
| 102 | --exclude globs exclude file/directory names that match these comma-separated globs |
| 103 | --imports IMPORTS by default, only unused standard library imports are removed; specify a comma-separated list of additional modules/packages |
| 104 | --expand-star-imports |
| 105 | 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 |
| 106 | --remove-all-unused-imports |
| 107 | remove all unused imports (not just those from the standard library) |
| 108 | --ignore-init-module-imports |
| 109 | exclude __init__.py when removing unused imports |
| 110 | --remove-duplicate-keys |
| 111 | remove all duplicate keys in objects |
| 112 | --remove-unused-variables |
| 113 | remove unused variables |
| 114 | --remove-rhs-for-unused-variables |
| 115 | remove RHS of statements when removing unused variables (unsafe) |
| 116 | --ignore-pass-statements |
| 117 | ignore all pass statements |
| 118 | --ignore-pass-after-docstring |
| 119 | ignore pass statements after a newline ending on '"""' |
| 120 | --version show program's version number and exit |
| 121 | --quiet Suppress output if there are no issues |
| 122 | -v, --verbose print more verbose logs (you can repeat `-v` to make it more verbose) |
| 123 | --stdin-display-name STDIN_DISPLAY_NAME |
| 124 | the name used when processing input from stdin |
| 125 | --config CONFIG_FILE Explicitly set the config file instead of auto determining based on file location |
| 126 | -i, --in-place make changes to files instead of printing diffs |
| 127 | -s, --stdout print changed text to stdout. defaults to true when formatting stdin, or to false otherwise |
| 128 | ``` |
| 129 | |
| 130 | To ignore the file, you can also add a comment to the top of the file: |
| 131 | ```python |
| 132 | # autoflake: skip_file |
| 133 | import os |
| 134 | ``` |
| 135 | |
| 136 | ## Configuration |
| 137 | |
| 138 | Configure default arguments using a `pyproject.toml` file: |
| 139 | |
| 140 | ```toml |
| 141 | [tool.autoflake] |
| 142 | check = true |
| 143 | imports = ["django", "requests", "urllib3"] |
| 144 | ``` |
| 145 | |
| 146 | Or a `setup.cfg` file: |
| 147 | |
| 148 | ```ini |
| 149 | [autoflake] |
| 150 | check=true |
| 151 | imports=django,requests,urllib3 |
| 152 | ``` |
| 153 | |
| 154 | The name of the configuration parameters match the flags (e.g. use the |
| 155 | parameter `expand-star-imports` for the flag `--expand-star-imports`). |
| 156 | |
| 157 | ## Tests |
| 158 | |
| 159 | To run the unit tests:: |
| 160 | |
| 161 | ``` |
| 162 | $ ./test_autoflake.py |
| 163 | ``` |
| 164 | |
| 165 | There is also a fuzz test, which runs against any collection of given Python |
| 166 | files. It tests autoflake against the files and checks how well it does by |
| 167 | running pyflakes on the file before and after. The test fails if the pyflakes |
| 168 | results change for the worse. (This is done in memory. The actual files are |
| 169 | left untouched.):: |
| 170 | |
| 171 | ``` |
| 172 | $ ./test_fuzz.py --verbose |
| 173 | ``` |
| 174 | |
| 175 | ## Excluding specific lines |
| 176 | |
| 177 | It might be the case that you have some imports for their side effects, even |
| 178 | if you are not using them directly in that file. |
| 179 | |
| 180 | That is common, for example, in Flask based applications. In where you import |
| 181 | Python modules (files) that imported a main ``app``, to have them included in |
| 182 | the routes. |
| 183 | |
| 184 | For example: |
| 185 | |
| 186 | ```python |
| 187 | from .endpoints import role, token, user, utils |
| 188 | ``` |
| 189 | |
| 190 | As those imports are not being used directly, if you are using the option |
| 191 | ``--remove-all-unused-imports``, they would be removed. |
| 192 | |
| 193 | To prevent that, without having to exclude the entire file, you can add a |
| 194 | ``# noqa`` comment at the end of the line, like: |
| 195 | |
| 196 | ```python |
| 197 | from .endpoints import role, token, user, utils # noqa |
| 198 | ``` |
| 199 | |
| 200 | That line will instruct ``autoflake`` to let that specific line as is. |
| 201 | |
| 202 | |
| 203 | ## Using [pre-commit](https://pre-commit.com) hooks |
| 204 | |
| 205 | Add the following to your `.pre-commit-config.yaml` |
| 206 | |
| 207 | ```yaml |
| 208 | - repo: https://github.com/PyCQA/autoflake |
| 209 | rev: v2.4.0 |
| 210 | hooks: |
| 211 | - id: autoflake |
| 212 | ``` |
| 213 | |
| 214 | When customizing the arguments, make sure you include `--in-place` in the list |
| 215 | of arguments: |
| 216 | |
| 217 | ```yaml |
| 218 | - repo: https://github.com/PyCQA/autoflake |
| 219 | rev: v2.4.0 |
| 220 | hooks: |
| 221 | - id: autoflake |
| 222 | args: [--remove-all-unused-imports, --in-place] |
| 223 | ``` |