TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Django: Reformatted code with Black

284 pointsby tamsover 3 years ago

26 comments

samwillisover 3 years ago
I believe from memory Django decided to move to using Black back in 2019 [0] but delayed the change until Black exited Beta. Black became none beta at the end of January [1].<p>This was finally merged to the main branch today [2].<p>I suspect there are lots of other both open source and private projects that are also making the change now. This is a show of confidence in Black as the standard code formatter for Python.<p>0: <a href="https:&#x2F;&#x2F;github.com&#x2F;django&#x2F;deps&#x2F;blob&#x2F;main&#x2F;accepted&#x2F;0008-black.rst" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;django&#x2F;deps&#x2F;blob&#x2F;main&#x2F;accepted&#x2F;0008-black...</a><p>1: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30130316" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30130316</a><p>2: <a href="https:&#x2F;&#x2F;github.com&#x2F;django&#x2F;django&#x2F;pull&#x2F;15387" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;django&#x2F;django&#x2F;pull&#x2F;15387</a>
评论 #30261862 未加载
bwhmatherover 3 years ago
Shameless plug: For people who like black, I&#x27;ve been working on ssort[0], a python source code sorter that will organize python statements into topological order based on their dependencies. It aims to resolve a similar source of bikeshedding and back and forth commits.<p>0: <a href="https:&#x2F;&#x2F;github.com&#x2F;bwhmather&#x2F;ssort" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bwhmather&#x2F;ssort</a>
评论 #30262501 未加载
评论 #30261385 未加载
评论 #30262165 未加载
评论 #30261591 未加载
评论 #30261017 未加载
评论 #30262305 未加载
评论 #30261222 未加载
评论 #30261365 未加载
评论 #30261676 未加载
评论 #30271600 未加载
评论 #30264356 未加载
评论 #30261333 未加载
mrtranscendenceover 3 years ago
I&#x27;ve been using black at work for over a year now. I don&#x27;t much care for some of the choices it makes, which can sometimes be quite ugly, but I&#x27;ve grown used to it and can (nearly) always anticipate how it will format code. One nice side effect of encouraging its use is how, at least where I work, it was <i>very</i> common to use the line continuation operator \ instead of encompassing an expression in parentheses. I always hated that and black does away with it.<p>What I don&#x27;t much care for is reorder-python-imports, which I think is related to black (but don&#x27;t quote me). For the sake of reducing merge conflicts it turns the innocuous<p>from typing import overload, List, Dict, Tuple, Option, Any<p>into<p>from typing import overload<p>from typing import List<p>from typing import Tuple<p>from typing import Option<p>from typing import Any<p>Ugh. Gross. Maybe I&#x27;m just lucky but I&#x27;ve never had a merge conflict due to an import line so the cure seems worse than the disease.<p>Edit: Just to be 100% clear: this is python-reorder-imports, not black. I thought they were related projects, though maybe I&#x27;m wrong. Regardless, black on its own won&#x27;t reorder imports.
评论 #30260381 未加载
评论 #30259545 未加载
评论 #30259538 未加载
评论 #30259540 未加载
评论 #30261699 未加载
评论 #30263099 未加载
VBprogrammerover 3 years ago
Reading some of the comments here it&#x27;s become clear to me that the next stage in the development of auto-formatters is to have the formatter commit the code as a canonical format but to display the code to each individual contributor in the style of their choosing. Thus removing all kinds of arguments about whether 80 or 120 columns is the one true width.
评论 #30263396 未加载
评论 #30263555 未加载
评论 #30264663 未加载
评论 #30269628 未加载
评论 #30262945 未加载
评论 #30263205 未加载
评论 #30263273 未加载
TheRealPomaxover 3 years ago
The reason to use Black is the same as Prettier on the HTML&#x2F;CSS&#x2F;JS side: forever stop having an opinion on code style, it&#x27;s wasted time and effort. Any &quot;it&#x27;s not exactly what we want&quot; comment with an attempt to customize the style to be closer to &quot;what we were already using&quot; is exactly why these things exist: by all means have that opinion, but that&#x27;s exactly the kind of opinion you shouldn&#x27;t ever even need to have, tooling should style the code universally consistently &quot;good enough&quot;. Which quotes to use, what indent to use, when to split args over multiple lines, it&#x27;s all time wasted. Even if you worked on a project for 15 years, once you finally add autoformatting, buy in to it. It&#x27;s going to give you a new code style, and you will never even actively have to follow it. You just need to be able to read it. Auto-formatting will do the rest.
评论 #30268095 未加载
declnzover 3 years ago
Aside: I love a good linter, but as a long-time Python fan I find it sad that Black has <i>so</i> little configuration (yes, I know, but still) and moreover that it often produces code that <i>no</i> human Python dev I know would write...<p>Python was always meant to look concise &#x2F; beautiful... (MyPy has also made this trickier too)
评论 #30259757 未加载
评论 #30262033 未加载
评论 #30261388 未加载
评论 #30259311 未加载
wyuenhoover 3 years ago
Every time I was tempted to do something like this, I hesitated because I didn&#x27;t want every other line in every file with my name on a single commit, mostly to avoid making git blame harder than necessary. It would be nice if there was a kind of diffing algorithm that can diff code units *syntactically* across history.
评论 #30259634 未加载
评论 #30261320 未加载
评论 #30267166 未加载
评论 #30263648 未加载
评论 #30260921 未加载
评论 #30260446 未加载
tompover 3 years ago
worst things about Black:<p>- doesn&#x27;t respect vertical space - sure, making the code fit on screen might be valuable (though the default width should be at least 120 characters, I mean we&#x27;re in 2022 after all), but Black does it by blowing up the <i>vertical</i> space used by the code<p>- spurious changes in commits - if you happen to indent a block, Black will cause lines to break<p>- Black fails at its most basic premise - &quot;avoiding manual code formatting&quot; - because a trailing comma causes a list&#x2F;function call to be split over lines <i>regardless</i> of width
评论 #30260242 未加载
评论 #30262080 未加载
评论 #30259781 未加载
评论 #30259616 未加载
评论 #30262258 未加载
评论 #30260348 未加载
codingkevover 3 years ago
A little shoutout to a alternative Python formating tool <a href="https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;yapf" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;yapf</a> (developed by Google).<p>The built in &quot;facebook style&quot; formating felt by far the most natural to me with the out of the box settings and no extra config.
评论 #30261181 未加载
评论 #30263235 未加载
评论 #30261301 未加载
daenzover 3 years ago
I&#x27;m so happy that languages are settling more and more on heavy reformatter usage. I&#x27;d like to think it was triggered by Go and gofmt. Working on a team where each engineer has their own personal syntax is not fun.
评论 #30259455 未加载
评论 #30259947 未加载
评论 #30260119 未加载
glacialsover 3 years ago
Black is slowly creeping into gofmt-level universality in the Python community and it’s great. The next big milestone is a first-party recommendation by python.org itself.
评论 #30259446 未加载
评论 #30267366 未加载
ibejoebover 3 years ago
In general, what are the strategies for large public codebases like this to mitigate supply chain attacks or other source-level attacks?<p>For clarity, I&#x27;m hoping to open us discussion about how we&#x27;re dealing with massive changesets like this that are difficult to review due chiefly to the breadth of it.
评论 #30259640 未加载
评论 #30260441 未加载
评论 #30261999 未加载
justinmchaseover 3 years ago
The output does look better but this also just looks like every PR for applying a linter &#x2F; formatter I&#x27;ve ever seen. Not sure why this is news worthy.
评论 #30259610 未加载
评论 #30259104 未加载
评论 #30259056 未加载
评论 #30260538 未加载
VWWHFSfQover 3 years ago
So now when you look at the annotated change history all you&#x27;re going to see is a bunch of changes by the person that reformatted the code instead of the person that wrote it.
评论 #30259053 未加载
评论 #30259300 未加载
评论 #30259369 未加载
评论 #30259046 未加载
评论 #30259059 未加载
评论 #30259037 未加载
rowanseymourover 3 years ago
I love this except the use of the default black line length of 88. One of the things I appreciate about gofmt is being trusted with deciding on line breaks.
NAHWheatCrackerover 3 years ago
I suggested Black to a team I was on a year ago and one developer hemmed and hawed about how he likes to format arrays or something. I didn&#x27;t win any friends by pointing out that disregarding those personal preferences is part of why I was recommending it.<p>A year later and it seems to be the default on all projects I&#x27;m working on and I&#x27;m loving it.
评论 #30281432 未加载
jnothingover 3 years ago
Why is it impossible to rebase? I didn’t understand the conversation around merging and rebasing
vitorfsover 3 years ago
This is such a great news. We&#x27;ve been using Black in the company that I work for the past 3 years or so and it was a game changer for code reviews. Hopefully other open source Python&#x2F;Django projects will follow the lead.
umviover 3 years ago
What&#x27;s the point of putting linters into CI? Is the point to fail the build if the code wasn&#x27;t pre-formatted with i.e. Black? Or is the point to autoformat and autocommit the formatted code?
评论 #30260705 未加载
评论 #30260682 未加载
评论 #30260695 未加载
euler_anglesover 3 years ago
Had a great experience with black. Only thing I did was change its default line length limit to 120 characters (I was regularly dealing with signal names from source data that were about 90 chars).
wolverine876over 3 years ago
Do Black and other autoformatters enable significantly more reusable code and computer-generated code? Formatting is certainly not the only or greatest barrier, but if format is standardized across projects, it&#x27;s easier to plug and play code from outside.
ReleaseCandidatover 3 years ago
I would really appreciate if there would exist exactly _one_ formatter (without any options) per language.<p>It is way better to deal with ugly formatting as long as it is consistent than with discussions where to put a closing brace&#x2F;bracket&#x2F;paren.
MahajanVardhanover 3 years ago
I am so sorry, but what is Black? I use django but I have never heard of Black
评论 #30264009 未加载
SoylentOrangeover 3 years ago
I’ve been using black for about a year and I’m generally a big fan. However my biggest gripe with it is bad VS Code integration.
评论 #30259719 未加载
phplovesongover 3 years ago
Good bye git history!
评论 #30267438 未加载
评论 #30267594 未加载
supreme_berryover 3 years ago
“Black” developer refused for a long time to add option to format code with single quotes with very aggressive manners. Now Django devs didn’t see that option for single quotes and code looks unpleasant.
评论 #30259469 未加载
评论 #30260059 未加载
评论 #30264464 未加载
评论 #30262407 未加载
评论 #30260997 未加载