openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1bdabb489a86bd721a3e237d8556583ed0d8dfa1

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/lib/test_audio.py

83lines · modecode

1from __future__ import annotations
2
3import sys
4import inspect
5import typing_extensions
6from typing import get_args
7
8import pytest
9
10from openai import OpenAI, AsyncOpenAI
11from tests.utils import evaluate_forwardref
12from openai._utils import assert_signatures_in_sync
13from openai._compat import is_literal_type
14from openai._utils._typing import is_union_type
15from openai.types.audio_response_format import AudioResponseFormat
16
17
18@pytest.mark.parametrize("sync", [True, False], ids=["sync", "async"])
19def test_translation_create_overloads_in_sync(sync: bool, client: OpenAI, async_client: AsyncOpenAI) -> None:
20 checking_client: OpenAI | AsyncOpenAI = client if sync else async_client
21
22 fn = checking_client.audio.translations.create
23 overload_response_formats: set[str] = set()
24
25 for i, overload in enumerate(typing_extensions.get_overloads(fn)):
26 assert_signatures_in_sync(
27 fn,
28 overload,
29 exclude_params={"response_format"},
30 description=f" for overload {i}",
31 )
32
33 sig = inspect.signature(overload)
34 typ = evaluate_forwardref(
35 sig.parameters["response_format"].annotation,
36 globalns=sys.modules[fn.__module__].__dict__,
37 )
38 if is_union_type(typ):
39 for arg in get_args(typ):
40 if not is_literal_type(arg):
41 continue
42
43 overload_response_formats.update(get_args(arg))
44 elif is_literal_type(typ):
45 overload_response_formats.update(get_args(typ))
46
47 src_response_formats: set[str] = set(get_args(AudioResponseFormat))
48 diff = src_response_formats.difference(overload_response_formats)
49 assert len(diff) == 0, f"some response format options don't have overloads"
50
51
52@pytest.mark.parametrize("sync", [True, False], ids=["sync", "async"])
53def test_transcription_create_overloads_in_sync(sync: bool, client: OpenAI, async_client: AsyncOpenAI) -> None:
54 checking_client: OpenAI | AsyncOpenAI = client if sync else async_client
55
56 fn = checking_client.audio.transcriptions.create
57 overload_response_formats: set[str] = set()
58
59 for i, overload in enumerate(typing_extensions.get_overloads(fn)):
60 assert_signatures_in_sync(
61 fn,
62 overload,
63 exclude_params={"response_format"},
64 description=f" for overload {i}",
65 )
66
67 sig = inspect.signature(overload)
68 typ = evaluate_forwardref(
69 sig.parameters["response_format"].annotation,
70 globalns=sys.modules[fn.__module__].__dict__,
71 )
72 if is_union_type(typ):
73 for arg in get_args(typ):
74 if not is_literal_type(arg):
75 continue
76
77 overload_response_formats.update(get_args(arg))
78 elif is_literal_type(typ):
79 overload_response_formats.update(get_args(typ))
80
81 src_response_formats: set[str] = set(get_args(AudioResponseFormat))
82 diff = src_response_formats.difference(overload_response_formats)
83 assert len(diff) == 0, f"some response format options don't have overloads"
84