Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-PEP8 is adding lines by turning my lambda into def function, how do I disable this specific auto format?

I am using Visual Studio Code and PEP8 is automatically formatting a part of my code, I was just learning about lambdas and I had a 3 line code like this:

It went from this 3 line code:

# Lambda example
divide = lambda x, y: x/y
print(divide(10, 2))

To this 7 line code:

# Lambda example


def divide(x, y): return x/y


print(divide(10, 2))

Does anyone know how do I make this program to specifically not convert my lambda function into def function?

It has been formatting my code really good, so I don't want to completely disable this automatic feature, just for the lambda thing.

like image 329
Jsh0s Avatar asked Jan 28 '19 05:01

Jsh0s


2 Answers

There are some methods to disable auto converting lambda to function definition.

  • Using --ignore=E731 as explained by Anthony Sottile in (his/her) answer. Press Ctrl+,, search for autopep8, and add item --ignore=E731 as shown in the following screenshot.

    enter image description here

  • Or you uninstall autopep8 first by invoking pip uninstall autopep8 and then install yapf via pip install yapf.

  • I let others add other methods from this line.

like image 124
The Code Mocker Avatar answered Oct 09 '22 23:10

The Code Mocker


This is triggered by the pycodestyle code E731

You can disable this with --ignore=E731

In a config file (for instance tox.ini / setup.cfg):

[pep8] 
ignore=E731
like image 45
Anthony Sottile Avatar answered Oct 09 '22 23:10

Anthony Sottile