Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 64 additions & 15 deletions src/driftbench/extract.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""Find candidate API changes (renames, deprecations, replacements) in release notes.

This is deliberately simple: it only matches sentences that name both the old and the new
API in backticks. Every candidate still needs a human (or model) review before it becomes
a question, because release notes also rename internal things nobody calls.
Match sentences that name both APIs in backticks and fenced import migrations.
Every candidate still needs review before it becomes a question.
"""

from __future__ import annotations
Expand All @@ -13,6 +12,8 @@
CODE = r"`(?P<{name}>[^`\n]{{2,80}})`"
OLD = CODE.format(name="old")
NEW = CODE.format(name="new")
CONTEXT = CODE.format(name="context")
PARAMETER = rf"{OLD}\s+parameter\s+(?:in|of)\s+{CONTEXT}"

# Each pattern must capture `old` and `new`. Order matters only for readability.
PATTERNS: list[tuple[str, re.Pattern[str]]] = [
Expand All @@ -36,8 +37,39 @@
),
("replacement", re.compile(rf"\breplace[sd]?\s+{OLD}\s+with\s+{NEW}", re.I)),
("replacement", re.compile(rf"\buse\s+{NEW}\s+instead\s+of\s+{OLD}", re.I)),
(
"deprecation",
re.compile(
rf"{PARAMETER}\s+is\s+deprecated\.\s+"
rf"(?:use|configure\s+[^.`]{{1,60}}\s+with)\s+{NEW}\s+instead\b",
re.I,
),
),
(
"deprecation",
re.compile(
rf"\bdeprecate[sd]?\s+(?:the\s+)?{PARAMETER},\s+replace\s+with\s+(?:new\s+)?{NEW}",
re.I,
),
),
]

PROPERTY = r"`[A-Za-z_]\w*`"
PROPERTIES = rf"{PROPERTY}(?:\s*/\s*{PROPERTY})*"
PROPERTY_CHANGE = re.compile(
rf"\bnow\s+exposes\s+(?P<new>{PROPERTIES})\s+properties\s+"
rf"\((?P<old>{PROPERTIES})\s+are\s+deprecated\)",
re.I,
)
DIFF_BLOCK = re.compile(r"^[ \t]*```diff[ \t]*\n(?P<body>.*?)^[ \t]*```[ \t]*$", re.M | re.S)
IMPORT_PAIR = re.compile(
r"^[ \t]*-[ \t]*(?P<old>(?P<binding>import[ \t]+[^'\"\n]{1,80}?[ \t]+from)[ \t]+"
r"(?P<old_quote>['\"])(?P<old_module>[^'\"\n]{1,80})(?P=old_quote);?)[ \t]*\n"
r"[ \t]*\+[ \t]*(?P<new>(?P=binding)[ \t]+"
r"(?P<new_quote>['\"])(?P<new_module>[^'\"\n]{1,80})(?P=new_quote);?)[ \t]*$",
re.M,
)

# Things in backticks that are not APIs: versions, commit hashes, file globs, flags-only noise.
_NOT_API = re.compile(r"^(v?\d+(\.\d+)*|[0-9a-f]{7,40}|\*+|-+)$")

Expand All @@ -61,23 +93,40 @@ def _clean(token: str) -> str:
return token.strip().strip("`").strip()


def extract_from_text(text: str) -> list[tuple[str, str, str, str]]:
"""Return (kind, old, new, excerpt) for every match, one excerpt per source line."""
found: list[tuple[str, str, str, str]] = []
seen: set[tuple[str, str]] = set()
def _candidate_matches(text: str):
for line in text.splitlines():
line = line.strip()
if "`" not in line:
continue
for kind, pat in PATTERNS:
for m in pat.finditer(line):
old, new = _clean(m.group("old")), _clean(m.group("new"))
if old == new or _NOT_API.match(old) or _NOT_API.match(new):
continue
if (old, new) in seen:
continue
seen.add((old, new))
found.append((kind, old, new, line[:400]))
for match in pat.finditer(line):
old = _clean(match["old"])
context = match.groupdict().get("context")
if context:
old = f"{old} in {_clean(context)}"
yield kind, old, _clean(match["new"]), line
for match in PROPERTY_CHANGE.finditer(line):
old = re.findall(PROPERTY, match["old"])
new = re.findall(PROPERTY, match["new"])
if len(old) == len(new):
for previous, current in zip(old, new, strict=True):
yield "deprecation", _clean(previous), _clean(current), line

for block in DIFF_BLOCK.finditer(text.replace("\r\n", "\n")):
for match in IMPORT_PAIR.finditer(block["body"]):
if match["old_module"] != match["new_module"]:
yield "replacement", match["old"], match["new"], match[0].strip()


def extract_from_text(text: str) -> list[tuple[str, str, str, str]]:
"""Return (kind, old, new, excerpt) for every distinct matching API pair."""
found: list[tuple[str, str, str, str]] = []
seen: set[tuple[str, str]] = set()
for kind, old, new, excerpt in _candidate_matches(text):
if old == new or _NOT_API.match(old) or _NOT_API.match(new) or (old, new) in seen:
continue
seen.add((old, new))
found.append((kind, old, new, excerpt[:400]))
return found


Expand Down
36 changes: 36 additions & 0 deletions tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,39 @@ def test_ignores_versions_and_lines_without_code():
def test_duplicate_pair_reported_once():
text = "Rename `a_b` to `c_d`\nRenamed `a_b` to `c_d` again"
assert len(pairs(text)) == 1


def test_deprecation_followed_by_configure_instead():
text = (
"The `root_path` parameter in `FastAPI()` is deprecated. "
"Configure the server with `fastapi run --root-path /api/v1` instead."
)
assert pairs(text) == [("deprecation", "root_path in FastAPI()", "fastapi run --root-path /api/v1")]


def test_import_migration_diff():
text = "```diff\n- import { serveStatic } from 'hono/bun'\n+ import { serveStatic } from '@hono/bun'\n```"
assert pairs(text) == [
("replacement", "import { serveStatic } from 'hono/bun'", "import { serveStatic } from '@hono/bun'")
]


def test_deprecate_parameter_and_replace_with_new_option():
text = (
"- Deprecate `strict` parameter of `pl.concat`, replace with new `how='horizontal_extend'` (#27965)"
)
assert pairs(text) == [("deprecation", "strict in pl.concat", "how='horizontal_extend'")]


def test_exposes_multiple_replacement_properties_in_order():
text = (
"- consoleMessage.location() now exposes `line` / `column` properties "
"(`lineNumber` / `columnNumber` are deprecated)."
)
assert pairs(text) == [("deprecation", "lineNumber", "line"), ("deprecation", "columnNumber", "column")]


def test_ignores_non_migration_diffs():
assert pairs("```diff\n- version 1.2\n+ version 1.3\n```") == []
assert pairs("- import { x } from 'a'\n+ import { x } from 'b'") == []
assert pairs("```diff\n- import { x } from 'a'\n+ import { y } from 'b'\n```") == []