I am trying to fix my typing coming from Pylance, but I am struggling with this one.
Argument of type "str" cannot be assigned to parameter "voice" of type "Literal['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer']" in function "create"
Type "str" is incompatible with type "Literal['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer']"
"str" is incompatible with type "Literal['alloy']"
"str" is incompatible with type "Literal['echo']"
"str" is incompatible with type "Literal['fable']"
"str" is incompatible with type "Literal['onyx']"
"str" is incompatible with type "Literal['nova']"
"str" is incompatible with type "Literal['shimmer']"PylancereportArgumentType
Below is my snippet of code, the warning occurs at voice=self.voice.
def speak(self, text, destination, api_key):
client = OpenAI(api_key=api_key)
try:
log.log_ui(logger, "Generating speech via OpenAI")
response = client.audio.speech.create(
model="tts-1",
voice=self.voice, # The error is shown here.
input=text,
)
except AuthenticationError as err:
log.log_ui(logger, f"OpenAI authentication error, please check you API: {err}")
return
response.stream_to_file(destination)
play(destination)
The implementation from the upstream client.audio.speech.create indeed defines voice as a Literal. But I can't figure out how to pass my string as a literal value.
Does anyone have any ideas?
def create(
self,
...REDACTED...
voice: Literal["alloy", "echo", "fable", "onyx", "nova", "shimmer"],
...REDACTED...
) -> _legacy_response.HttpxBinaryResponseContent: