Repository: astral-sh/ruff
File: scripts/conformance.py (Python)

Code before:
def render_grouped_diagnostics(
    grouped: list[GroupedDiagnostics],
    *,
    changed_only: bool = True,
    format: Literal["diff", "github"] = "diff",
) -> str:
    if changed_only:
        grouped = [
            diag for diag in grouped if diag.change in (Change.ADDED, Change.REMOVED)
        ]

    g1, g2 = tee(grouped)
    required, optional = (
        filterfalse(attrgetter("optional"), g1),
        filter(attrgetter("optional"), g2),
    )
    required = sorted(
        required,
        key=attrgetter("classification"),
        reverse=True,
    )
    optional = sorted(
        optional,
        key=attrgetter("change"),
        reverse=True,
    )

    match format:
        case "diff":
            header = ["```diff"]
            footer = "```"
        case "github":
            header = [
                "| Location | Name | Message |",
                "|----------|------|---------|",
            ]
            footer = ""
        case _:
            raise ValueError("format must be one of 'diff' or 'github'")

    lines = []
    for group, diagnostics in chain(
        groupby(required, key=attrgetter("classification")),
        groupby(optional, key=attrgetter("change")),
    ):
        lines.append(f"### {group.into_title()}")
        lines.extend(["", "<details>", ""])

        lines.extend(header)

        for diag in diagnostics:

Review comment (suggestion):
I think in this case, you can do this in a slightly more readable way without the `tee` ;)

What about something like

```diff
diff --git a/scripts/conformance.py b/scripts/conformance.py
index f8e7124d8a..383c5eb6bf 100644
--- a/scripts/conformance.py
+++ b/scripts/conformance.py
@@ -435,19 +435,17 @@ def render_grouped_diagnostics(
             diag for diag in grouped if diag.change in (Change.ADDED, Change.REMOVED)
         ]
 
-    g1, g2 = tee(grouped)
-    required, optional = (
-        filterfalse(attrgetter("optional"), g1),
-        filter(attrgetter("optional"), g2),
-    )
-    required = sorted(
-        required,
-        key=attrgetter("classification"),
+    get_change = attrgetter("change")
+    get_classification = attrgetter("classification")
+
+    optional_diagnostics = sorted(
+        (diag for diag in grouped if diag.optional),
+        key=get_change,
         reverse=True,
     )
-    optional = sorted(
-        optional,
-        key=attrgetter("change"),
+    required_diagnostics = sorted(
+        (diag for diag in grouped if not diag.optional),
+        key=get_classification,
         reverse=True,
     )
 
@@ -466,8 +464,8 @@ def render_grouped_diagnostics(
 
     lines = []
     for group, diagnostics in chain(
-        groupby(required, key=attrgetter("classification")),
-        groupby(optional, key=attrgetter("change")),
+        groupby(required_diagnostics, key=get_classification),
+        groupby(optional_diagnostics, key=get_change),
     ):
         lines.append(f"### {group.into_title()}")
         lines.extend(["", "<details>", ""])
```

Code after:
def render_grouped_diagnostics(
    grouped: list[GroupedDiagnostics],
    *,
    changed_only: bool = True,
    format: Literal["diff", "github"] = "diff",
) -> str:
    if changed_only:
        grouped = [
            diag for diag in grouped if diag.change in (Change.ADDED, Change.REMOVED)
        ]

    get_change = attrgetter("change")
    get_classification = attrgetter("classification")

    optional_diagnostics = sorted(
        (diag for diag in grouped if diag.optional),
        key=get_change,
        reverse=True,
    )
    required_diagnostics = sorted(
        (diag for diag in grouped if not diag.optional),
        key=get_classification,
        reverse=True,
    )

    match format:
        case "diff":
            header = ["```diff"]
            footer = "```"
        case "github":
            header = [
                "| Location | Name | Message |",
                "|----------|------|---------|",
            ]
            footer = ""
        case _:
            raise ValueError("format must be one of 'diff' or 'github'")

    lines = []
    for group, diagnostics in chain(
        groupby(required_diagnostics, key=get_classification),
        groupby(optional_diagnostics, key=get_change),
    ):
        lines.append(f"### {group.into_title()}")
        lines.extend(["", "<details>", ""])

        lines.extend(header)

        for diag in diagnostics:
            lines.append(diag.display(format=format))