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

Replacing isort with Ruff #10912

Merged
merged 10 commits into from
Oct 28, 2023
Merged

Conversation

Avasam
Copy link
Collaborator

@Avasam Avasam commented Oct 18, 2023

This PR explores using Ruff for linting and sorting imports.
This should lead to less configurations, one less dependency and faster imports auto-formatting
Ruff respects isort's action comments like isort:skip_file

There's some overlap with #10910 since both need to initially add the Ruff check in github actions.
Closes #10911

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@Avasam
Copy link
Collaborator Author

Avasam commented Oct 19, 2023

Waiting for #10915 and #10914 to land before fixing conflicts
Added comment on that PR, so whichever doesn't get merged first updates the isort line in scripts/sync_tensorflow_protobuf_stubs.sh

extra_standard_library = [
"typing_extensions",
[tool.ruff.isort]
combine-as-imports = true
Copy link
Contributor

Choose a reason for hiding this comment

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

I recommend also split-on-trailing-comma = false, for closer isort-like behavior.

Otherwise Ruff doesn't collapse multiline imports (even if they do fit on one line).

Copy link
Collaborator Author

@Avasam Avasam Oct 19, 2023

Choose a reason for hiding this comment

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

Regardless of intentions for the default value, I'm down to keep imports on a single line when possible anyway. Especially since we have skip_magic_trailing_comma = true configured for Black

Thanks for letting me know of this difference.

@Avasam Avasam mentioned this pull request Oct 19, 2023
@github-actions

This comment has been minimized.

def run_ruff(stub_dir: str) -> None:
print(f"Running Ruff: ruff {stub_dir}")
subprocess.run([sys.executable, "-m", "ruff", stub_dir])
subprocess.run([sys.executable, "-m", "ruff", stub_dir, "--fix-only"])
Copy link
Collaborator

Choose a reason for hiding this comment

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

What does this do?

Copy link
Member

Choose a reason for hiding this comment

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

Fix errors it can fix, and don't report on things it can't.

Ruff now recommends explicitly adding check:

Suggested change
subprocess.run([sys.executable, "-m", "ruff", stub_dir, "--fix-only"])
subprocess.run([sys.executable, "-m", "ruff", "check", stub_dir, "--fix-only"])

Copy link
Collaborator Author

@Avasam Avasam Oct 22, 2023

Choose a reason for hiding this comment

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

Ruff now recommends explicitly adding check

Right because of ruff format and to be explicit. I'll update other code and doc locations too.

@@ -73,7 +73,7 @@ PROTO_FILES=$(grep "GenProto.*google" $PYTHON_PROTOBUF_DIR/python/setup.py | \
# shellcheck disable=SC2086
protoc_install/bin/protoc --proto_path="$PYTHON_PROTOBUF_DIR/src" --mypy_out="relax_strict_optional_primitives:$REPO_ROOT/stubs/protobuf" $PROTO_FILES

isort "$REPO_ROOT/stubs/protobuf"
ruff "$REPO_ROOT/stubs/protobuf" --exit-non-zero-on-fix --fix-only
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think --exit-non-zero-on-fix is backwards. If fixing happens, we want ruff to exit with success so that the script continues. It uses set -e, so nonzero exit code would stop the script here.

Copy link
Collaborator Author

@Avasam Avasam Oct 22, 2023

Choose a reason for hiding this comment

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

oops, you're right.
I'm also wondering whether --fix-only is wanted here. If you're gonna get errors later, may as well report them now? As long as it doesn't block script execution. Same with create_baseline_stubs

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JelleZijlstra JelleZijlstra merged commit 49ba409 into python:main Oct 28, 2023
96 checks passed
@Avasam Avasam deleted the replace-isort-with-Ruff branch October 28, 2023 15:41
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.

Replace isort with ruff?
4 participants