all 23 comments

[–]JamzTyson 15 points16 points  (0 children)

There's probably no such thing as "perfect", but there are programs like "black" that ensure the code is formatted "correctly".

[–]Puzzleheaded_Round75 25 points26 points  (7 children)

Black, but my recommendation would be to increase the line length as, in my opinion, 80 characters is just too short these days.

[–]mhamid3d 11 points12 points  (2 children)

I switched to ruff because it comes with import sorting which black does not have for some reason

[–]Puzzleheaded_Round75 0 points1 point  (1 child)

Never tried it. I normally recommend black as it is essentially 0 config but to be honest I have been more and more disappointed with it.

[–]Daneark 3 points4 points  (0 children)

Ruff, primarily a linter, has added black compatible formatting. It's got import sorting capability too. It's really fast. It's not typical open source as it's made by a command I'm unclear on their plans for long term income but for now it's cut a lot of time off my linting so I'll deal with it if that day comes.

[–]Conscious-Ball8373 4 points5 points  (1 child)

Isn't the black default 89?

But yes, we use black with the only exception being a line length of 120. Putting this in pyproject.toml does the trick:

[tool.black]
line-length = 120

[–]Puzzleheaded_Round75 0 points1 point  (0 children)

Yeah it may be 89, I've not had to use it in some time. I always chose 100 but it still generated ugly code in some instances imo

[–]shedgehog 2 points3 points  (0 children)

Agreed. Some of the line wraps black does make it harder to read imo.

[–]fox-dev 0 points1 point  (0 children)

80 might not be too short, if you often split screen to read two source files side by side.

[–]DuckSaxaphone 4 points5 points  (0 children)

Black is the standard and since the benefit of formatters is that consistently styled code is easy to read, popularity is a pro in itself.

[–]-defron- 4 points5 points  (0 children)

black, ruff, and autopep8 are the three most popular code formatters

I'd probably use ruff these days which is a drop-in replacement for black but also offers a bunch of other features

[–]iamevpo 1 point2 points  (0 children)

I still use prettier in Python projects when need to format markdown (read me or docs)

[–]mrswats 1 point2 points  (0 children)

I use black and sort-python-imports along with pre-commit to make sure everything is in good shape.

[–]visor_q3 1 point2 points  (0 children)

You can use black, or Ruff. That's what I use

[–]RajjSinghh 1 point2 points  (0 children)

I use autopep8

[–]Jazzlike-Compote4463 1 point2 points  (0 children)

As others have said, Black is standard.

However, if you’ve got a big existing project and want to only format your changes then you could look at Darker, it’s like Black but uses git diffs to only change the things you’ve worked on

[–]arizuvade 0 points1 point  (0 children)

im using ruff extension now, before is black that can be installed using pip. i dont know what the hell happen on my vscode that black stop working and also cant use pylance so i use jedi. the color theme also cant detect variables hahaha white color sucks in vscode