Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code Python autopep8 does not honor 2 spaces hanging indentation

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?

like image 678
Logan Yang Avatar asked Jan 29 '19 22:01

Logan Yang


People also ask

How do you change indentation spaces in VS code?

Type “Indentation” into the search field then head to the “Editor: Tab Size” section. Replace the default space number with your preferred one: Your setting will be applied and reflected immediately. If this doesn't happen (it's a little lag sometimes), just reload or restart your VS Code.

How to get 2 spaces instead of 4 in autopep8?

If you want 2 spaces instead of 4 - the fix is to add this to your settings.json. Btw you can see your autopep8 arguments by opening the command palette (⌘-shift-p on mac) and entering >Python: Show Language Server Output then switching to view the "Python" log.

Where can I See my autopep8 arguments?

Btw you can see your autopep8 arguments by opening the command palette (⌘-shift-p on mac) and entering >Python: Show Language Server Output then switching to view the "Python" log. This seems to be a common issue. See: VS Code Python autopep8 does not honor 2 spaces hanging indentation

How do I change the indentation in Visual Studio Code?

VS Code: How To Change Indentation (2 spaces, 4 spaces) 1. Open your VS Code and: Go to Code > Preferences > Settings if you’re using macOS Go to File > Preferences > Settings... 2. Type “Indentation” into the search field then head to the “Editor: Tab Size” section. Replace the default...

What is autopep8 in Python?

Project description autopep8 automatically formats Python code to conform to the PEP 8 style guide. It uses the pycodestyle 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 pycodestyle.


2 Answers

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...

like image 126
Kris O'Mealy Avatar answered Oct 21 '22 18:10

Kris O'Mealy


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
like image 42
Navid Avatar answered Oct 21 '22 19:10

Navid