TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Django: Reformatted code with Black

284 点作者 tams超过 3 年前

26 条评论

samwillis超过 3 年前
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 未加载
bwhmather超过 3 年前
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 未加载
mrtranscendence超过 3 年前
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 未加载
VBprogrammer超过 3 年前
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 未加载
TheRealPomax超过 3 年前
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 未加载
declnz超过 3 年前
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 未加载
wyuenho超过 3 年前
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 未加载
tomp超过 3 年前
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 未加载
codingkev超过 3 年前
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 未加载
daenz超过 3 年前
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 未加载
glacials超过 3 年前
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 未加载
ibejoeb超过 3 年前
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 未加载
justinmchase超过 3 年前
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 未加载
VWWHFSfQ超过 3 年前
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 未加载
rowanseymour超过 3 年前
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.
NAHWheatCracker超过 3 年前
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 未加载
jnothing超过 3 年前
Why is it impossible to rebase? I didn’t understand the conversation around merging and rebasing
vitorfs超过 3 年前
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.
umvi超过 3 年前
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_angles超过 3 年前
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).
wolverine876超过 3 年前
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.
ReleaseCandidat超过 3 年前
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.
MahajanVardhan超过 3 年前
I am so sorry, but what is Black? I use django but I have never heard of Black
评论 #30264009 未加载
SoylentOrange超过 3 年前
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 未加载
phplovesong超过 3 年前
Good bye git history!
评论 #30267438 未加载
评论 #30267594 未加载
supreme_berry超过 3 年前
“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 未加载