Skip to main content

A tool that automatically formats Python code to conform to the PEP 8 style guide

Project description

Build status

autopep8 automatically formats Python code to conform to the PEP 8 style guide. It uses the pep8 utility to determine what parts of the code needs to be formatted. autopep8 is capable of fixing most of the formatting issues that can be reported by pep8.

Installation

From pip:

$ pip install --upgrade autopep8

Requirements

autopep8 requires pep8.

Usage

To modify a file in place (with all fixes enabled):

$ autopep8 --in-place --aggressive <filename>

Before running autopep8.

import sys, os;

def example1():
    a=(   1,2, 3,'a'  );
    ####This is a long comment. This should be wrapped to fit within 72 characters.
    some_variable = [100,200,300,9876543210,'This is a long string that goes on']
def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key(''));
class Example3(   object ):
    def __init__    ( self, bar ):
     #Comments should have a space after the hash.
     if bar : bar+=1;  bar=bar* bar   ; return bar
     else:
                    indentation_in_strings_should_not_be_touched = """
                       hello
world
"""
                    raise ValueError, indentation_in_strings_should_not_be_touched
    def my_method(self):
                                              print(self);

After running autopep8.

import sys
import os


def example1():
    a = (1, 2, 3, 'a')
    # This is a long comment. This should be wrapped to fit within 72
    # characters.
    some_variable = [
        100,
        200,
        300,
        9876543210,
        'This is a long string that goes on']


def example2():
    return ('' in {'f': 2}) in {'has_key() is deprecated': True}


class Example3(object):

    def __init__(self, bar):
        # Comments should have a space after the hash.
        if bar:
            bar += 1
            bar = bar * bar
            return bar
        else:
            indentation_in_strings_should_not_be_touched = """
                       hello
world
"""
            raise ValueError(indentation_in_strings_should_not_be_touched)

    def my_method(self):
        print(self)

Options:

Usage: autopep8 [options] [filename [filename ...]]
Use filename '-'  for stdin.

Automatically formats Python code to conform to the PEP 8 style guide.

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -v, --verbose         print verbose messages; multiple -v result in more
                        verbose messages
  -d, --diff            print the diff for the fixed source
  -i, --in-place        make changes to files in place
  -r, --recursive       run recursively over directories; must be used with
                        --in-place or --diff
  -j n, --jobs=n        number of parallel jobs; match CPU count if value is
                        less than 1
  -p n, --pep8-passes=n
                        maximum number of additional pep8 passes (default:
                        infinite)
  -a, --aggressive      enable non-whitespace changes; multiple -a result in
                        more aggressive changes
  --exclude=globs       exclude file/directory names that match these comma-
                        separated globs
  --list-fixes          list codes for fixes; used by --ignore and --select
  --ignore=errors       do not fix these errors/warnings (default: E24)
  --select=errors       fix only these errors/warnings (e.g. E4,W)
  --max-line-length=n   set maximum allowed line length (default: 79)
  --range=start end     only fix errors found within this inclusive range of
                        line numbers (e.g. 1 99); line numbers are indexed at
                        1

Features

autopep8 fixes the following issues reported by pep8:

E101 - Reindent all lines.
E111 - Reindent all lines.
E121 - Fix indentation to be a multiple of four.
E122 - Add absent indentation for hanging indentation.
E123 - Align closing bracket to match opening bracket.
E124 - Align closing bracket to match visual indentation.
E125 - Indent to distinguish line from next logical line.
E126 - Fix over-indented hanging indentation.
E127 - Fix visual indentation.
E128 - Fix visual indentation.
E20  - Remove extraneous whitespace.
E211 - Remove extraneous whitespace.
E22  - Fix extraneous whitespace around keywords.
E224 - Remove extraneous whitespace around operator.
E22  - Fix missing whitespace around operator.
E231 - Add missing whitespace.
E241 - Fix extraneous whitespace around keywords.
E242 - Remove extraneous whitespace around operator.
E251 - Remove whitespace around parameter '=' sign.
E26  - Fix spacing after comment hash for inline comments.
E269 - Fix spacing after comment hash for block comments.
E27  - Fix extraneous whitespace around keywords.
E301 - Add missing blank line.
E302 - Add missing 2 blank lines.
E303 - Remove extra blank lines.
E304 - Remove blank line following function decorator.
E309 - Add missing blank line (after class declaration).
E401 - Put imports on separate lines.
E501 - Try to make lines fit within --max-line-length characters.
E502 - Remove extraneous escape of newline.
E701 - Put colon-separated compound statement on separate lines.
E70  - Put semicolon-separated compound statement on separate lines.
E711 - Fix comparison with None.
E712 - Fix comparison with boolean.
E721 - Use "isinstance()" instead of comparing types directly.
W191 - Reindent all lines.
W291 - Remove trailing whitespace.
W293 - Remove trailing whitespace on blank line.
W391 - Remove trailing blank lines.
W601 - Use "in" rather than "has_key()".
W602 - Fix deprecated form of raising exception.
W603 - Use "!=" instead of "<>"
W604 - Use "repr()" instead of backticks.
W690 - Fix various deprecated code (via lib2to3).

autopep8 also fixes some issues not found by pep8.

  • Correct deprecated or non-idiomatic Python code (via lib2to3). Use this for making Python 2.6 and 2.7 code more compatible with Python 3. (This is triggered if W690 is enabled.)

  • Format block comments. (This is triggered if E269 is enabled.)

  • Normalize files with mixed line endings.

  • Put a blank line between a class declaration and its first method declaration. (Enabled with E309.)

  • Put a blank line between a class docstring and its first method declaration. (Enabled with E301.)

  • Remove blank lines between a function declaration and its docstring. (Enabled with E303.)

More advanced usage

By default autopep8 only makes whitespace changes. Thus, by default, it does not fix E711 and E712. (Changing x == None to x is None may change the meaning of the program if x has its __eq__ method overridden.) Nor does it correct deprecated code W6. To enable these more aggressive fixes, use the --aggressive option:

$ autopep8 --aggressive <filename>

Use multiple --aggressive to increase the aggressiveness level. For example, E712 requires aggressiveness level 2 (since x == True could be changed to either x or x is True, but autopep8 chooses the former).

--aggressive will also shorten lines more aggressively. It will also remove trailing whitespace more aggressively. (Usually, we don’t touch trailing whitespace in docstrings and other multiline strings. And to do even more aggressive changes to docstrings, use docformatter.)

To enable only a subset of the fixes, use the --select option. For example, to fix various types of indentation issues:

$ autopep8 --select=E1,W1 <filename>

Similarly, to just fix deprecated code:

$ autopep8 --aggressive --select=W6 <filename>

The above is useful when trying to port a single code base to work with both Python 2 and Python 3 at the same time.

If the file being fixed is large, you may want to enable verbose progress messages:

$ autopep8 -v <filename>

Use as a module

The simplest way of using autopep8 as a module is via the fix_code() function.

>>> import autopep8
>>> autopep8.fix_code('x=       123\n')
'x = 123\n'

Testing

Test cases are in test/test_autopep8.py. They can be run directly via python test/test_autopep8.py or via tox. The latter is useful for testing against multiple Python interpreters. (We currently test against CPython versions 2.6, 2.7, 3.2, and 3.3. We also test against PyPy.)

Broad spectrum testing is available via test/acid.py. This script runs autopep8 against Python code and checks for correctness and completeness of the code fixes. It can check that the bytecode remains identical. test/acid_pypi.py makes use of acid.py to test against the latest released packages on PyPI. In a similar fashion, test/acid_github.py tests against Python code in Github repositories.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

autopep8-0.9.7.tar.gz (53.7 kB view details)

Uploaded Source

File details

Details for the file autopep8-0.9.7.tar.gz.

File metadata

  • Download URL: autopep8-0.9.7.tar.gz
  • Upload date:
  • Size: 53.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for autopep8-0.9.7.tar.gz
Algorithm Hash digest
SHA256 9071ef91a9cd5fdacca64d92fbb82feaa8552d2f01ceed85fb6f8a914f742038
MD5 97d49d9107958d20e67f317b5d3e65c1
BLAKE2b-256 864ad4c5a67366a92a57187f43a06c448bff5d9342eaf969b432d2656f3d3095

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page