Skip to content

Commit 6faffb1

Browse files
committed
fix(python/sdk): Fix mypy and pytest-httpx errors (#6609)
GitOrigin-RevId: 141b374033010e6154edd910e7082dba37159613
1 parent 60ca311 commit 6faffb1

File tree

7 files changed

+37
-53
lines changed

7 files changed

+37
-53
lines changed

assemblyai/api.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def create_transcript(
4141
by_alias=True,
4242
),
4343
)
44-
if response.status_code != httpx.codes.ok:
44+
if response.status_code != httpx.codes.OK:
4545
raise types.TranscriptError(
4646
f"failed to transcribe url {request.audio_url}: {_get_error_message(response)}"
4747
)
@@ -57,7 +57,7 @@ def get_transcript(
5757
f"{ENDPOINT_TRANSCRIPT}/{transcript_id}",
5858
)
5959

60-
if response.status_code != httpx.codes.ok:
60+
if response.status_code != httpx.codes.OK:
6161
raise types.TranscriptError(
6262
f"failed to retrieve transcript {transcript_id}: {_get_error_message(response)}",
6363
)
@@ -73,7 +73,7 @@ def delete_transcript(
7373
f"{ENDPOINT_TRANSCRIPT}/{transcript_id}",
7474
)
7575

76-
if response.status_code != httpx.codes.ok:
76+
if response.status_code != httpx.codes.OK:
7777
raise types.TranscriptError(
7878
f"failed to delete transcript {transcript_id}: {_get_error_message(response)}",
7979
)
@@ -100,7 +100,7 @@ def upload_file(
100100
content=audio_file,
101101
)
102102

103-
if response.status_code != httpx.codes.ok:
103+
if response.status_code != httpx.codes.OK:
104104
raise types.TranscriptError(
105105
f"Failed to upload audio file: {_get_error_message(response)}"
106106
)
@@ -125,7 +125,7 @@ def export_subtitles_srt(
125125
params=params,
126126
)
127127

128-
if response.status_code != httpx.codes.ok:
128+
if response.status_code != httpx.codes.OK:
129129
raise types.TranscriptError(
130130
f"failed to export SRT for transcript {transcript_id}: {_get_error_message(response)}"
131131
)
@@ -150,7 +150,7 @@ def export_subtitles_vtt(
150150
params=params,
151151
)
152152

153-
if response.status_code != httpx.codes.ok:
153+
if response.status_code != httpx.codes.OK:
154154
raise types.TranscriptError(
155155
f"failed to export VTT for transcript {transcript_id}: {_get_error_message(response)}"
156156
)
@@ -172,7 +172,7 @@ def word_search(
172172
),
173173
)
174174

175-
if response.status_code != httpx.codes.ok:
175+
if response.status_code != httpx.codes.OK:
176176
raise types.TranscriptError(
177177
f"failed to search words in transcript {transcript_id}: {_get_error_message(response)}"
178178
)
@@ -223,7 +223,7 @@ def get_sentences(
223223
f"{ENDPOINT_TRANSCRIPT}/{transcript_id}/sentences",
224224
)
225225

226-
if response.status_code != httpx.codes.ok:
226+
if response.status_code != httpx.codes.OK:
227227
raise types.TranscriptError(
228228
f"failed to retrieve sentences for transcript {transcript_id}: {_get_error_message(response)}"
229229
)
@@ -239,7 +239,7 @@ def get_paragraphs(
239239
f"{ENDPOINT_TRANSCRIPT}/{transcript_id}/paragraphs",
240240
)
241241

242-
if response.status_code != httpx.codes.ok:
242+
if response.status_code != httpx.codes.OK:
243243
raise types.TranscriptError(
244244
f"failed to retrieve paragraphs for transcript {transcript_id}: {_get_error_message(response)}"
245245
)
@@ -262,7 +262,7 @@ def list_transcripts(
262262
),
263263
)
264264

265-
if response.status_code != httpx.codes.ok:
265+
if response.status_code != httpx.codes.OK:
266266
raise types.AssemblyAIError(
267267
f"failed to retrieve transcripts: {_get_error_message(response)}"
268268
)
@@ -283,7 +283,7 @@ def lemur_question(
283283
timeout=http_timeout,
284284
)
285285

286-
if response.status_code != httpx.codes.ok:
286+
if response.status_code != httpx.codes.OK:
287287
raise types.LemurError(
288288
f"failed to call Lemur questions: {_get_error_message(response)}"
289289
)
@@ -304,7 +304,7 @@ def lemur_summarize(
304304
timeout=http_timeout,
305305
)
306306

307-
if response.status_code != httpx.codes.ok:
307+
if response.status_code != httpx.codes.OK:
308308
raise types.LemurError(
309309
f"failed to call Lemur summary: {_get_error_message(response)}"
310310
)
@@ -325,7 +325,7 @@ def lemur_action_items(
325325
timeout=http_timeout,
326326
)
327327

328-
if response.status_code != httpx.codes.ok:
328+
if response.status_code != httpx.codes.OK:
329329
raise types.LemurError(
330330
f"failed to call Lemur action items: {_get_error_message(response)}"
331331
)
@@ -346,7 +346,7 @@ def lemur_task(
346346
timeout=http_timeout,
347347
)
348348

349-
if response.status_code != httpx.codes.ok:
349+
if response.status_code != httpx.codes.OK:
350350
raise types.LemurError(
351351
f"failed to call Lemur task: {_get_error_message(response)}"
352352
)
@@ -358,13 +358,13 @@ def lemur_purge_request_data(
358358
client: httpx.Client,
359359
request: types.LemurPurgeRequest,
360360
http_timeout: Optional[float],
361-
) -> types.LemurPurgeRequest:
361+
) -> types.LemurPurgeResponse:
362362
response = client.delete(
363363
f"{ENDPOINT_LEMUR_BASE}/{request.request_id}",
364364
timeout=http_timeout,
365365
)
366366

367-
if response.status_code != httpx.codes.ok:
367+
if response.status_code != httpx.codes.OK:
368368
raise types.LemurError(
369369
f"Failed to purge LeMUR request data for provided request ID: {request.request_id}. Error: {_get_error_message(response)}"
370370
)
@@ -374,7 +374,7 @@ def lemur_purge_request_data(
374374

375375
def lemur_get_response_data(
376376
client: httpx.Client,
377-
request_id: int,
377+
request_id: str,
378378
http_timeout: Optional[float],
379379
) -> Union[
380380
types.LemurStringResponse,
@@ -385,7 +385,7 @@ def lemur_get_response_data(
385385
timeout=http_timeout,
386386
)
387387

388-
if response.status_code != httpx.codes.ok:
388+
if response.status_code != httpx.codes.OK:
389389
raise types.LemurError(
390390
f"Failed to get LeMUR response data for provided request ID: {request_id}. Error: {_get_error_message(response)}"
391391
)
@@ -409,7 +409,7 @@ def create_temporary_token(
409409
timeout=http_timeout,
410410
)
411411

412-
if response.status_code != httpx.codes.ok:
412+
if response.status_code != httpx.codes.OK:
413413
raise types.AssemblyAIError(
414414
f"Failed to create temporary token: {_get_error_message(response)}"
415415
)

assemblyai/lemur.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def __init__(
1212
self,
1313
*,
1414
client: _client.Client,
15-
sources: List[types.LemurSource],
15+
sources: Optional[List[types.LemurSource]],
1616
) -> None:
1717
self._client = client
1818

assemblyai/types.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ class RawTranscriptionConfig(BaseModel):
521521
iab_categories: Optional[bool]
522522
"Enable Topic Detection."
523523

524-
custom_spelling: Optional[List[Dict[str, List[str]]]]
524+
custom_spelling: Optional[List[Dict[str, Union[str, List[str]]]]]
525525
"Customize how words are spelled and formatted using to and from values"
526526

527527
disfluencies: Optional[bool]
@@ -649,10 +649,11 @@ def __init__(
649649
speech_threshold: Reject audio files that contain less than this fraction of speech. Valid values are in the range [0,1] inclusive.
650650
raw_transcription_config: Create the config from a `RawTranscriptionConfig`
651651
"""
652-
self._raw_transcription_config = raw_transcription_config
653-
654-
if raw_transcription_config is None:
655-
self._raw_transcription_config = RawTranscriptionConfig()
652+
self._raw_transcription_config = (
653+
raw_transcription_config
654+
if raw_transcription_config is not None
655+
else RawTranscriptionConfig()
656+
)
656657

657658
# explicit configurations have higher priority if `raw_transcription_config` has been passed as well
658659
self.language_code = language_code
@@ -914,17 +915,21 @@ def iab_categories(self, enable: Optional[bool]) -> None:
914915
self._raw_transcription_config.iab_categories = enable
915916

916917
@property
917-
def custom_spelling(self) -> Optional[Dict[str, List[str]]]:
918+
def custom_spelling(self) -> Optional[Dict[str, Union[str, List[str]]]]:
918919
"Returns the current set custom spellings."
919920

920921
if self._raw_transcription_config.custom_spelling is None:
921922
return None
922923

923924
custom_spellings = {}
924925
for custom_spelling in self._raw_transcription_config.custom_spelling:
925-
custom_spellings[custom_spelling["from"]] = custom_spelling["to"]
926+
_from = custom_spelling["from"]
927+
if isinstance(_from, str):
928+
custom_spellings[_from] = custom_spelling["to"]
929+
else:
930+
raise ValueError("`from` argument must be a string!")
926931

927-
return custom_spellings
932+
return custom_spellings if custom_spelling else None
928933

929934
@property
930935
def disfluencies(self) -> Optional[bool]:
@@ -938,8 +943,6 @@ def disfluencies(self, enable: Optional[bool]) -> None:
938943

939944
self._raw_transcription_config.disfluencies = enable
940945

941-
return self
942-
943946
@property
944947
def sentiment_analysis(self) -> Optional[bool]:
945948
"Returns the status of the Sentiment Analysis feature."
@@ -953,7 +956,7 @@ def sentiment_analysis(self, enable: Optional[bool]) -> None:
953956
self._raw_transcription_config.sentiment_analysis = enable
954957

955958
@property
956-
def auto_chapters(self) -> bool:
959+
def auto_chapters(self) -> Optional[bool]:
957960
"Returns the status of the Auto Chapters feature."
958961

959962
return self._raw_transcription_config.auto_chapters
@@ -971,7 +974,7 @@ def auto_chapters(self, enable: Optional[bool]) -> None:
971974
self._raw_transcription_config.auto_chapters = enable
972975

973976
@property
974-
def entity_detection(self) -> bool:
977+
def entity_detection(self) -> Optional[bool]:
975978
"Returns whether Entity Detection feature is enabled or not."
976979

977980
return self._raw_transcription_config.entity_detection
@@ -1076,7 +1079,7 @@ def set_casing_and_formatting(
10761079

10771080
def set_speaker_diarization(
10781081
self,
1079-
enable: bool = True,
1082+
enable: Optional[bool] = True,
10801083
speakers_expected: Optional[int] = None,
10811084
) -> Self:
10821085
"""
@@ -1261,7 +1264,7 @@ def set_custom_spelling(
12611264

12621265
def set_summarize(
12631266
self,
1264-
enable: bool = True,
1267+
enable: Optional[bool] = True,
12651268
model: Optional[SummarizationModel] = None,
12661269
type: Optional[SummarizationType] = None,
12671270
) -> Self:
@@ -1866,13 +1869,6 @@ def source(self) -> Sourcable:
18661869
"""
18671870
return self._source
18681871

1869-
@property
1870-
def context(self) -> Optional[Union[str, Dict[str, Any]]]:
1871-
"""
1872-
An optional context on the source (can be a string or an arbitrary dictionary)
1873-
"""
1874-
return self._context
1875-
18761872
@property
18771873
def type(self) -> LemurSourceType:
18781874
"""

tests/unit/test_auto_chapters.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ def test_auto_chapters_fails_without_punctuation(httpx_mock: HTTPXMock):
3434
# Check that the error was raised before any requests were made
3535
assert len(httpx_mock.get_requests()) == 0
3636

37-
# Inform httpx_mock that it's okay we didn't make any requests
38-
httpx_mock.reset(False)
39-
4037

4138
def test_auto_chapters_disabled_by_default(httpx_mock: HTTPXMock):
4239
"""

tests/unit/test_content_safety.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,3 @@ def test_content_safety_with_invalid_confidence_threshold(
229229

230230
# Check that the error was raised before any requests were made
231231
assert len(httpx_mock.get_requests()) == 0
232-
233-
# Inform httpx_mock that it's okay we didn't make any requests
234-
httpx_mock.reset(False)

tests/unit/test_redact_pii.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ def test_redact_pii_fails_without_policies(httpx_mock: HTTPXMock):
115115
# Check that the error was raised before any requests were made
116116
assert len(httpx_mock.get_requests()) == 0
117117

118-
# Inform httpx_mock that it's okay we didn't make any requests
119-
httpx_mock.reset(False)
120-
121118

122119
def test_redact_pii_params_excluded_when_disabled(httpx_mock: HTTPXMock):
123120
"""

tests/unit/test_summarization.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ def test_summarization_fails_without_required_field(
3737
# Check that the error was raised before any requests were made
3838
assert len(httpx_mock.get_requests()) == 0
3939

40-
# Inform httpx_mock that it's okay we didn't make any requests
41-
httpx_mock.reset(False)
42-
4340

4441
def test_summarization_disabled_by_default(httpx_mock: HTTPXMock):
4542
"""

0 commit comments

Comments
 (0)