New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use black for code formatting #6264
Conversation
Codecov Report
@@ Coverage Diff @@
## master #6264 +/- ##
==========================================
- Coverage 82.71% 82.68% -0.03%
==========================================
Files 39 39
Lines 10525 10525
Branches 2076 2076
==========================================
- Hits 8706 8703 -3
- Misses 1312 1314 +2
- Partials 507 508 +1
Continue to review full report at Codecov.
|
This causes CodeQL to fail: print(
'Your passphrase (between double-quotes): "%s"' % passphrase,
file=sys.stderr,
) error: Clear-text logging of sensitive information Not a bug: a deliberate feature. |
I like a lot of the changes black does, but (sadly) can't agree with it completely. So, guess no black for now. IIRC i have already checked that line (false positive) due to codeql feedback, but likely it popped up now because the line was reformatted by black. |
Could you please provide more details? There is a chance black can be configured to suit your tastes. If not, perhaps we could try a more configurable formatter. It would be useful to know more about your stylistic preferences when "shopping" for a it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commented some black changes.
my main issue is black using too much vspace (and too little hspace).
our screens usually have more width than height (like 16:9 or 16:10), but black is transforming a lot of hspace usage into vspace usage.
for the same reason, we do not use the "80 chars wide" pep8 recommendation, but more.
it is important that a reasonable amount of code fits into the screen / into the editor window or one ends up with having to scroll way too often.
Thank you |
Good news: line length is configurable in black. The default is 88. Iet's see what --line-length=100 looks like. |
015cf83
to
236d2d9
Compare
--line-length=120 seams to address most of your vspace concerns. |
https://github.com/borgbackup/borg/blob/master/setup.cfg#L91 yeah, 120 is what we have there also. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
much better, some dislikes remaining.
but we get closer to the point where i'ld say automated blackening has more advantages than disadvantages.
we can't merge this now though, this must wait until after 1.2.0 (not sure whether it should be limited to master then and not be done in 1.2-maint. while that would be more conservative, it would make backports harder, guess we do not want that).
in any case, the first run of black needs to be a totally separate PR with no manual changes.
src/borg/constants.py
Outdated
[ | ||
"path", | ||
"source", | ||
"rdev", | ||
"chunks", | ||
"chunks_healthy", | ||
"hardlink_master", | ||
"mode", | ||
"user", | ||
"group", | ||
"uid", | ||
"gid", | ||
"mtime", | ||
"atime", | ||
"ctime", | ||
"birthtime", | ||
"size", | ||
"xattrs", | ||
"bsdflags", | ||
"acl_nfs4", | ||
"acl_access", | ||
"acl_default", | ||
"acl_extended", | ||
"part", | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strange. a lot of vspace usage is gone, but here another extreme one is still present.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not strange: black either puts each item on its own line or puts them all on the same line. This is too long to fit in 120 characters so black puts each item on a separate line.
black respects # fmt: off/on
to suppress formatting in special situations. https://stackoverflow.com/a/58584557
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, that's nice to deal with a few cases.
to check:
|
psf/black#359 is incomplete semantic changes are extremely unlikely |
Ok. I'll get back to this when 1.2.0 comes out. |
src/borg/constants.py
Outdated
REQUIRED_ITEM_KEYS = frozenset( | ||
[ | ||
"path", | ||
"mtime", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a trailing comma after mtime. This would have stayed on one line:
REQUIRED_ITEM_KEYS = frozenset(['path', 'mtime'])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like trailing commas, they make future changes more "regular".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears that trailing commas force black to split items across multiple lines:
REQUIRED_ITEM_KEYS = frozenset(['path', 'mtime',])
REQUIRED_ITEM_KEYS = frozenset(['path', 'mtime'])
becomes:
REQUIRED_ITEM_KEYS = frozenset(
[
"path",
"mtime",
]
)
REQUIRED_ITEM_KEYS = frozenset(["path", "mtime"])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope. This is a feature, but one that can be disabled. See psf/black#1824
pipx run black .
produces:
diff --git a/example.py b/example.py
index 6e257e0..ed2a7da 100644
--- a/example.py
+++ b/example.py
@@ -1,2 +1,7 @@
-REQUIRED_ITEM_KEYS = frozenset(["path", "mtime",])
+REQUIRED_ITEM_KEYS = frozenset(
+ [
+ "path",
+ "mtime",
+ ]
+)
REQUIRED_ITEM_KEYS = frozenset(["path", "mtime"])
but:
pipx run black --skip-magic-trailing-comma .
produces:
diff --git a/example.py b/example.py
index 6e257e0..3a4ce71 100644
--- a/example.py
+++ b/example.py
@@ -1,2 +1,2 @@
-REQUIRED_ITEM_KEYS = frozenset(["path", "mtime",])
+REQUIRED_ITEM_KEYS = frozenset(["path", "mtime"])
REQUIRED_ITEM_KEYS = frozenset(["path", "mtime"])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and what if we want to just keep the trailing comma and not have it split into one line per element?
it looks like the idea of "always do the same change" when adding or removing an element is not present in black:
[] # start with an empty list
[first, ] # add first element
[first, second, ] # add another element - same procedure as first element.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw, for tuples, this also solves the non-tupleness of (first)
, because then it is (first, )
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am sorry, but black does not have an option to keep a trailing comma when putting the output on a single-line. Is this a deal-breaker? We could theoretically ask for such an option to be added, but I am not sure it'll happen given that the default is magic trailing comma - a trailing comma indicates that the output should be split across multiple lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i actually like the fact that the comma is used as indicator on how one wants to be diff friendly/lined up
with the tailing comma black prepares multi-line, without the comma black prepares single line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really a deal-breaker, just one item on the disadvantages list.
If something like this is done (personally not a fan) I'd suggest adding a blame-ignore-revs file and a note to the developer's docs to set that up after cloning so that |
https://github.com/ipython/ipython/pull/12091/files see there for an example of what @enkore meant. |
@hexagonrecursion could you please:
|
This blocks borgbackup#6264 because black configuration is stored in pyproject.toml. See <https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#configuration-format> I copied this from <https://setuptools.pypa.io/en/latest/userguide/quickstart.html#basic-use>
Saving a link here for my future self: Avoiding ruining git blame |
I'd suggest to pin Black to a 22.x version because the FAQ of Black points out ("How stable is Black’s style?") that the output isn't stable across major (yearly) versions, |
@enkore good idea. otoh, i would suspect that changes would be rather minor maybe? |
could be rebased in current master now and soon be applied to master. we'll need to keep master and 1.2-maint separated already, so IF we apply to 1.2-maint also, we would hope that the result of the transformation is about the same as in master. |
|
security: do not trust me! It is easy to bury a backdoor in a diff this big. Run black yourself and compare. Even if you trust me, there is always a chance that my machine is compromised. Why risk? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for your note about security, you're very careful.
@ThomasWaldmann I am confused. GitHub tells me that you requested changes, but in your most recent review (Mar 2) you only commented about GitHub not respecting .git-blame-ignore-revs. Do you want me to change something? |
@hexagonrecursion No, nothing to do here for you. Next step is me finishing this. |
Rebased on current master |
.git-blame-ignore-revs is now supported by github https://docs.github.com/en/repositories/working-with-files/using-files/viewing-a-file#ignore-commits-in-the-blame-view |
Superseded by #6842. |
https://github.com/psf/black is an The uncompromising Python code formatter.
Examples of things black does that I know we want:
This commit only applies the formatting to demo it.
If you like what you see, I'll add back to tox.ini and to CI.