| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
from typing import Iterator |
| 5 |
from pathlib import Path |
| 6 |
|
| 7 |
import griffe |
| 8 |
from rich.text import Text |
| 9 |
from rich.style import Style |
| 10 |
from rich.console import Console |
| 11 |
|
| 12 |
|
| 13 |
def resolve_assignment_alias(obj: griffe.Object | griffe.Alias) -> griffe.Object | griffe.Alias: |
| 14 |
# griffe models `Foo = Bar` (a deprecated alias for a renamed class) as an attribute |
| 15 |
# with no members, so compare against the class it points to instead. Aliases can be |
| 16 |
# chained (`Foo = Bar`, `Bar = Baz`), so keep following them until we reach a class. |
| 17 |
target: griffe.Object | griffe.Alias | None = obj |
| 18 |
seen: set[str] = set() |
| 19 |
while isinstance(target, griffe.Attribute) and isinstance(target.value, griffe.ExprName): |
| 20 |
if target.path in seen: # `A = B; B = A` |
| 21 |
return obj |
| 22 |
seen.add(target.path) |
| 23 |
try: |
| 24 |
# annotated as `Module | Class` upstream, but a re-exported name resolves to an `Alias` |
| 25 |
resolved: griffe.Object | griffe.Alias | None = target.value.resolved |
| 26 |
if isinstance(resolved, griffe.Alias): |
| 27 |
resolved = resolved.final_target |
| 28 |
target = resolved |
| 29 |
except Exception: |
| 30 |
return obj |
| 31 |
|
| 32 |
return target if isinstance(target, griffe.Class) else obj |
| 33 |
|
| 34 |
|
| 35 |
def public_members(obj: griffe.Object | griffe.Alias) -> dict[str, griffe.Object | griffe.Alias]: |
| 36 |
obj = resolve_assignment_alias(obj) |
| 37 |
if isinstance(obj, griffe.Alias): |
| 38 |
# ignore imports for now, they're technically part of the public API |
| 39 |
# but we don't have good preventative measures in place to prevent |
| 40 |
# changing them |
| 41 |
return {} |
| 42 |
|
| 43 |
return {name: value for name, value in obj.all_members.items() if not name.startswith("_")} |
| 44 |
|
| 45 |
|
| 46 |
def find_breaking_changes( |
| 47 |
new_obj: griffe.Object | griffe.Alias, |
| 48 |
old_obj: griffe.Object | griffe.Alias, |
| 49 |
*, |
| 50 |
path: list[str], |
| 51 |
) -> Iterator[Text | str]: |
| 52 |
new_members = public_members(new_obj) |
| 53 |
old_members = public_members(old_obj) |
| 54 |
|
| 55 |
for name, old_member in old_members.items(): |
| 56 |
if isinstance(old_member, griffe.Alias) and len(path) > 2: |
| 57 |
# ignore imports in `/types/` for now, they're technically part of the public API |
| 58 |
# but we don't have good preventative measures in place to prevent changing them |
| 59 |
continue |
| 60 |
|
| 61 |
new_member = new_members.get(name) |
| 62 |
if new_member is None: |
| 63 |
cls_name = old_member.__class__.__name__ |
| 64 |
yield Text(f"({cls_name})", style=Style(color="rgb(119, 119, 119)")) |
| 65 |
yield from [" " for _ in range(10 - len(cls_name))] |
| 66 |
yield f" {'.'.join(path)}.{name}" |
| 67 |
yield "\n" |
| 68 |
continue |
| 69 |
|
| 70 |
yield from find_breaking_changes(new_member, old_member, path=[*path, name]) |
| 71 |
|
| 72 |
|
| 73 |
def main() -> None: |
| 74 |
try: |
| 75 |
against_ref = sys.argv[1] |
| 76 |
except IndexError as err: |
| 77 |
raise RuntimeError("You must specify a base ref to run breaking change detection against") from err |
| 78 |
|
| 79 |
package = griffe.load( |
| 80 |
"anthropic", |
| 81 |
search_paths=[Path(__file__).parent.parent.joinpath("src")], |
| 82 |
) |
| 83 |
old_package = griffe.load_git( |
| 84 |
"anthropic", |
| 85 |
ref=against_ref, |
| 86 |
search_paths=["src"], |
| 87 |
) |
| 88 |
assert isinstance(package, griffe.Module) |
| 89 |
assert isinstance(old_package, griffe.Module) |
| 90 |
|
| 91 |
output = list(find_breaking_changes(package, old_package, path=["anthropic"])) |
| 92 |
if output: |
| 93 |
# soft_wrap: CI stdout is not a TTY, so rich would otherwise hard-wrap long symbol paths at 80 columns |
| 94 |
console = Console(soft_wrap=True) |
| 95 |
console.print(Text("Breaking changes detected!", style=Style(color="rgb(165, 79, 87)"))) |
| 96 |
console.print() |
| 97 |
|
| 98 |
for text in output: |
| 99 |
console.print(text, end="") |
| 100 |
|
| 101 |
sys.exit(1) |
| 102 |
|
| 103 |
|
| 104 |
main() |