-
-
Notifications
You must be signed in to change notification settings - Fork 31k
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
GH-121970: Use Ruff to check and format the docs tools #122018
Conversation
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'm generally in favour!
Co-authored-by: Alex Waygood <[email protected]>
Me too! Note there's some overlap with #121241 which @AlexWaygood wanted to post about on Discourse first. I'm also fine with not doing so. |
Is that the right issue/PR? I don't see a mention of Ruff on it. |
I'm specifically nervous about enabling more aggressive linting on stdlib modules in |
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 would personally go for quote-style = "preserve"
, and switch to quote-style = "double"
in a followup, rather than going for quote-style = "single"
(https://github.com/python/cpython/pull/122018/files#r1684185495). But I don't feel strongly. LGTM!
Thanks @AA-Turner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. |
Sorry, @AA-Turner, I could not cleanly backport this to
|
Sorry, @AA-Turner, I could not cleanly backport this to
|
Thanks @AA-Turner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12. |
Thanks @AA-Turner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
Sorry, @AA-Turner, I could not cleanly backport this to
|
…H-122018) (cherry picked from commit 40855f3) Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
GH-122023 is a backport of this pull request to the 3.13 branch. |
GH-122024 is a backport of this pull request to the 3.12 branch. |
…ythonGH-122018) (cherry picked from commit 40855f3) Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
) (#122024) (cherry picked from commit 40855f3) Co-authored-by: Alex Waygood <[email protected]>
) (#122023) GH-121970: Use Ruff to check and format the docs tools (GH-122018) (cherry picked from commit 40855f3) Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
No problem :) |
I got a lint failure on a PR:
As far as I know, the import block is OK according to PEP 8. When adding checks like this, could you also add some instructions to the devguide about how to deal with the warnings -- preferably without installing third-party tools I might not trust? |
@encukou, I'm sorry if we rushed this. Sorry that the error was inscrutable; thanks for flagging.
We already have documentation in the devguide noting that we have pre-commit checks set up, and giving some instructions about setting up pre-commit locally:
(I personally tend to avoid actually installing pre-commit hooks myself -- I usually run
Our CI is setup so that generally pre-commit will show you the changes it wants you to make to your PR as a diff when CI fails. In this case, it looks like that didn't happen for #121278. I'm not sure why that is, but anyway -- it's telling you that your imports aren't alphabetically sorted. You can find the documentation for the |
Thanks! I filed python/devguide#1360 to improve the set-up instructions. (If you don't do it yourself, is installing pre-commit as a commit hook the right thing to do?)
I don't understand why it is important to sort imports alphabetically. |
It's a thing that many people like to do. The advantage is that you don't have to run the linter manually; it automatically applies any autofixes as part of running I wasn't involved in the decision to add a section to the devguide telling people to setup commit hooks to run pre-commit.
It's not particularly important, but many people have decided that it's something they want to enforce as a matter of style. I personally like to enforce a consistent style of imports in this way in the projects I maintain, but I also wouldn't be distraught if we got rid of this rule.
PEP 8 is the style guide for the standard library, and we're not enforcing this rule on the standard library -- only internal tooling in the |
args: [--exit-non-zero-on-fix, --config=Tools/clinic/.ruff.toml] | ||
files: ^Tools/clinic/|Lib/test/test_clinic.py | ||
- id: ruff-format |
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.
@AA-Turner in pre-commit, it's usually best to put formatters before linters. This is because if there's a rule violation that linter shows, it will then show up as two different broken checks. It's also more important for the post-format version to be checked rather than showing errors on things that then get changed/relocated.
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.
Oh, are the pre-commit tools not idempotent? I hadn't realised that order mattered, as I thought we'd configured them to check only in a read-only fashion.
As you can probably tell, I don't use pre-commit personally!
A
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.
The Ruff pre-commit README says:
When running with
--fix
, Ruff's lint hook should be placed before Ruff's formatter hook, and before Black, isort, and other formatting tools, as Ruff's fix behavior can output code changes that require reformatting.When running without
--fix
, Ruff's formatter hook can be placed before or after Ruff's lint hook.
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.
@AA-Turner oh, I didn't realize. It's so popular that I tend to forget there's people who don't use it. I think I first contributed to pre-commit in 2016…
But yeah, there's science to crafting a config for the pre-commit tool. And there are a few principles I found very useful in this context. Here's how I sort the entries:
- fast checks first, followed by heavier linters (as in, flake8 goes before pylint, for example, by MyPy would probably be in between)
- formatters before linters (as in, autopep8 and then flake8)
- if there are groups of formatters+linters touching different file categories, the entire category can be moved together, no need to put autopep8 and docformatter together as they change different file types
pre-commit itself does not modify things by itself, so you can say it's idempotent in this sense. It's the checks that call other tools that modify files. I think if a check changes files (pre-commit docs sometimes call these “fixers”, not “formatters”), pre-commit will then show that entry as failed (although, it probably relies on the called program's return code). When something changed, it's useful to know what, so it's best to call the tool with --show-diff-on-failure
. I think the https://pre-commit.ci web service does this by default (this service is also able to push a commit with the changed files back to pull requests, by the way). Some people only use this CLI flag in CI, while others stick it into the command runner like tox so it's active locally when the contributors run linting.
Feel free to tag me for reviews in future PRs if you need any more insight on this topic.
Same here on both counts. Alphabetical import sorting can be more useful, than for example, new imports being tacked onto the end:
|
But as a contributor to various projects, each of which expects or enforces a slightly different code style (and most of which expect me to format the code), I get the opposite of consistency. If alphabetical import sorting is useful, I do think it should be preferred for new code across the entire project. Then, it would be easier to accept that in some parts of the code we enforce some parts of the style guide. It worries me to see a maintainer that has things set up just right, slightly differently than what's in the docs, while others get an inscrutable error for what should be a simple contribution. Should we be worried about non-dogfooded devguide instructions? |
pyspecific
#121970📚 Documentation preview 📚: https://cpython-previews--122018.org.readthedocs.build/