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

Ruff formatter [v2]: remove flake8/black/isort/bandit and format to ruff defaults #2844

Closed
wants to merge 38 commits into from

Conversation

Tronic
Copy link
Member

@Tronic Tronic commented Oct 25, 2023

This is a more radical fork of my other Ruff PR #2843 which already had a fairly large diff. This one strips away the old linters and doesn't attempt to follow their style anymore, so the diff keeps growing...

Both PRs are available for review and discussion. Have a look and state which one if either should be merged.

@Tronic
Copy link
Member Author

Tronic commented Oct 25, 2023

Note: Type annotations using the Python 3.9/3.10 new syntax should work with 3.8 too, thanks to the future imports that Ruff added to each module.

@Tronic Tronic marked this pull request as ready for review October 25, 2023 03:43
@Tronic Tronic requested review from a team as code owners October 25, 2023 03:43
@codecov
Copy link

codecov bot commented Oct 25, 2023

Codecov Report

Attention: 20 lines in your changes are missing coverage. Please review.

Comparison is base (a5a9658) 88.387% compared to head (a9cbff6) 88.481%.

Additional details and impacted files
@@              Coverage Diff              @@
##              main     #2844       +/-   ##
=============================================
+ Coverage   88.387%   88.481%   +0.094%     
=============================================
  Files           92        92               
  Lines         7156      7145       -11     
  Branches      1228      1223        -5     
=============================================
- Hits          6325      6322        -3     
+ Misses         575       571        -4     
+ Partials       256       252        -4     
Files Coverage Δ
sanic/__init__.py 100.000% <ø> (ø)
sanic/application/ext.py 100.000% <ø> (ø)
sanic/application/logo.py 100.000% <100.000%> (ø)
sanic/application/motd.py 97.849% <100.000%> (ø)
sanic/application/spinner.py 100.000% <ø> (ø)
sanic/asgi.py 91.791% <100.000%> (ø)
sanic/base/root.py 93.548% <100.000%> (ø)
sanic/blueprint_group.py 100.000% <ø> (ø)
sanic/blueprints.py 89.067% <100.000%> (ø)
sanic/config.py 98.675% <100.000%> (+1.256%) ⬆️
... and 70 more

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Tronic Tronic changed the title Ruff formatter [v2]: remove flake8/black/isort and format to ruff defaults Ruff formatter [v2]: remove flake8/black/isort/bandit and format to ruff defaults Oct 26, 2023
Copy link
Member

@ahopkins ahopkins left a comment

Choose a reason for hiding this comment

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

I do not see any benefit or reason to be switching our tooling in this regard right now. Let's continue the conversation on Discord, but I am against this change right now.

@Tronic
Copy link
Member Author

Tronic commented Oct 29, 2023

I do not see any benefit or reason to be switching our tooling in this regard right now. Let's continue the conversation on Discord, but I am against this change right now.

1500 lines less code, far improved formatting of things that were split in unreadable ways. All typing upgraded to modern format. Four separate tools costing expensive CI runs replaced with one that runs much faster (with setting up that one CI job still takes about 40 seconds). Including examples, guide and tests in CI too.

I believe these are benefits enough to justify the changes of this PR [v2]. But I have also provided the other PR #2843 in case such a large rewrite of code is not wanted just yet. Ruff notably can keep almost all Black formatting as it were, as demonstrated in that PR.

Notably prominent projects like FastAPI and Pydantic immediately switched their tooling to Ruff with this new release that supports full formatting.

@Tronic
Copy link
Member Author

Tronic commented Oct 29, 2023

Ruff targets all Sanic's supported Python versions in one run, if we add to pyproject.toml:

[project]
requires-python = ">=3.8"

Thus it also doesn't need separate CI runs on each version, as was previously done with bandit (security) presumably because that tool only checks for the Python version it runs on.

@sjsadowski sjsadowski self-requested a review October 29, 2023 14:19
Copy link
Contributor

@sjsadowski sjsadowski left a comment

Choose a reason for hiding this comment

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

95%+ of the changes are related to formatting and syntax updates. I am perfectly fine with updating Union[] and Optional[] to their pipe syntax as I think it is cleaner and easier to read. I'm also ok with replacing dict and set explicits with implicits. I also am fine with the default line length being moved to 88 chars, though I think that is a project level decision that the entire core dev team should weigh in on. I'm sure there will be opinions.

This has my tentative approval and once the line length issue has been settled, I'll add explicit approval if the 88 char line length is accepted.

examples/run_async_advanced.py Show resolved Hide resolved
examples/http_redirect.py Show resolved Hide resolved
examples/try_everything.py Show resolved Hide resolved
@@ -10,7 +9,6 @@
import os
import sys


Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like ruff moves from 2 line breaks between imports and code to one. I don't know if I actually like that change because it provides a better visual break, though with any modern editor, code highlighting generally makes this fine. If nobody else finds the 2lb useful, I'm fine with moving forward with just the one.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sanic was somewhat 50%/50% one or two lines after imports. The guide and other parts not included in automatic formatting had one line while the main codebase had two. I opted for Ruff default, which is one line, to unify everything and to avoid extra space that might not be wanted in guide.

We can provide different configuration for different parts of Sanic code if so desired. I personally don't care about the specific number of empty lines there (or the various other positions where two lines might be inserted).

guide/webapp/display/layouts/elements/navbar.py Outdated Show resolved Hide resolved
sanic/errorpages.py Show resolved Hide resolved
@@ -14,7 +14,7 @@ def _apply_exception_handler(self, handler: FutureException):
def exception(
self,
*exceptions: Union[Type[Exception], List[Type[Exception]]],
apply: bool = True
apply: bool = True,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpicking that I don't think a trailing comma belongs before the closing parenthesis.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is a fairly common to put comma even after the last item in expanded (multi line) format, so that adding more entries does not require adding a comma on the previous line. Black apparently also does this most of the time... And there appears to be no configuration for it.

This is one of the things that Ruff apparently intentionally deviates from Black in some yet not clearly specified cases. https://docs.astral.sh/ruff/formatter/black/#trailing-commas-are-inserted-when-expanding-a-function-definition-with-a-single-argument

sanic/mixins/startup.py Outdated Show resolved Hide resolved
tests/test_graceful_shutdown.py Show resolved Hide resolved
or platform.system() != "Linux",
reason="Not testable with current client",
)
def test_keep_alive_client_timeout():
Copy link
Contributor

Choose a reason for hiding this comment

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

We're axing this test completely. Is it just because we don't find the test useful or is there some other background?

Copy link
Member Author

Choose a reason for hiding this comment

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

The test was hanging for a long time. It tests a timeout of httpx, which is essentially a disconnection on Sanic's side, and is not particularly meaningful to test.

…terwards because ruff format warns of possible problems with it.
@ahopkins
Copy link
Member

ahopkins commented Nov 6, 2023

95%+ of the changes are related to formatting and syntax updates. I am perfectly fine with updating Union[] and Optional[] to their pipe syntax as I think it is cleaner and easier to read.

This is a Python 3.10 feature.

@Tronic
Copy link
Member Author

Tronic commented Nov 6, 2023

95%+ of the changes are related to formatting and syntax updates. I am perfectly fine with updating Union[] and Optional[] to their pipe syntax as I think it is cleaner and easier to read.

This is a Python 3.10 feature.

Supported fine on Python 3.8 and 3.9 as well by from __future__ import annotations.

@ahopkins ahopkins mentioned this pull request Dec 4, 2023
@ahopkins
Copy link
Member

ahopkins commented Dec 5, 2023

Closing this for now, but let's come back and revisit some of this after LTS.

@ahopkins ahopkins closed this Dec 5, 2023
@ahopkins ahopkins deleted the ruff-only branch December 5, 2023 09:56
@Tronic Tronic restored the ruff-only branch December 6, 2023 17:34
@Tronic
Copy link
Member Author

Tronic commented Dec 6, 2023

Keeping the branch to revisit this after LTS... It includes plenty of manual modification for tests and typing.

@Tronic
Copy link
Member Author

Tronic commented Mar 30, 2024

I won't have time to get back to this. A lot of changes have piled up after this branch, so deleting it anyway.

@Tronic Tronic deleted the ruff-only branch March 30, 2024 00:31
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.

None yet

3 participants