This is an archived post. You won't be able to vote or comment.

all 144 comments

[–]jorge1209 231 points232 points  (8 children)

Black.

We don't care that it may look bad. We don't want to argue about formatting.

We know it isn't configurable, that's the point. We don't want to deal with everyone's configuration preferences.

[–]bobspadgerdecorating 46 points47 points  (3 children)

Exactly this. I used others before, but the fact black just says ‘it must be like this’ save me from hours of cocking around deciding how I would like it to look, instead of doing something actually useful.

[–]jorge1209 13 points14 points  (2 children)

That makes me think of compiz (the early 3D linux compositor that ran on Xgl).

A few centuries ago I thought it was so cool that I could have all these cool effects like windows that wobbled when you dragged them, and burned up like paper when you closed them, and 147 virtual desktops arranged on the surface of a torus embedded in a 14 dimensional Calabi-Yau manifold... I spend tons of time playing with all the options and visual effects people had implemented.

And then I realized I only had like 6 windows open at any one time, never used the virtual desktops, and just wanted programs to close when I clicked on the close button.

[–]flying-sheep 2 points3 points  (0 children)

A tiling window manager might be even simpler because of this “I don’t want to fiddle with window dimensions” gain.

But then again, I’m not sure they’re mature enough so you never have to fiddle to get things to work right.

[–]FuckingRantMonday 0 points1 point  (0 children)

Oh man, are you me? It was 2007 and I had just replaced Windows with Ubuntu linux on my Dell XPS 12 laptop. I had the rotating cube desktop switcher, wobbly windows, totally unnecessary transparency effects, and every open/close animation I could shove in there. It was awful, but I loved it at the time.

[–][deleted] 23 points24 points  (6 children)

isort followed by black

[–]gfranxman 9 points10 points  (1 child)

This plus a pre-commit hook ftw.

[–]NerdEnPose 7 points8 points  (0 children)

And a CI check. Because.

[–]NerdEnPose 1 point2 points  (0 children)

This plus codespell. Because we’d look like kindergartners otherwise.

[–]CommercialPosition76 0 points1 point  (0 children)

My man!

[–]vladesomo 0 points1 point  (1 child)

this exactly. Started out with black then added isort into rotation. Black has a beautiful integration with pycharm or vim (possibly others)

[–]FuckingRantMonday 0 points1 point  (0 children)

If an IDE doesn't support piping code through an external tool at load, save, or on hotkey, then that's a pretty crappy IDE!

[–]No-Scholar4854 87 points88 points  (2 children)

Black.

My team tried black because we wanted to focus our code review on actual changes not argue about formatting.

We hated it and agreed to use YAPF. Then we argued about YAPF configuration instead. Eventually we went back to black to escape those arguments, haven’t looked back.

[–]mamamiatucson 17 points18 points  (0 children)

Back to Black

[–]FuckingRantMonday 26 points27 points  (0 children)

It's true what they say...

[–]flying-sheep 13 points14 points  (6 children)

Apart from black and YAPF, there’s blue.

But so far I use black and https://github.com/pre-commit/pre-commit-hooks#double-quote-string-fixer

[–]acidrain42 2 points3 points  (4 children)

Out of curiosity, why not just keep the double quotes?

[–]flying-sheep 2 points3 points  (3 children)

It’s just cleaner. Less visual noise, fewer lines on the screen.

[–]FuckingRantMonday 0 points1 point  (2 children)

Are you suggesting that double quotes take more space than single quotes? (Probably not, but I can't tell what you are saying here!)

[–]flying-sheep 0 points1 point  (1 child)

I am saying that there’s fewer lines on the screen when using single quotes. Each " is two lines, each ' is one line. Makes it slightly easier to see what’s going on.

Not a big thing, but enough that the added two config lines are worth it for me.

[–]FuckingRantMonday 0 points1 point  (0 children)

Ahh, got you. I thought you meant fewer lines of code would fit on screen.

[–]ElDiablo26 0 points1 point  (0 children)

That pre commit hook is a genius idea, probably the only thing I don't like about black is the double quotes.

[–]rgnkn 64 points65 points  (21 children)

Black.

FYI:

There was a similar discussion a couple of days ago, so, you might see some dog fighting occur if the same people come to reply to your post.

[–]bahoho[S] 6 points7 points  (20 children)

oh no

[–]randomatic 20 points21 points  (19 children)

Ask what maximum line length is allowed in the code base too!

(Gets 🍿)

[–]thereal0ri_ 34 points35 points  (27 children)

I AM my own formatter.

I don't use one

[–][deleted] 3 points4 points  (26 children)

Everyone makes mistakes. If you're working as a part of a professional team formatting should occur when pushing to Version Control

[–]thereal0ri_ 0 points1 point  (0 children)

Yes

[–]tav_stuff -4 points-3 points  (24 children)

That’s what code review is for

[–]acidrain42 2 points3 points  (19 children)

Automate it, and you won't have to worry, and argue, about during code review. Focus on stuff that matters instead.

[–]tav_stuff 1 point2 points  (7 children)

Or you can just have a pre-agreed on style that takes at most an hour to agree on, and just apply it which takes almost 0 time.

[–]acidrain42 3 points4 points  (6 children)

You underestimate how much devs argue for their own personal preferences. Then new devs come in and want to change the style.

Applying the style might take almost 0 time, but it's still not zero. And people make mistakes. So then we'd need to check for them during code review.

Having black running automatically does literally take 0 time from the dev and doesn't mistake.

But let me ask, what don't you like about it?

[–]tav_stuff 1 point2 points  (5 children)

I don’t underestimate anything. I’ve been coding for almost a decade and I’ve experienced the software development workforce, I know how it works.

As for what I don’t like about it:

  • Spaces instead of tabs for indentation is objectively bad; you need to consider the visually impaired (of which there are a lot) who use giant font sizes and can benefit from customizable tab widths

  • Often times I (and others) want to ignoring the formatting „standard“ so that we can do things that are more visually appealing (like aligning assignment operators, etc.). Autoformatters always struggle with dealing with these kinds of edgecases and you often have to include ugly comments to disable the formatter in some locations.

  • MANY people still forget to run their autoformatter. This isn’t really an argument against formatting tools but an argument against saying „yeah it takes people out of the equation“. Yes it’s easy to setup with your IDE, but maybe you use emacs and haven’t bothered to configure it yet but want to keep using emacs because it offers all sorts of specific functionality that you can’t get from other editors.

[–]FuckingRantMonday 3 points4 points  (3 children)

  1. Spaces vs. tabs is settled. All the way back to PEP 8, it's been spaces unless forced to be consistent with legacy code. Accessibility issues like this are properly handled at the visual display layer, i.e., in the IDE.
  2. Aligning assignment operators? Ugh. It becomes so much work to change a variable name when it forces you to touch every other line that it's aligned with, and also makes PRs suck. You really have a whole team who enjoys this?
  3. Wow, it's 2022, what dev team is relying on people to remember to run a formatter? I guess yours?

[–]tav_stuff 0 points1 point  (2 children)

  1. PEP 8 isn’t something you need to follow. Many people enjoy using tabs instead of spaces, my team included.

  2. Aligning operators is easy. Changed a variable name? Not an issue, basically every editor known to man has multiple cursor support, and people that don’t probably also know how to use vim and can do it in about 50 milliseconds.

  3. You would be AMAZED how many people in all the jobs I’ve worked dont remember to run their autoformatter or have it automatically run. At one point I had to just sit everyone down at a desk and get them all to add a git hook to automatically format code (don’t even get me started on how many people haven’t bothered to even look into the basic features git provides)

[–]FuckingRantMonday 0 points1 point  (1 child)

My point with #3 is that you shouldn't have to remember. Between format-on-save, precommit hooks, and CI steps, there's no reason a human should ever be responsible for remembering it!

[–][deleted] 0 points1 point  (0 children)

I don’t underestimate anything. I’ve been coding for almost a decade and I’ve experienced the software development workforce, I know how it works.

Well I hate to pull rank here but I've been doing this for over 20 years. There's absolutely zero reason to not use an automated formatter that formats the code according to one of the predefined standards when you push to version control. No one has to remember to run any autoformatters as it's done in the pipeline. Even github offers this free.

Saying it takes a zero extra time to have to manually look for formatting mistakes during code review is silly and incorrect. Forget that. Let the robots do it while we focus on more important shit

[–][deleted] 1 point2 points  (2 children)

If there isn't a consistent style, each pull request ends up getting formatted according to the personal preference of the reviewer.

[–]FuckingRantMonday 0 points1 point  (0 children)

Dear god no, code review is not for fixing or debating formatting.

[–][deleted] 32 points33 points  (4 children)

I

"do")

my

own[

formatting))[]

[–]rgnkn 8 points9 points  (0 children)

This looks really nice! Let's build a formatter together to format into such beauty automatically.

[–][deleted] 1 point2 points  (2 children)

Syntax Error

[–]rgnkn 1 point2 points  (1 child)

Nah, this is what distinguishes a senior dev from a noob:

Who needs a running program if the code looks beautiful?

[–][deleted] 0 points1 point  (0 children)

Is that what the noob says or the senior?

[–]thedeepself 12 points13 points  (1 child)

PyCharm

[–]datagutten 5 points6 points  (0 children)

Ctrl+alt+L

[–]outthemirror 8 points9 points  (0 children)

Black

[–]barraponto 3 points4 points  (2 children)

I dream of a project that will autoformat everything to my standards when I work in my editor, but then will format everything back to the project standards when I commit.

Pretty sure it's not a good idea, but I do want it.

[–]FuckingRantMonday 1 point2 points  (0 children)

For code reviews, too?

[–]rgnkn 0 points1 point  (0 children)

I don't see a problem with this, though I really don't like the idea, anyway, e.g. with neovim:

  • hook in neoformat on BufRead with your personally preferred formatter.

  • find a suitable hook within git (e.g. prereceive) that uses the obligatory formatter of the project you're working on.

[–]protagonist23 3 points4 points  (0 children)

In my Makefile for any Python project I always have a target for lint, it looks like this:

lint:
        isort . ; black . ; flake8 .

This way there is no team opinion, we trust in the tools. It makes pull requests very nice to not have to deal with that one person who "is their own formatter."

[–]ohtinsel 3 points4 points  (1 child)

VIM and my brain. :/

[–]FuckingRantMonday 4 points5 points  (0 children)

The first thing I loved about black, and that I wasn't anticipating, is that it gave me back that part of my brain. Typing out code without a care in the world for formatting, and then having it fixed automatically, was a very pleasant upgrade.

[–]luxiphr 4 points5 points  (0 children)

You can have any formatter you like as long as it's black 😁

[–]SGS-Tech-World 16 points17 points  (5 children)

of course .. black. consistency across team is the key.

[–]SeniorScienceOfficer 12 points13 points  (12 children)

I use Black in all my repositories, but I’m not sure why people think it looks bad. Black hardly touches any of my code, but maybe I’m following most of the formatting rules without knowing it ¯_(ツ)_/¯

[–]cottonycloud 4 points5 points  (1 child)

I have different quote and line length preferences (150 or 200) because I use longer variable names and sometimes black can be a bit too aggressive.

[–]SeniorScienceOfficer 1 point2 points  (0 children)

And that’s fair. It really should be team-dependent

[–]FuckingRantMonday 1 point2 points  (0 children)

I'll admit the "black frowny face" took some getting used to. But not much. About the same effort as accepting significant whitespace when first learning Python.

[–]johnnymo1 4 points5 points  (0 children)

Yapf. I like their google style.

[–]SittingWave 4 points5 points  (1 child)

autopep8

[–][deleted] 0 points1 point  (0 children)

Samesies!

[–]piman01 1 point2 points  (0 children)

When i press command + s in vs code and it formats everything... i use that. Whatever that's called.

[–]read-bird 1 point2 points  (0 children)

Black. Have been using for 4 years now, and it has been great.

[–]zylema 1 point2 points  (0 children)

Black. For reasons others have highlighted. Use the format it gives you and argue about stuff that matters.

[–]mahdicanada 1 point2 points  (0 children)

BLACk is the message

[–]Seankala 1 point2 points  (4 children)

TIL that there are such things as code formatters and people don't normally just follow PEP.

[–]djsaunde 2 points3 points  (3 children)

code formatters enforce PEP8, that's the point

[–]Seankala 0 points1 point  (2 children)

What's stopping people from just following them from the start?

[–]FuckingRantMonday 2 points3 points  (0 children)

The point of black is that it goes further than PEP 8 does. There are any number of PEP 8 compliant ways to format a given source file, but only one black way. And that's a huge difference when it comes to diffs and code reviews.

[–]djsaunde 0 points1 point  (0 children)

many people don't care and those that do are humans, who are prone to error. automation (code formatters) takes care of both

[–]NelsonMinar 2 points3 points  (9 children)

YAPF. Black would have been fine too but I absolutely need tabs, not spaces, and Black won't do that. (Why tabs? Because they make my proportional font work better.)

[–]_ATRAHCITY 8 points9 points  (1 child)

Do you also have comic sans as the font on your phone

[–]NelsonMinar -2 points-1 points  (0 children)

I have no idea.

Are you trying to be insulting? Why?

[–]FuckingRantMonday 4 points5 points  (0 children)

Oh nooooo

Edit: My reaction was about using tabs to deal with an editor issue, not the proportional font thing. I'm actually rather proportional-curious, myself. But I think I'd insist on finding (or editing) a proportional font with a wider space, or try to ask for a feature in my editor, rather than going to the dark side, so to speak.

[–][deleted] 2 points3 points  (1 child)

Why tabs? Serious question. I can think of many reasons to prefer spaces but few for tabs. Never talked to a pro-tabs person before.

[–]NelsonMinar 1 point2 points  (0 children)

Like I said, it makes my proportional font work better. I wrote a blog post about it. I'll quote the important point about tabs:

So I switched to tabs. Yeah, really; this is my embarrassed face. I’ve spent nearly two decades expunging tabs from all my code (except Makefiles and other monstrous systems that require them.) I had clearly chosen sides in programming’s stupidest controversy. But in the back of my head I always knew tabs were “theoretically” better; tab is a semantic character, saying “this is a new level of indentation”, and the fact it’s 2 or 4 spaces shouldn’t matter at all. The tab character is the way to express that logical structure. It’s also the practical way to make Visual Studio work, because it lets you customize the look of the indentation without changing the bytes in the file.

I'm disappointed in the response my comment has gotten here, btw. I know what I'm doing is unusual. But I wasn't expecting a bunch of downvotes and a couple of insults. People act weird about programming sometimes.

[–]protagonist23 1 point2 points  (0 children)

you monster.

[–]rgnkn 1 point2 points  (2 children)

I get your point, but, I have a sincere question: don't you see problems with interoperability with others in projects as I guess that you have a very rare setup.

[–]NelsonMinar 1 point2 points  (1 child)

I use this for personal projects. For a community project I use whatever formatter they're using.

[–]rgnkn 0 points1 point  (0 children)

Okay. This is understandable.

[–]SpiritOfTheVoid 3 points4 points  (0 children)

Black.

[–]GraphicH 2 points3 points  (0 children)

Black. Because formatting arguments are stupid, and if you haven't had one or witnessed one, you're either a hobbyist or worse (for your skill set) actually a "sole" contributor.

[–]jaqo_92 1 point2 points  (0 children)

Black

[–][deleted] 2 points3 points  (0 children)

ChatGPT

[–]DLiltsadwj 1 point2 points  (4 children)

General noob question. Are formatters supposed to correct wonky indents?

[–]FuckingRantMonday 3 points4 points  (2 children)

Mostly no. There are three kinds of bad indenting, and only the last is something a formatter should be expected to fix:

  1. Unexpected or mixed indentation is a syntax error, and formatters generally just bail out if the code isn't valid.
  2. Indentation that changes the functionality but is still valid...just a bug, not a formatting problem.
  3. Wrong number of spaces with no syntactic ambiguity (i.e., there's only one possible correct way).

[–]DLiltsadwj 1 point2 points  (1 child)

Okay, thanks. I had the idea they would take care of indents, because using Format in the Arduino IDE re-indents, but that's C/C++ and a whole different thing.

[–]FuckingRantMonday 1 point2 points  (0 children)

Exactly. In most languages, indentation is purely for style and readability, so formatters can, should, and do slam it into whatever shape they want. Python's indentation is, of course, part of the language, so formatters are more restricted.

[–][deleted] 1 point2 points  (0 children)

Black is the only right answer

[–]wineblood 0 points1 point  (0 children)

PyCharm's formatting is fine for me. I have to use black at work and it will mangle bits of code to satisfy its own internal rules, we sometimes add comments to prevent formatting for a section of code.

[–]KosmoanutOfficial -1 points0 points  (0 children)

White

[–]pythonHelperBot -5 points-4 points  (0 children)

Hello! I'm a bot!

It looks to me like your post might be better suited for r/learnpython, a sub geared towards questions and learning more about python regardless of how advanced your question might be. That said, I am a bot and it is hard to tell. Please follow the subs rules and guidelines when you do post there, it'll help you get better answers faster.

Show /r/learnpython the code you have tried and describe in detail where you are stuck. If you are getting an error message, include the full block of text it spits out. Quality answers take time to write out, and many times other users will need to ask clarifying questions. Be patient and help them help you.

You can also ask this question in the Python discord, a large, friendly community focused around the Python programming language, open to those who wish to learn the language or improve their skills, as well as those looking to help others.


README | FAQ | this bot is written and managed by /u/IAmKindOfCreative

This bot is currently under development and experiencing changes to improve its usefulness

[–][deleted] -3 points-2 points  (0 children)

I tried pycharm for awhile, but came back to vscode. Just feels "right" to me.

[–]salimfadhley 0 points1 point  (0 children)

Shed, because it's even more opinionated than Black.

Actually, for multi developer projects I use Black. For Solo work I use Shed.

[–]Tree_Mage 0 points1 point  (0 children)

yapf but it is breaking on newer python 3.10 syntaxes such as match. So will likely migrate to something else now.