35

I'm trying to get autopep8 work to properly indent Python code with 2 spaces instead of 4. I'm using VS Code with Python extension which uses autopep8 for formatting. I found here that autopep8 can be configured to use 2 spaces by

"python.formatting.autopep8Args": ["--indent-size=2"]

But it does not work for me.

My situation is like this. When I press enter, it correctly starts the next line with the same indentation as the previous line. Press enter after an open parenthesis, it correctly starts the new line with 2 more spaces. But when I paste or save (I have "editor.formatOnPaste" and "editor.formatOnSave" set to true), the annoying thing happened: all the 2-space indentation inside the parentheses became 4 (other 2-space indentation are unaffected). Why is it doing this and how can I make it 2 spaces everywhere?

enter image description here

====EDIT====

I found out that the pylint error Wrong hanging indentation (remove 2 spaces). [bad-continuation]. It's because my pylintrc has indent-after-paren=2. I'm wondering if autopep8 or other Python formatter can set this property?

Logan Yang
  • 2,364
  • 6
  • 27
  • 43
  • I'm curious why you want to have 2 spaces of (hanging) indentation; [the standard is 4 spaces](https://www.python.org/dev/peps/pep-0008/#indentation). – 9769953 Jan 30 '19 at 22:26
  • 12
    It's just preference. The question is whether there is a way to customize this. – Logan Yang Jan 30 '19 at 22:38
  • 3
    I realise what your question is. But I'm inclined to say: go with the standard/rest of the world. It makes things, including potentially sharing your code with others, much easier. – 9769953 Jan 31 '19 at 06:56
  • 3
    According to https://github.com/google/vim-codefmt/issues/24, it seems autopep8 didn't make progress on it. So either pick black, yapf as your formatter. I prefer use the 2 spaces as indentation. Simply because some companies, like Google has this style to save some space. – Kimmi May 31 '19 at 18:23
  • 5
    hey guys. I fighting with this error since I started coding python with vscode. It is VERY ANNOYING to me. putting `"python.formatting.autopep8Args": ["--indent-size=2"]` inside `settings.json` WORKED for me. THANK YOU. – canbax Oct 29 '20 at 09:34
  • [canbax](https://stackoverflow.com/users/3209523/canbax) thanks a lot man, Can you reply to this ask to vote up? – Roberth Solís Jan 04 '22 at 01:32

4 Answers4

16

Adding --indent-size=2 --ignore=E121 worked for me. autopep8 indent size 2

MutantMahesh
  • 1,480
  • 15
  • 20
15

I also had to include this in my array in settings.json, similar to yours.

"--ignore E121"

According to https://pypi.org/project/autopep8/, this setting ensures your indents are a multiple of 4. By not enforcing that, the configured tab size in VSCode is used.

E121 - Fix indentation to be a multiple of four.

That being said, your indentation is still "acceptable" according to pep8, so it actually will not change it to the 4 spaces you are expecting in your parens. I had to outdent mine one level, then when it ran again, it didn't change it.

Unfortunately this is actually just a workaround and it actually negatively affects other indention rules...

You can see in the code for pep8 that they hardcode the default tab size to be the "python way" (4 spaces) in:

https://github.com/hhatto/autopep8/blob/120537a051d7f3cbbb5c4ede19b9e515156bd3d1/autopep8.py#L104

That makes it look like the hanging indent is just not respecting the --indent-size option...

Kris O'Mealy
  • 172
  • 1
  • 9
7

had the same issue, here is the solution:

  1. Navigate to library directory of your environment
  2. Open autopep8.py
  3. Search for "DEFAULT_INDENT_SIZE" and change it to 2
Navid
  • 73
  • 1
  • 2
6

Using "autopep8.args" instead "python.formatting.autopep8Args" worked for me.

"autopep8.args": [
  "--indent-size=2"
]
Itta
  • 61
  • 1
  • 2