autopep8 Documentation

repository·main·Indexed 26 days ago

https://github.com/hhatto/autopep8

autopep8 is a tool that automatically formats Python code to conform to the PEP 8 style guide by using pycodestyle to identify and fix formatting issues. It provides a CLI for in-place editing, recursive directory processing, and parallel execution, as well as a programmatic API via fix_code and fix_file. The tool supports aggressive and experimental formatting modes, custom fix extensions, and configuration via pyproject.toml.

Tokens
3.9K
Snippets
12
Records
51
Agent score
87%

What's inside autopep8

  1. Submit a pull request to autopep8

    main

    Before submitting a pull request:

    1. Verify that the issue is not a pycodestyle bug. If it is, file the PR at the pycodestyle repository.
    2. Add a test case to test/test_autopep8.py that demonstrates your changes.
    3. Ensure all existing tests pass.
  2. Integrate autopep8 with pre-commit

    main

    To use autopep8 as a pre-commit hook, add the following to your .pre-commit-config.yaml file:

    repos:
    -   repo: https://github.com/hhatto/autopep8
        rev: ...  # select the tag or revision you want
        hooks:
        -   id: autopep8
  3. Disable autopep8 for specific code blocks

    main

    You can disable autopep8 for specific sections of a file by using # autopep8: off and re-enabling it with # autopep8: on. Alternatively, # fmt: off and # fmt: on are also valid markers.

    # autopep8: off
        [
            [23, 23, 13, 43],
            [32, 34, 34, 34],
            [56, 34, 34, 11],
            [10, 10, 10, 10],
        ]
    # autopep8: on
  4. Overview of autopep8

    main
    autopep8 is a tool that automatically formats Python code to conform to the PEP 8 style guide. It works by identifying PEP 8 violations (using pycodestyle) and applying corresponding fixes to the source code.
  5. Configure autopep8 via configuration files

    main

    autopep8 searches for configuration in several locations:

    1. Global Config: $HOME/.config/pycodestyle (or ~/.pycodestyle on Windows). You can also specify a custom global file using --global-config.
    2. Project Config: setup.cfg, tox.ini, .pep8, or .flake8 in the directory of the target file. These can use the [pycodestyle], [pep8], or [flake8] sections.
    3. pyproject.toml: Uses the [tool.autopep8] section. This takes precedence over all other configuration files.
    [tool.autopep8]
    max_line_length = 120
    ignore = "E501,W6"  # or ["E501", "W6"]
    in-place = true
    recursive = true
    aggressive = 3
  6. Extend autopep8 with custom fixes

    main

    You can add custom formatting rules to autopep8 by defining functions in the module with the pattern fix_<code_name>(source).

    • The function must take source as an argument.
    • It must return the fixed source code.
    • These functions are automatically picked up by apply_global_fixes().

    For fixes that depend on pycodestyle, you should add them as methods to the FixPEP8 class.

  7. Troubleshoot pkg_resources.DistributionNotFound

    main

    If you encounter pkg_resources.DistributionNotFound while running autopep8, it is likely due to an outdated version of setuptools. Upgrade setuptools to resolve this issue.

    $ pip install --upgrade setuptools
  8. Report a bug in autopep8

    main

    When submitting a bug report, ensure the issue is not actually a bug in pycodestyle. If pycodestyle is behaving incorrectly, file the report at the pycodestyle repository instead.

    For an autopep8 bug report, please include:

    1. autopep8 --version output
    2. pycodestyle --version output
    3. python --version output
    4. uname -a (if on Unix)
    5. The example input that causes the bug
    6. The autopep8 command-line options used
    7. The expected output
    8. Confirmation that the bug persists on the latest version
  9. Modify Python files in place with aggressive formatting

    main

    To automatically format a file and save the changes directly to the file, use the --in-place flag. Adding --aggressive multiple times increases the level of non-whitespace changes applied.

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