Skip to content
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

Install black as a pre-commit hook #2442

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open

Install black as a pre-commit hook #2442

wants to merge 2 commits into from

Conversation

tienne-B
Copy link
Member

This is a huge PR, as it's re-formatting a lot of the code. However only the 1st commit implements black. An automatic code formatter is more user-friendly than a linter in that most problems get automatically fixed, so there'd be less friction as to "why can't I commit."

Most of the reformatting is to prefer quotation marks over apostrophes for strings, and expanding lines. These commits can be excluded when bisecting/blaming past this PR, as explained in Black's docs.

I would want to move towards Ruff to replace both Black and Flake8 if it could have the fixes for long lines.

tienne-B added 2 commits June 16, 2024 13:24
This commit adds an auto-formatter for Python: `black`, and adapts
flake8 to avoid conflicts.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migrations are auto-generated so there's not much point enforcing an linter on them imo, I'd exclude migration files from it (as we currently do in the flake8 config file).

@@ -0,0 +1,4 @@
[tool.black]
line-length = 100
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use 120? I feel that 100 is unduly conservative.

Comment on lines -117 to +149
10: [1, 10, 55, 220, 705, 1902, 4455, 9240, 17205, 29050, 44803, 63460, 82885, 100110, 112035, 116304, 112035, 100110, 82885, 63460, 44803, 29050, 17205, 9240, 4455, 1902, 705, 220, 55, 10, 1], # noqa: E501
10: [
1,
10,
55,
220,
705,
1902,
4455,
9240,
17205,
29050,
44803,
63460,
82885,
100110,
112035,
116304,
112035,
100110,
82885,
63460,
44803,
29050,
17205,
9240,
4455,
1902,
705,
220,
55,
10,
1,
], # noqa: E501
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes like this seem like clearly a bad idea to me. This had a # noqa ignore command for flake8, how do we do the same with black?

Comment on lines -153 to +188
9: (16, 11, [16, 16, 16, 15, 15, 15, 15, 16, 15, 16, 15, 13, 13, 14, 14, 12, 14, 13, 13, 12, 13, 12, 12, 12, 12, 14, 12, 12, 11, 13, 12, 12, 13, 11, 12, 11, 11, 11, 11, 11, 13, 11, 13, 11, 12, 12, 12, 13, 12, 13, 12, 10, 13, 11, 10, 12, 10, 11, 12, 13, 10, 10, 10, 11, 12, 12, 11, 12, 9, 11, 11, 10, 9, 12, 10, 12, 9, 10, 10, 9, 11, 11, 9, 10, 11, 10, 10, 10, 9, 12, 10, 11, 10, 10, 11, 11, 11, 10, 10, 11, 8, 8, 9, 11, 8, 10, 10, 8, 9, 8, 9, 9, 9, 10, 9, 8, 10, 10, 7, 9, 10, 8, 10, 10, 9, 10, 10, 8, 10, 7, 7, 9, 8, 9, 9, 9, 9, 8, 8, 9, 8, 7, 8, 7, 9, 9, 6, 7, 6, 8, 9, 6, 6, 7, 5, 8, 7, 7, 6, 7, 6, 7, 6, 6, 0, 0]), # noqa: E501
8: (16, 8, [13, 15, 14, 13, 13, 13, 15, 14, 12, 13, 13, 10, 10, 14, 14, 11, 13, 11, 13, 11, 12, 11, 11, 10, 12, 12, 12, 11, 11, 11, 11, 10, 10, 9, 11, 9, 8, 8, 10, 10, 12, 8, 11, 8, 10, 11, 10, 10, 12, 13, 12, 10, 12, 9, 7, 9, 10, 10, 9, 10, 9, 8, 9, 10, 11, 10, 11, 12, 8, 10, 9, 10, 9, 10, 10, 9, 9, 8, 9, 9, 11, 9, 9, 8, 11, 9, 10, 8, 8, 9, 7, 8, 10, 10, 9, 10, 10, 10, 7, 9, 7, 8, 7, 10, 7, 9, 9, 8, 6, 8, 8, 6, 7, 10, 7, 8, 9, 7, 7, 9, 10, 6, 9, 7, 7, 8, 7, 8, 8, 6, 5, 8, 8, 7, 9, 9, 8, 8, 7, 9, 6, 6, 7, 7, 6, 8, 3, 7, 6, 5, 6, 6, 6, 7, 5, 7, 6, 6, 4, 4, 6, 5, 5, 5, 0, 0]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 1, 0, 0, 0]), # noqa: E501
9: (
16,
11,
[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or this, even worse 🙈

Comment on lines -43 to +61
return ({
'list': getattr(view, 'list_permission', False),
'create': getattr(view, 'create_permission', False),
'retrieve': getattr(view, 'list_permission', False),
'update': getattr(view, 'update_permission', False),
'partial_update': getattr(view, 'update_permission', False),
'destroy': getattr(view, 'destroy_permission', False),
'delete_all': getattr(view, 'destroy_permission', False),
'add_blank': getattr(view, 'create_permission', False),
'GET': getattr(view, 'list_permission', False),
'POST': getattr(view, 'update_permission', False),
'PUT': getattr(view, 'update_permission', False),
'PATCH': getattr(view, 'update_permission', False),
'DELETE': getattr(view, 'destroy_permission', False),
}).get(getattr(view, 'action', view.request.method), False)
return (
{
"list": getattr(view, "list_permission", False),
"create": getattr(view, "create_permission", False),
"retrieve": getattr(view, "list_permission", False),
"update": getattr(view, "update_permission", False),
"partial_update": getattr(view, "update_permission", False),
"destroy": getattr(view, "destroy_permission", False),
"delete_all": getattr(view, "destroy_permission", False),
"add_blank": getattr(view, "create_permission", False),
"GET": getattr(view, "list_permission", False),
"POST": getattr(view, "update_permission", False),
"PUT": getattr(view, "update_permission", False),
"PATCH": getattr(view, "update_permission", False),
"DELETE": getattr(view, "destroy_permission", False),
}
).get(getattr(view, "action", view.request.method), False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to configure it to permit two opening brackets on the same line, where the same brackets are adjacent on both sides?

@czlee
Copy link
Member

czlee commented Jun 17, 2024

Most of the reformatting is to prefer quotation marks over apostrophes for strings, and expanding lines.

Previously we were using double quotation marks for human-readable strings, and single quotation marks for machine-readable strings. (Of course, a linter cannot enforce this, so just has to disable the check.) I have a slight preference for this but if both of you feel strongly about switching to double quotation marks that's fine.

Expanding lines concerns me though. I've highlighted a particularly egregious example above, but I find oftentimes exploding nested brackets into many lines makes code less readable, not more readable. (Not always—in many of the changes here so far, there's not much difference.) There's probably some way to configure it to be more reasonable, though.

@teymour-aldridge
Copy link
Contributor

teymour-aldridge commented Sep 29, 2024

Expanding lines concerns me though. I've highlighted a particularly egregious example above, but I find oftentimes exploding nested brackets into many lines makes code less readable, not more readable. (Not always—in many of the changes here so far, there's not much difference.) There's probably some way to configure it to be more reasonable, though.

I did a quick web search and I believe Black doesn't support this (psf/black#1281). I agree that it's not an improvement to have these long arrays wrapped on multiple lines; I think the easiest way to solve this is to use the #fmt: on and #fmt: off directives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants