Comment by brewmarche
1 day ago
FWIW, ruff sees your line comment and keeps each item on its line. It only adds a trailing comma and additional space.
It also does not collapse lines when there’s a trailing comma.
Your output seems to be from black. IMO it’s insane to collapse lines when there are line comments.
$ uvx ruff format --diff formatting.py
--- formatting.py
+++ formatting.py
@@ -1,8 +1,4 @@
-no_comma = {
- "x": 3,
- "y": 42,
- "z": 2
-}
+no_comma = {"x": 3, "y": 42, "z": 2}
with_comma = {
"x": 3,
@@ -12,6 +8,6 @@
comment = {
"x": 3,
- "y": 42, # Answer to the Ultimate Question!
- "z": 2
+ "y": 42, # Answer to the Ultimate Question!
+ "z": 2,
}
1 file would be reformatted
(I intentionally switched to double quotes since that really is a stylistic choice in Python, you can escape in both, and if you use double quotes inside of single quotes ruff leaves it as-is)
(am not the GP)
I personally like double quotes, because in so many other languages they are for strings, while single quotes are often for other things. But somehow many people have a single quotes obsession in Python. I am guessing, that it is because of ease of typing them on a US keyboard layout.
> many people have a single quotes obsession
Not my problem. My problem is that I want to use them both, as I've been able to for many years.
I'll always use double to surround "can't" and single to surround '8.5" x 14"'. I'll use a context-appropriate pair when adding to existing code. But for all the other cases, I don't want to expend any energy on this. It's a complete non-issue.
> ease of typing them on a US keyboard layout
On both the US and European keyboards single-quotes are easier: no need to press Shift.
Regional keyboards are a mess for coding. I'm much happier with a qmk-enabled keyboard where I remap keys in hardware. For instance, on a French keyboard:
- easy: " ' , : * ! - _ = ( )
- harder (shift): numbers . µ ? / ℅ +
- hardest (Alt Gr): []{}€~|`^\@#
This is not exhaustive, but some of the most common symbols are quite hard to type: not surprising as most language designers use US keyboards. Still, I think everyone would benefit from a "symbols" layer on their keyboards.
At least on German keyboard layout double quotes are not a single key press, but a shift + something combination. Shift + 2, I believe. It's been a while since I used German layout. But I am aware, that other EU countries have other layouts, so there it could be a single key press, and I wouldn't know.