| 1 |
======== |
| 2 |
autopep8 |
| 3 |
======== |
| 4 |
|
| 5 |
.. image:: https://travis-ci.org/hhatto/autopep8.svg?branch=master |
| 6 |
:target: https://travis-ci.org/hhatto/autopep8 |
| 7 |
:alt: Build status |
| 8 |
|
| 9 |
autopep8 automatically formats Python code to conform to the `PEP 8`_ style |
| 10 |
guide. It uses the pep8_ utility to determine what parts of the code needs to |
| 11 |
be formatted. autopep8 is capable of fixing most of the formatting issues_ that |
| 12 |
can be reported by pep8. |
| 13 |
|
| 14 |
.. _PEP 8: http://www.python.org/dev/peps/pep-0008/ |
| 15 |
.. _issues: https://pep8.readthedocs.org/en/latest/intro.html#error-codes |
| 16 |
|
| 17 |
.. contents:: |
| 18 |
|
| 19 |
|
| 20 |
Installation |
| 21 |
============ |
| 22 |
|
| 23 |
From pip:: |
| 24 |
|
| 25 |
$ pip install --upgrade autopep8 |
| 26 |
|
| 27 |
Consider using the ``--user`` option_. |
| 28 |
|
| 29 |
.. _option: https://pip.readthedocs.org/en/latest/user_guide.html#user-installs |
| 30 |
|
| 31 |
|
| 32 |
Requirements |
| 33 |
============ |
| 34 |
|
| 35 |
autopep8 requires pep8_. |
| 36 |
|
| 37 |
.. _pep8: https://github.com/jcrocholl/pep8 |
| 38 |
|
| 39 |
|
| 40 |
Usage |
| 41 |
===== |
| 42 |
|
| 43 |
To modify a file in place (with aggressive level 2):: |
| 44 |
|
| 45 |
$ autopep8 --in-place --aggressive --aggressive <filename> |
| 46 |
|
| 47 |
Before running autopep8. |
| 48 |
|
| 49 |
.. code-block:: python |
| 50 |
|
| 51 |
import math, sys; |
| 52 |
|
| 53 |
def example1(): |
| 54 |
####This is a long comment. This should be wrapped to fit within 72 characters. |
| 55 |
some_tuple=( 1,2, 3,'a' ); |
| 56 |
some_variable={'long':'Long code lines should be wrapped within 79 characters.', |
| 57 |
'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'], |
| 58 |
'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1, |
| 59 |
20,300,40000,500000000,60000000000000000]}} |
| 60 |
return (some_tuple, some_variable) |
| 61 |
def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key('')); |
| 62 |
class Example3( object ): |
| 63 |
def __init__ ( self, bar ): |
| 64 |
#Comments should have a space after the hash. |
| 65 |
if bar : bar+=1; bar=bar* bar ; return bar |
| 66 |
else: |
| 67 |
some_string = """ |
| 68 |
Indentation in multiline strings should not be touched. |
| 69 |
Only actual code should be reindented. |
| 70 |
""" |
| 71 |
return (sys.path, some_string) |
| 72 |
|
| 73 |
After running autopep8. |
| 74 |
|
| 75 |
.. code-block:: python |
| 76 |
|
| 77 |
import math |
| 78 |
import sys |
| 79 |
|
| 80 |
|
| 81 |
def example1(): |
| 82 |
# This is a long comment. This should be wrapped to fit within 72 |
| 83 |
# characters. |
| 84 |
some_tuple = (1, 2, 3, 'a') |
| 85 |
some_variable = { |
| 86 |
'long': 'Long code lines should be wrapped within 79 characters.', |
| 87 |
'other': [ |
| 88 |
math.pi, |
| 89 |
100, |
| 90 |
200, |
| 91 |
300, |
| 92 |
9876543210, |
| 93 |
'This is a long string that goes on'], |
| 94 |
'more': { |
| 95 |
'inner': 'This whole logical line should be wrapped.', |
| 96 |
some_tuple: [ |
| 97 |
1, |
| 98 |
20, |
| 99 |
300, |
| 100 |
40000, |
| 101 |
500000000, |
| 102 |
60000000000000000]}} |
| 103 |
return (some_tuple, some_variable) |
| 104 |
|
| 105 |
|
| 106 |
def example2(): |
| 107 |
return ('' in {'f': 2}) in {'has_key() is deprecated': True} |
| 108 |
|
| 109 |
|
| 110 |
class Example3(object): |
| 111 |
|
| 112 |
def __init__(self, bar): |
| 113 |
# Comments should have a space after the hash. |
| 114 |
if bar: |
| 115 |
bar += 1 |
| 116 |
bar = bar * bar |
| 117 |
return bar |
| 118 |
else: |
| 119 |
some_string = """ |
| 120 |
Indentation in multiline strings should not be touched. |
| 121 |
Only actual code should be reindented. |
| 122 |
""" |
| 123 |
return (sys.path, some_string) |
| 124 |
|
| 125 |
Options:: |
| 126 |
|
| 127 |
usage: autopep8 [-h] [--version] [-v] [-d] [-i] [--global-config filename] |
| 128 |
[--ignore-local-config] [-r] [-j n] [-p n] [-a] |
| 129 |
[--experimental] [--exclude globs] [--list-fixes] |
| 130 |
[--ignore errors] [--select errors] [--max-line-length n] |
| 131 |
[--range line line] [--indent-size n] |
| 132 |
[files [files ...]] |
| 133 |
|
| 134 |
Automatically formats Python code to conform to the PEP 8 style guide. |
| 135 |
|
| 136 |
positional arguments: |
| 137 |
files files to format or '-' for standard in |
| 138 |
|
| 139 |
optional arguments: |
| 140 |
-h, --help show this help message and exit |
| 141 |
--version show program's version number and exit |
| 142 |
-v, --verbose print verbose messages; multiple -v result in more |
| 143 |
verbose messages |
| 144 |
-d, --diff print the diff for the fixed source |
| 145 |
-i, --in-place make changes to files in place |
| 146 |
--global-config filename |
| 147 |
path to a global pep8 config file; if this file does |
| 148 |
not exist then this is ignored (default: |
| 149 |
~/.config/pep8) |
| 150 |
--ignore-local-config |
| 151 |
don't look for and apply local config files; if not |
| 152 |
passed, defaults are updated with any config files in |
| 153 |
the project's root directory |
| 154 |
-r, --recursive run recursively over directories; must be used with |
| 155 |
--in-place or --diff |
| 156 |
-j n, --jobs n number of parallel jobs; match CPU count if value is |
| 157 |
less than 1 |
| 158 |
-p n, --pep8-passes n |
| 159 |
maximum number of additional pep8 passes (default: |
| 160 |
infinite) |
| 161 |
-a, --aggressive enable non-whitespace changes; multiple -a result in |
| 162 |
more aggressive changes |
| 163 |
--experimental enable experimental fixes |
| 164 |
--exclude globs exclude file/directory names that match these comma- |
| 165 |
separated globs |
| 166 |
--list-fixes list codes for fixes; used by --ignore and --select |
| 167 |
--ignore errors do not fix these errors/warnings (default: E24) |
| 168 |
--select errors fix only these errors/warnings (e.g. E4,W) |
| 169 |
--max-line-length n set maximum allowed line length (default: 79) |
| 170 |
--range line line only fix errors found within this inclusive range of |
| 171 |
line numbers (e.g. 1 99); line numbers are indexed at |
| 172 |
1 |
| 173 |
--indent-size n number of spaces per indent level (default 4) |
| 174 |
|
| 175 |
|
| 176 |
Features |
| 177 |
======== |
| 178 |
|
| 179 |
autopep8 fixes the following issues_ reported by pep8_:: |
| 180 |
|
| 181 |
E101 - Reindent all lines. |
| 182 |
E121 - Fix indentation to be a multiple of four. |
| 183 |
E122 - Add absent indentation for hanging indentation. |
| 184 |
E123 - Align closing bracket to match opening bracket. |
| 185 |
E124 - Align closing bracket to match visual indentation. |
| 186 |
E125 - Indent to distinguish line from next logical line. |
| 187 |
E126 - Fix over-indented hanging indentation. |
| 188 |
E127 - Fix visual indentation. |
| 189 |
E128 - Fix visual indentation. |
| 190 |
E20 - Remove extraneous whitespace. |
| 191 |
E211 - Remove extraneous whitespace. |
| 192 |
E22 - Fix extraneous whitespace around keywords. |
| 193 |
E224 - Remove extraneous whitespace around operator. |
| 194 |
E226 - Fix missing whitespace around arithmetic operator. |
| 195 |
E227 - Fix missing whitespace around bitwise/shift operator. |
| 196 |
E228 - Fix missing whitespace around modulo operator. |
| 197 |
E231 - Add missing whitespace. |
| 198 |
E241 - Fix extraneous whitespace around keywords. |
| 199 |
E242 - Remove extraneous whitespace around operator. |
| 200 |
E251 - Remove whitespace around parameter '=' sign. |
| 201 |
E26 - Fix spacing after comment hash for inline comments. |
| 202 |
E265 - Fix spacing after comment hash for block comments. |
| 203 |
E27 - Fix extraneous whitespace around keywords. |
| 204 |
E301 - Add missing blank line. |
| 205 |
E302 - Add missing 2 blank lines. |
| 206 |
E303 - Remove extra blank lines. |
| 207 |
E304 - Remove blank line following function decorator. |
| 208 |
E309 - Add missing blank line (after class declaration). |
| 209 |
E401 - Put imports on separate lines. |
| 210 |
E501 - Try to make lines fit within --max-line-length characters. |
| 211 |
E502 - Remove extraneous escape of newline. |
| 212 |
E701 - Put colon-separated compound statement on separate lines. |
| 213 |
E70 - Put semicolon-separated compound statement on separate lines. |
| 214 |
E711 - Fix comparison with None. |
| 215 |
E712 - Fix comparison with boolean. |
| 216 |
E721 - Use "isinstance()" instead of comparing types directly. |
| 217 |
W291 - Remove trailing whitespace. |
| 218 |
W293 - Remove trailing whitespace on blank line. |
| 219 |
W391 - Remove trailing blank lines. |
| 220 |
W601 - Use "in" rather than "has_key()". |
| 221 |
W602 - Fix deprecated form of raising exception. |
| 222 |
W603 - Use "!=" instead of "<>" |
| 223 |
W604 - Use "repr()" instead of backticks. |
| 224 |
W690 - Fix various deprecated code (via lib2to3). |
| 225 |
|
| 226 |
autopep8 also fixes some issues not found by pep8_. |
| 227 |
|
| 228 |
- Correct deprecated or non-idiomatic Python code (via ``lib2to3``). Use this |
| 229 |
for making Python 2.6 and 2.7 code more compatible with Python 3. (This is |
| 230 |
triggered if ``W690`` is enabled.) |
| 231 |
- Normalize files with mixed line endings. |
| 232 |
- Put a blank line between a class declaration and its first method |
| 233 |
declaration. (Enabled with ``E309``.) |
| 234 |
- Put a blank line between a class docstring and its first method |
| 235 |
declaration. (Enabled with ``E301``.) |
| 236 |
- Remove blank lines between a function declaration and its docstring. (Enabled |
| 237 |
with ``E303``.) |
| 238 |
|
| 239 |
autopep8 avoids fixing some issues found by pep8_. |
| 240 |
|
| 241 |
- ``E112``/``E113`` for non comments are reports of bad indentation that break |
| 242 |
syntax rules. These should not be modified at all. |
| 243 |
- ``E265``, which refers to spacing after comment hash, is ignored if the |
| 244 |
comment looks like code. autopep8 avoids modifying these since they are not |
| 245 |
real comments. If you really want to get rid of the pep8_ warning, consider |
| 246 |
just removing the commented-out code. (This can be automated via eradicate_.) |
| 247 |
|
| 248 |
.. _eradicate: https://github.com/myint/eradicate |
| 249 |
|
| 250 |
|
| 251 |
More advanced usage |
| 252 |
=================== |
| 253 |
|
| 254 |
By default autopep8 only makes whitespace changes. Thus, by default, it does |
| 255 |
not fix ``E711`` and ``E712``. (Changing ``x == None`` to ``x is None`` may |
| 256 |
change the meaning of the program if ``x`` has its ``__eq__`` method |
| 257 |
overridden.) Nor does it correct deprecated code ``W6``. To enable these |
| 258 |
more aggressive fixes, use the ``--aggressive`` option:: |
| 259 |
|
| 260 |
$ autopep8 --aggressive <filename> |
| 261 |
|
| 262 |
Use multiple ``--aggressive`` to increase the aggressiveness level. For |
| 263 |
example, ``E712`` requires aggressiveness level 2 (since ``x == True`` could be |
| 264 |
changed to either ``x`` or ``x is True``, but autopep8 chooses the former). |
| 265 |
|
| 266 |
``--aggressive`` will also shorten lines more aggressively. It will also remove |
| 267 |
trailing whitespace more aggressively. (Usually, we don't touch trailing |
| 268 |
whitespace in docstrings and other multiline strings. And to do even more |
| 269 |
aggressive changes to docstrings, use docformatter_.) |
| 270 |
|
| 271 |
.. _docformatter: https://github.com/myint/docformatter |
| 272 |
|
| 273 |
To enable only a subset of the fixes, use the ``--select`` option. For example, |
| 274 |
to fix various types of indentation issues:: |
| 275 |
|
| 276 |
$ autopep8 --select=E1,W1 <filename> |
| 277 |
|
| 278 |
Similarly, to just fix deprecated code:: |
| 279 |
|
| 280 |
$ autopep8 --aggressive --select=W6 <filename> |
| 281 |
|
| 282 |
The above is useful when trying to port a single code base to work with both |
| 283 |
Python 2 and Python 3 at the same time. |
| 284 |
|
| 285 |
If the file being fixed is large, you may want to enable verbose progress |
| 286 |
messages:: |
| 287 |
|
| 288 |
$ autopep8 -v <filename> |
| 289 |
|
| 290 |
|
| 291 |
Use as a module |
| 292 |
=============== |
| 293 |
|
| 294 |
The simplest way of using autopep8 as a module is via the ``fix_code()`` |
| 295 |
function. |
| 296 |
|
| 297 |
>>> import autopep8 |
| 298 |
>>> autopep8.fix_code('x= 123\n') |
| 299 |
'x = 123\n' |
| 300 |
|
| 301 |
Or with command-line options: |
| 302 |
|
| 303 |
>>> import autopep8 |
| 304 |
>>> autopep8.fix_code('x.has_key(y)\n', |
| 305 |
... options=autopep8.parse_args(['--aggressive', ''])) |
| 306 |
'y in x\n' |
| 307 |
|
| 308 |
|
| 309 |
Testing |
| 310 |
======= |
| 311 |
|
| 312 |
Test cases are in ``test/test_autopep8.py``. They can be run directly via |
| 313 |
``python test/test_autopep8.py`` or via tox_. The latter is useful for |
| 314 |
testing against multiple Python interpreters. (We currently test against |
| 315 |
CPython versions 2.6, 2.7, 3.2, and 3.3. We also test against PyPy.) |
| 316 |
|
| 317 |
.. _`tox`: https://pypi.python.org/pypi/tox |
| 318 |
|
| 319 |
Broad spectrum testing is available via ``test/acid.py``. This script runs |
| 320 |
autopep8 against Python code and checks for correctness and completeness of the |
| 321 |
code fixes. It can check that the bytecode remains identical. |
| 322 |
``test/acid_pypi.py`` makes use of ``acid.py`` to test against the latest |
| 323 |
released packages on PyPI. |
| 324 |
|
| 325 |
|
| 326 |
Troubleshooting |
| 327 |
=============== |
| 328 |
|
| 329 |
``pkg_resources.DistributionNotFound`` |
| 330 |
-------------------------------------- |
| 331 |
|
| 332 |
If you are using an ancient version of ``setuptools``, you might encounter |
| 333 |
``pkg_resources.DistributionNotFound`` when trying to run ``autopep8``. Try |
| 334 |
upgrading ``setuptools`` to workaround this ``setuptools`` problem:: |
| 335 |
|
| 336 |
$ pip install --upgrade setuptools |
| 337 |
|
| 338 |
Use ``sudo`` if you are installing to the system. |
| 339 |
|
| 340 |
|
| 341 |
Links |
| 342 |
===== |
| 343 |
|
| 344 |
* PyPI_ |
| 345 |
* GitHub_ |
| 346 |
* `Travis CI`_ |
| 347 |
* Coveralls_ |
| 348 |
* Jenkins_ |
| 349 |
|
| 350 |
.. _PyPI: https://pypi.python.org/pypi/autopep8/ |
| 351 |
.. _GitHub: https://github.com/hhatto/autopep8 |
| 352 |
.. _`Travis CI`: https://travis-ci.org/hhatto/autopep8 |
| 353 |
.. _`Coveralls`: https://coveralls.io/r/hhatto/autopep8 |
| 354 |
.. _Jenkins: http://jenkins.hexacosa.net/job/autopep8/ |