openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/lib/test_audio.py
83lines · modecode
| 1 | from __future__ import annotations |
| 2 | |
| 3 | import sys |
| 4 | import inspect |
| 5 | import typing_extensions |
| 6 | from typing import get_args |
| 7 | |
| 8 | import pytest |
| 9 | |
| 10 | from openai import OpenAI, AsyncOpenAI |
| 11 | from tests.utils import evaluate_forwardref |
| 12 | from openai._utils import assert_signatures_in_sync |
| 13 | from openai._compat import is_literal_type |
| 14 | from openai._utils._typing import is_union_type |
| 15 | from openai.types.audio_response_format import AudioResponseFormat |
| 16 | |
| 17 | |
| 18 | @pytest.mark.parametrize("sync", [True, False], ids=["sync", "async"]) |
| 19 | def 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"]) |
| 53 | def 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 | |