Skip to content

Commit 6044e1b

Browse files
stainless-app[bot]RobertCraigie
authored andcommitted
chore(internal): add reflection helper function (#1508)
1 parent 6316b7c commit 6044e1b

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/openai/_utils/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,7 @@
4949
maybe_transform as maybe_transform,
5050
async_maybe_transform as async_maybe_transform,
5151
)
52-
from ._reflection import function_has_argument as function_has_argument
52+
from ._reflection import (
53+
function_has_argument as function_has_argument,
54+
assert_signatures_in_sync as assert_signatures_in_sync,
55+
)

src/openai/_utils/_reflection.py

+34
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import inspect
24
from typing import Any, Callable
35

@@ -6,3 +8,35 @@ def function_has_argument(func: Callable[..., Any], arg_name: str) -> bool:
68
"""Returns whether or not the given function has a specific parameter"""
79
sig = inspect.signature(func)
810
return arg_name in sig.parameters
11+
12+
13+
def assert_signatures_in_sync(
14+
source_func: Callable[..., Any],
15+
check_func: Callable[..., Any],
16+
*,
17+
exclude_params: set[str] = set(),
18+
) -> None:
19+
"""Ensure that the signature of the second function matches the first."""
20+
21+
check_sig = inspect.signature(check_func)
22+
source_sig = inspect.signature(source_func)
23+
24+
errors: list[str] = []
25+
26+
for name, source_param in source_sig.parameters.items():
27+
if name in exclude_params:
28+
continue
29+
30+
custom_param = check_sig.parameters.get(name)
31+
if not custom_param:
32+
errors.append(f"the `{name}` param is missing")
33+
continue
34+
35+
if custom_param.annotation != source_param.annotation:
36+
errors.append(
37+
f"types for the `{name}` param are do not match; source={repr(source_param.annotation)} checking={repr(source_param.annotation)}"
38+
)
39+
continue
40+
41+
if errors:
42+
raise AssertionError(f"{len(errors)} errors encountered when comparing signatures:\n\n" + "\n\n".join(errors))

0 commit comments

Comments
 (0)