openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.106.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/detect-breaking-changes.py

79lines · modeblame

72e0ad60stainless-app[bot]10 months ago1from __future__ import annotations
2
3import sys
4from typing import Iterator
5from pathlib import Path
6
7import rich
8import griffe
9from rich.text import Text
10from rich.style import Style
11
12
13def public_members(obj: griffe.Object | griffe.Alias) -> dict[str, griffe.Object | griffe.Alias]:
14if isinstance(obj, griffe.Alias):
15# ignore imports for now, they're technically part of the public API
16# but we don't have good preventative measures in place to prevent
17# changing them
18return {}
19
20return {name: value for name, value in obj.all_members.items() if not name.startswith("_")}
21
22
23def find_breaking_changes(
24new_obj: griffe.Object | griffe.Alias,
25old_obj: griffe.Object | griffe.Alias,
26*,
27path: list[str],
28) -> Iterator[Text | str]:
29new_members = public_members(new_obj)
30old_members = public_members(old_obj)
31
32for name, old_member in old_members.items():
33if isinstance(old_member, griffe.Alias) and len(path) > 2:
34# ignore imports in `/types/` for now, they're technically part of the public API
35# but we don't have good preventative measures in place to prevent changing them
36continue
37
38new_member = new_members.get(name)
39if new_member is None:
40cls_name = old_member.__class__.__name__
41yield Text(f"({cls_name})", style=Style(color="rgb(119, 119, 119)"))
42yield from [" " for _ in range(10 - len(cls_name))]
43yield f" {'.'.join(path)}.{name}"
44yield "\n"
45continue
46
47yield from find_breaking_changes(new_member, old_member, path=[*path, name])
48
49
50def main() -> None:
51try:
52against_ref = sys.argv[1]
53except IndexError as err:
54raise RuntimeError("You must specify a base ref to run breaking change detection against") from err
55
56package = griffe.load(
57"openai",
58search_paths=[Path(__file__).parent.parent.joinpath("src")],
59)
60old_package = griffe.load_git(
61"openai",
62ref=against_ref,
63search_paths=["src"],
64)
65assert isinstance(package, griffe.Module)
66assert isinstance(old_package, griffe.Module)
67
68output = list(find_breaking_changes(package, old_package, path=["openai"]))
69if output:
70rich.print(Text("Breaking changes detected!", style=Style(color="rgb(165, 79, 87)")))
71rich.print()
72
73for text in output:
74rich.print(text, end="")
75
76sys.exit(1)
77
78
79main()