Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/elevenlabs/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ def __init__(
self._speech_to_text = SpeechToTextClient(client_wrapper=self._client_wrapper)
self._speech_engine = SpeechEngineClient(client_wrapper=self._client_wrapper)

@property
def text_to_speech(self) -> RealtimeTextToSpeechClient:
return typing.cast(RealtimeTextToSpeechClient, self._text_to_speech)

@property
def webhooks(self) -> WebhooksClient:
return typing.cast(WebhooksClient, self._webhooks)

@property
def music(self) -> MusicClient:
return typing.cast(MusicClient, self._music)

@property
def speech_to_text(self) -> SpeechToTextClient:
return typing.cast(SpeechToTextClient, self._speech_to_text)

@property
def speech_engine(self) -> SpeechEngineClient:
return typing.cast(SpeechEngineClient, self._speech_engine)
Expand Down Expand Up @@ -131,6 +147,18 @@ def __init__(
self._speech_to_text = AsyncSpeechToTextClient(client_wrapper=self._client_wrapper)
self._speech_engine = AsyncSpeechEngineClient(client_wrapper=self._client_wrapper)

@property
def webhooks(self) -> AsyncWebhooksClient:
return typing.cast(AsyncWebhooksClient, self._webhooks)

@property
def music(self) -> AsyncMusicClient:
return typing.cast(AsyncMusicClient, self._music)

@property
def speech_to_text(self) -> AsyncSpeechToTextClient:
return typing.cast(AsyncSpeechToTextClient, self._speech_to_text)

@property
def speech_engine(self) -> AsyncSpeechEngineClient:
return typing.cast(AsyncSpeechEngineClient, self._speech_engine)
43 changes: 43 additions & 0 deletions tests/test_client_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import typing

import pytest

from elevenlabs.client import AsyncElevenLabs, ElevenLabs
from elevenlabs.music_custom import AsyncMusicClient, MusicClient
from elevenlabs.realtime_tts import RealtimeTextToSpeechClient
from elevenlabs.speech_engine_custom import AsyncSpeechEngineClient, SpeechEngineClient
from elevenlabs.speech_to_text_custom import AsyncSpeechToTextClient, SpeechToTextClient
from elevenlabs.webhooks_custom import AsyncWebhooksClient, WebhooksClient

# The base client's properties are typed as the autogenerated clients, so type checkers
# reject the custom methods (e.g. `client.webhooks.construct_event`) unless the wrapper
# re-declares each property with the custom client type.
SYNC_PROPERTIES = {
"text_to_speech": RealtimeTextToSpeechClient,
"webhooks": WebhooksClient,
"music": MusicClient,
"speech_to_text": SpeechToTextClient,
"speech_engine": SpeechEngineClient,
}
ASYNC_PROPERTIES = {
"webhooks": AsyncWebhooksClient,
"music": AsyncMusicClient,
"speech_to_text": AsyncSpeechToTextClient,
"speech_engine": AsyncSpeechEngineClient,
}


@pytest.mark.parametrize(
"client_cls, name, expected",
[(ElevenLabs, name, cls) for name, cls in SYNC_PROPERTIES.items()]
+ [(AsyncElevenLabs, name, cls) for name, cls in ASYNC_PROPERTIES.items()],
)
def test_custom_client_property_is_typed(client_cls: typing.Any, name: str, expected: type) -> None:
prop = getattr(client_cls, name)
assert typing.get_type_hints(prop.fget)["return"] is expected
assert type(getattr(client_cls(api_key="test"), name)) is expected


def test_construct_event_is_accessible() -> None:
assert callable(ElevenLabs(api_key="test").webhooks.construct_event)
assert callable(AsyncElevenLabs(api_key="test").webhooks.construct_event)