DEV Community

Cover image for Python code formatting using Black

Python code formatting using Black

Maxime Gaston on September 09, 2019

What is Black The official Python style guide is PEP8. Linters such as pycodestyle or flake8 can show you if, according to PEP8, your co...
Collapse
 
akaihola profile image
Antti Kaihola

If you're contributing to an Open Source project and want to apply Black to only your contribution without reformatting the complete code base, check out darker – it's a wrapper for Black for just reformatting the lines you've changed.

Collapse
 
nicolaerario profile image
Nicola Erario

Still with autopep8 here that I like over black for some array formatting behavior. However black really worth a try.

Collapse
 
notsag profile image
Maxime Gaston • Edited

Black also formats list and arrays, trying render a one liner expression if shorter than line-length or else putting one element per line :

# before:

numbers = [1,
     2,
     3
]

# after:

numbers = [1, 2, 3]

It also does the same for function parameters or statements.

Collapse
 
nicolaerario profile image
Nicola Erario

I perfectly know 😎

Collapse
 
wangonya profile image
Kelvin Wangonya

Very nice! Thanks for the post.

Collapse
 
notsag profile image
Maxime Gaston

My pleasure, I hope you will find Black as useful as I do.

Collapse
 
codemouse92 profile image
Jason C. McDonald

I do love Black!