-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Migrate from autoflake
, black
, isort
, pyupgrade
, flake8
and pydocstyle
, to ruff
#11901
Migrate from autoflake
, black
, isort
, pyupgrade
, flake8
and pydocstyle
, to ruff
#11901
Conversation
Taken from pytest-dev#11885 that was closed.
Thanks @Pierre-Sassoulas! I don't mind the churn, however I would prefer to see ruff replacing the other hooks in the same PR (specially black and autoflake, as well as whitespace, eol, etc), to avoid having multiples rounds of PRs which change a large portion of the code each. Before merging, we can group all the automatic changes into a single commit and register that commit in https://github.com/pytest-dev/pytest/blob/main/.git-blame-ignore-revs. |
autoflake
, black
, isort
, some of pre-commit
checks, pyupgrade
, and flake8
to ruff
I'm not sure it's possible to remove pre-commit's eol / whitespace checks because they act on all files when ruff fmt or black will only act on python files. Also @nicoddemus do we remove black for sure ? There's also blacken-doc it could maybe be replace by something like astral-sh/ruff#8854 option... But I'm not sure it's equivalent because docstring != python code area in rst/md. Thus it would introduce two kind of formatting black for docstring and code example, and ruff for the code itself. |
The isort looks good to me!
From https://docs.astral.sh/ruff/formatter/#docstring-formatting it's also supposed to handle rst and md. |
791543b
to
f300f7c
Compare
.pre-commit-config.yaml
Outdated
rev: "v0.1.15" | ||
hooks: | ||
- id: ruff | ||
args: ["--fix"] | ||
- repo: https://github.com/PyCQA/flake8 |
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 think we can remove flake8
too. 👍
That's awesome. Did this work? I didn't notice any docs changing (which might mean it worked or did not even run). |
I rebased and squashed the isort commits together since they are reviewed now. Migrated autoflake / flake8 core and pyupgrade. Still missing black blacken-doc and flake8-typing-imports atm |
Is flake8-typing-imports needed? ruff has support for flake8-type-checking https://github.com/astral-sh/ruff/pull/2147/files TYP002 through TYP005 isn't needed on projects only supporting python 3.7+ |
Apparently it's not supported yet : astral-sh/ruff#2302 (but there's only one check that is still useful) |
src/_pytest/_io/pprint.py
Outdated
@@ -376,7 +376,7 @@ def _pprint_simplenamespace( | |||
context: Set[int], | |||
level: int, | |||
) -> None: | |||
if type(object) is _types.SimpleNamespace: | |||
if isinstance(object, _types.SimpleNamespace): |
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.
This should stay as is if I understand the comment below correctly.
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.
Thank you for the reviews ! 287c651
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 thinking let's disable this rule entirely, generally it's a good rule but pytest can do weird low-level things and we usually know what we're doing when we use type(..) is ...
.
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.
All right, I'll remove the manual fix for type / is instance and rebase everything cleanly.
Added ruff-format for code and doc. It changed some doc that blacken doc missed. I can produce a diff specifically for the doc changes because a lot of things changed overall. I still need to check how many checks are actually used in |
Looks like most of the diff is from changing the max line length from 88 to 120. I think we should stick with 88 since that's the established style. |
Removed flake8-docstring, because it's a wrapper around pydocstyle, and pydocstyle explicitly asks to use ruff in PyCQA/pydocstyle#658. Also the configuration given in the ruff thread about isort I'm going to check how to format for 88 chars and not have warning for line longer than that next, thank you the pointer @bluetech . Should I separate the manual fixes in another merge request ? |
autoflake
, black
, isort
, some of pre-commit
checks, pyupgrade
, and flake8
to ruff
autoflake
, black
, isort
, pyupgrade
, flake8
and pydocstyle
, to ruff
Most of the diff now seems to be related to the newly autofixed rules from pydocstyle. (But it should probably be a good idea to rebase and re-apply the pre-commit fixes from zero) |
For the docstrings, we should go with D212 and disable D213, that would fit our current style better. |
I think it's OK to do them here. I'll do a full look over once you're ready. |
a550fa8
to
6ecc15d
Compare
Ready for review, I removed flake8 entirely even if we don't have an equivalent of flake8-typing-import (but almost all checks are not useful if we don't support python 3.7 and the only check that is useful will be included in ruff at some point see astral-sh/ruff#2302). |
I need to either add one of the autofix from pydocstyle in the unfixable list so it doesn't break the doctests, or exclude the code example from ruff-format (example_scripts/doctests/main.py, maybe others) |
Looks at the docstring changes. Let's set
this allows to remove the ignores for the conflicting rules D203 and D213 since the convention implies that. Also I see many changes to one-sentence docstrings
into
I don't like it personally and it breaks our existing style, so let's disable D209 for now. |
6ecc15d
to
2c45d72
Compare
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.
Great work @Pierre-Sassoulas, went through everything and it looks good to me.
One thing though, I tried it and it seems like ruff format doesn't support formatting code snippets in rst and md files yet. It does support formatting rst and md snippets in docstrings but that's not what we need. So until ruff implements this (astral-sh/ruff#8237), we should keep blacken-docs.
testing/test_assertrewrite.py
Outdated
@@ -1868,16 +1867,13 @@ def test_simple(): | |||
id="assert statement not on own line", | |||
), | |||
pytest.param( | |||
b"def x():\n" |
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 changes here are definitely worse after, dunno if there's anything to be done though.
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.
Oups, I missed this, I only searched for " f"
and " "
, after fixing it would be b"def x():\n assert (\n 1 + 2 # comment\n )\n",
not extraordinary, but better I guess ?
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.
Another way would be to change it to:
b"""def x():
assert (
1 + 2 # comment
)
"""
And it seems to works for bytes.
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.
Multiline b works but I think it will be annoying with the indentation.
Maybe just put # fmt: off
/# fmt: on
around them?
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 scope of # fmt: off/# fmt: on
might not be exactly the same in ruff vs black, I had to "un-nest" the existing fmt: off
.
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.
195f1ea
to
3d53bbe
Compare
I rebased all the autofix commits to create the "one commit we can put in https://github.com/pytest-dev/pytest/blob/main/.git-blame-ignore-revs". Let me know what to do with all the manual fixes, squash all together or clean up the history. |
The rule I try to follow is "every commit should pass CI", this is very useful for bisects for example. The easiest way to achieve this here is to squash everything to one commit. If you want to preserve separate commits it will require a bit more effort. |
All right, I can squash everything then upgrade the |
…e, to ruff ruff is faster and handle everything we had prior. isort configuration done based on the indication from astral-sh/ruff#4670, previousely based on reorder-python-import (pytest-dev#11896) flake8-docstrings was a wrapper around pydocstyle (now archived) that explicitly asks to use ruff in PyCQA/pydocstyle#658. flake8-typing-import is useful mainly for project that support python 3.7 and the one useful check will be implemented in astral-sh/ruff#2302 We need to keep blacken-doc because ruff does not handle detection of python code inside .md and .rst. The direct link to the repo is now used to avoid a redirection. Manual fixes: - Lines that became too long - % formatting that was not done automatically - type: ignore that were moved around - noqa of hard to fix issues (UP031 generally) - fmt: off and fmt: on that is not really identical between black and ruff - autofix re-order in pre-commit from faster to slower Co-authored-by: Ran Benita <ran@unusedvar.com>
…n-docs 2024's style
7fa06f8
to
9ef905e
Compare
@Pierre-Sassoulas thanks for the work on this! I think this should be backported to ease other backports. |
Follow-up to #11896, this seems to be blocked by astral-sh/ruff#4670 unless we're ready to accept a little churn. There are other tools that we could remove if we activate more ruff message, but I figured doing one at a time would help with the reviews.