Skip to content
Prev Previous commit
Next Next commit
use textwrap
  • Loading branch information
ShaharNaveh committed Sep 9, 2025
commit 30d1294b4fc48451684ca53db6daf49b96e7ed55
29 changes: 12 additions & 17 deletions scripts/lib_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@
import pathlib
import re
import sys
import textwrap
import typing

if typing.TYPE_CHECKING:
from collections.abc import Iterator

type Patches = dict[str, dict[str, list["PatchSpec"]]]
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix: PEP 695 type alias breaks under Python ≤3.11 (E999).

Flake8 E999 points here. Replace the type statement with a TypeAlias so the script runs on 3.11 (CI baseline).

Apply this diff:

-type Patches = dict[str, dict[str, list["PatchSpec"]]]
+Patches: typing.TypeAlias = dict[str, dict[str, list["PatchSpec"]]]
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
type Patches = dict[str, dict[str, list["PatchSpec"]]]
Patches: typing.TypeAlias = dict[str, dict[str, list["PatchSpec"]]]
🧰 Tools
🪛 Flake8 (7.2.0)

[error] 41-41: SyntaxError: invalid syntax

(E999)

🤖 Prompt for AI Agents
In scripts/lib_updater.py around line 41, the use of the PEP 695 "type" alias
causes Flake8 E999 on Python ≤3.11; replace it with typing.TypeAlias: add "from
typing import TypeAlias" to the imports and change the declaration to use the
TypeAlias annotation, e.g. "Patches: TypeAlias = dict[str, dict[str,
list['PatchSpec']]]", preserving the inner forward reference string for
PatchSpec.


COL_OFFSET = 4
INDENT1 = " " * COL_OFFSET
INDENT2 = INDENT1 * 2
DEFAULT_INDENT = " " * 4
COMMENT = "TODO: RUSTPYTHON"
UT = "unittest"

Expand Down Expand Up @@ -231,20 +230,18 @@ def iter_patch_lines(tree: ast.Module, patches: Patches) -> "Iterator[tuple[int,

# Phase 1: Iterate and mark existing tests
for cls_node, fn_node in iter_tests(tree):
cache[cls_node.name] = cls_node.end_lineno
specs = patches.get(cls_node.name, {}).pop(fn_node.name, None)
if not specs:
continue
cache[cls_node.name] = cls_node.end_lineno

lineno = min(
(dec_node.lineno for dec_node in fn_node.decorator_list),
default=fn_node.lineno,
)
indent = " " * fn_node.col_offset
yield (
lineno - 1,
"\n".join(f"{indent}{spec.as_decorator()}" for spec in specs),
)
patch_lines = "\n".join(spec.as_decorator() for spec in specs)
yield (lineno - 1, textwrap.indent(patch_lines, indent))

# Phase 2: Iterate and mark inhereted tests
for cls_name, tests in patches.items():
Expand All @@ -254,15 +251,13 @@ def iter_patch_lines(tree: ast.Module, patches: Patches) -> "Iterator[tuple[int,
continue

for test_name, specs in tests.items():
patch_lines = "\n".join(f"{INDENT1}{spec.as_decorator()}" for spec in specs)
yield (
lineno,
f"""
{patch_lines}
{INDENT1}def {test_name}(self):
{INDENT2}return super().{test_name}()
""".rstrip(),
)
decorators = "\n".join(spec.as_decorator() for spec in specs)
patch_lines = f"""
{decorators}
def {test_name}(self):
{DEFAULT_INDENT}return super().{test_name}()
""".rstrip()
yield (lineno, textwrap.indent(patch_lines, DEFAULT_INDENT))


def apply_patches(contents: str, patches: Patches) -> str:
Expand Down