Files
Brent Westbrook 947179d7c6 Enable lint.preview and missing default rules in our Ruff config (#28112)
Summary
--

This PR updates our ruff-pre-commit dependency to 0.16.5, which supports
the new category selectors,
and then enables `lint.preview` and the new default categories so that
we can dogfood them on our
code. There were 66 diagnostics initially, which I also fixed or
ignored:

```
28	FURB113 	[ ] repeated-append
12	ASYNC240	[ ] blocking-path-method-in-async-function
 6	RUF105  	[*] noqa-comments
 5	PLW1510 	[ ] subprocess-run-without-check
 3	PLW0108 	[ ] unnecessary-lambda
 2	EXE001  	[ ] shebang-not-executable
 2	RUF039  	[*] unraw-re-pattern
 1	B024    	[ ] abstract-base-class-without-abstract-method
 1	FURB142 	[*] for-loop-set-mutations
 1	PERF102 	[ ] incorrect-dict-iterator
 1	PLC0207 	[*] missing-maxsplit-arg
 1	PLW0127 	[ ] self-assigning-variable
 1	TRY004  	[ ] type-check-without-type-error
 1	PLW0120 	[*] useless-else-on-loop
 1	TRY201  	[ ] verbose-raise
 ```

Of these, I ignored `repeated-append` and `blocking-path-method-in-async-function`. I think they are
still reasonable defaults, just less useful/important in our uses.

0.16.5 also includes https://github.com/astral-sh/ruff-pre-commit/pull/159, so I removed `markdown` from our main `ruff-format` hook, leaving it for the later mdtest formatting stage.

I also had to make a few changes related to config discovery in tests:
- move our cached ecosystem checkouts to `$RUNNER_TEMP` instead of inside of our checkout
- convert some CLI tests to use the `CliTest` helpers
- add `configurationPreference: editorOnly` to some notebook tests

In these cases, our own Ruff settings were being inherited by test code. This likely affects many other tests, but I only updated the obviously failing ones for now. I'm least confident about the notebook tests, but as far as I can tell from the history (https://github.com/astral-sh/ruff/pull/11944, https://github.com/astral-sh/ruff/pull/22810), the specific diagnostics are not the important parts being asserted.

Test Plan
--

CI on this PR
2026-08-28 12:24:36 -04:00

176 lines
5.3 KiB
Python
Executable File

#! /usr/bin/python
"""See CONTRIBUTING.md"""
# %%
from __future__ import annotations
import re
from collections import defaultdict
from pathlib import Path
from subprocess import check_output
def rustfmt(code: str) -> str:
return check_output(["rustfmt", "--emit=stdout"], input=code, text=True)
# %%
# Read nodes
root = Path(
check_output(["git", "rev-parse", "--show-toplevel"], text=True).strip(),
)
nodes_file = (
root.joinpath("crates")
.joinpath("ruff_python_ast")
.joinpath("src")
.joinpath("generated.rs")
.read_text()
)
node_lines = (
nodes_file.split("pub enum AnyNodeRef<'a> {")[1].split("}")[0].strip().splitlines()
)
nodes = []
for node_line in node_lines:
node = node_line.split("(")[1].split(")")[0].split("::")[-1].removeprefix("&'a ")
# `FString` has a custom implementation while the formatting for
# `FStringLiteralElement`, `FStringFormatSpec` and `FStringExpressionElement` are
# handled by the `FString` implementation.
if node in (
"InterpolatedStringLiteralElement",
"InterpolatedElement",
"InterpolatedStringFormatSpec",
"Identifier",
):
continue
nodes.append(node)
print(nodes)
# %%
# Generate newtypes with dummy FormatNodeRule implementations
out = (
root.joinpath("crates")
.joinpath("ruff_python_formatter")
.joinpath("src")
.joinpath("generated.rs")
)
src = root.joinpath("crates").joinpath("ruff_python_formatter").joinpath("src")
nodes_grouped = defaultdict(list)
# We rename because mod is a keyword in rust
groups = {
"mod": "module",
"expr": "expression",
"stmt": "statement",
"pattern": "pattern",
"type_param": "type_param",
"other": "other",
}
def group_for_node(node: str) -> str:
for group in groups:
if node.startswith(group.title().replace("_", "")):
return group
return "other"
def to_camel_case(node: str) -> str:
"""Converts PascalCase to camel_case"""
return re.sub(r"([A-Z])", r"_\1", node).lower().lstrip("_")
for node in nodes:
nodes_grouped[group_for_node(node)].append(node)
for group, group_nodes in nodes_grouped.items():
# These conflict with the manually content of the mod.rs files
# src.joinpath(groups[group]).mkdir(exist_ok=True)
# mod_section = "\n".join(
# f"pub(crate) mod {to_camel_case(node)};" for node in group_nodes
# )
# src.joinpath(groups[group]).joinpath("mod.rs").write_text(rustfmt(mod_section))
for node in group_nodes:
node_path = src.joinpath(groups[group]).joinpath(f"{to_camel_case(node)}.rs")
# Don't override existing manual implementations
if node_path.exists():
continue
code = f"""
use ruff_formatter::write;
use ruff_python_ast::{node};
use crate::verbatim_text;
use crate::prelude::*;
#[derive(Default)]
pub struct Format{node};
impl FormatNodeRule<{node}> for Format{node} {{
fn fmt_fields(&self, item: &{node}, f: &mut PyFormatter) -> FormatResult<()> {{
write!(f, [verbatim_text(item)])
}}
}}
""".strip()
node_path.write_text(rustfmt(code))
# %%
# Generate `FormatRule`, `AsFormat` and `IntoFormat`
generated = """//! This is a generated file. Don't modify it by hand! Run `crates/ruff_python_formatter/generate.py` to re-generate the file.
#![allow(unknown_lints, clippy::default_constructed_unit_structs)]
use crate::context::PyFormatContext;
use crate::{AsFormat, FormatNodeRule, IntoFormat, PyFormatter};
use ruff_formatter::{FormatOwnedWithRule, FormatRefWithRule, FormatResult, FormatRule};
use ruff_python_ast as ast;
"""
for node in nodes:
text = f"""
impl FormatRule<ast::{node}, PyFormatContext<'_>>
for crate::{groups[group_for_node(node)]}::{to_camel_case(node)}::Format{node}
{{
#[inline]
fn fmt(
&self,
node: &ast::{node},
f: &mut PyFormatter,
) -> FormatResult<()> {{
FormatNodeRule::<ast::{node}>::fmt(self, node, f)
}}
}}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::{node} {{
type Format<'a> = FormatRefWithRule<
'a,
ast::{node},
crate::{groups[group_for_node(node)]}::{to_camel_case(node)}::Format{node},
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {{
FormatRefWithRule::new(
self,
crate::{groups[group_for_node(node)]}::{to_camel_case(node)}::Format{node}::default(),
)
}}
}}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::{node} {{
type Format = FormatOwnedWithRule<
ast::{node},
crate::{groups[group_for_node(node)]}::{to_camel_case(node)}::Format{node},
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {{
FormatOwnedWithRule::new(
self,
crate::{groups[group_for_node(node)]}::{to_camel_case(node)}::Format{node}::default(),
)
}}
}}
"""
generated += text
out.write_text(rustfmt(generated))