Search code examples
pythonvisual-studio-codeautopep8

Why autopep8 don't format on save?


Autopep8 don't working at all.

Here's my setting:

{
    "editor.defaultFormatter": "ms-python.autopep8",
    "editor.formatOnSave": true,
    "editor.formatOnPaste": true,
    "editor.formatOnType": true,

    "autopep8.showNotifications": "always",
    "indentRainbow.colorOnWhiteSpaceOnly": true,
    "[python]": {
        "editor.formatOnType": true
    },

    "workbench.colorTheme": "Monokai Dimmed",
    "editor.codeActionsOnSave": {
    },
    "autopep8.args": [
        "--in-place --aggressive --aggressive"
    ]
}

For exaple I using this code:

import math, sys;

def example1():
    ####This is a long comment. This should be wrapped to fit within 72 characters.
    some_tuple=(   1,2, 3,'a'  );
    some_variable={'long':'Long code lines should be wrapped within 79 characters.',
    'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'],
    'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1,
    20,300,40000,500000000,60000000000000000]}}
    return (some_tuple, some_variable)
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:
                    some_string = """
                       Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""
                    return (sys.path, some_string)

The only way I can use autopep8 is import it as module and run autopep8.fix_code('''code example''') and I can't get result that I want. That am i doing wrong?

Thanks!

P. S. I'm using VS Code 1.75.1, Python 3.9.16, autopep8 v2022.2.0.

Excepted result:

import math
import sys


def example1():
    # This is a long comment. This should be wrapped to fit within 72
    # characters.
    some_tuple = (1, 2, 3, 'a')
    some_variable = {
        'long': 'Long code lines should be wrapped within 79 characters.',
        'other': [
            math.pi,
            100,
            200,
            300,
            9876543210,
            'This is a long string that goes on'],
        'more': {
            'inner': 'This whole logical line should be wrapped.',
            some_tuple: [
                1,
                20,
                300,
                40000,
                500000000,
                60000000000000000]}}
    return (some_tuple, some_variable)


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:
            some_string = """
                       Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""
            return (sys.path, some_string)

Solution

  • Add "python.formatting.provider": "autopep8", to your settings.json.

    Read document for more information.