Skip to content

Commit 9ad2330

Browse files
hive-botclaude
andcommitted
fix: raise DeepLException when the usage response is not a JSON object
get_usage() replaced a non-object JSON body with {} and returned a Usage whose values were all invalid. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8bc53bf commit 9ad2330

4 files changed

Lines changed: 27 additions & 11 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
311311
`TypeError` or `AttributeError` when the API returns a malformed response.
312312
They now raise `DeepLException`. `list_translation_memories()` also raises
313313
`DeepLException` for a non-JSON response instead of returning an empty list.
314+
- Fixed `get_usage()` returning a `Usage` with every value invalid when the
315+
response body is not a JSON object. It now raises `DeepLException`.
314316

315317
## [1.32.0] - 2026-08-12
316318
### Added

‎deepl/translator.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ def get_usage(self) -> Usage:
11961196
self._raise_for_status(status, content, json)
11971197

11981198
if not isinstance(json, dict):
1199-
json = {}
1199+
raise DeepLException("Usage response is not a JSON object")
12001200
return Usage(json)
12011201

12021202
def create_glossary(

‎tests/offline/test_translator_options.py‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,20 @@ def test_warns_about_deprecated_user_agent_global(monkeypatch):
9292

9393

9494
@pytest.mark.parametrize(
95-
"make_client, path, call",
95+
"make_client, path, body, call",
9696
[
97-
(make_translator, "v2/usage", lambda c: c.get_usage()),
98-
(make_deepl_client, "v2/usage", lambda c: c.get_usage()),
97+
(make_translator, "v2/usage", {}, lambda c: c.get_usage()),
98+
(make_deepl_client, "v2/usage", {}, lambda c: c.get_usage()),
9999
(
100100
make_admin_client,
101101
"v2/admin/developer-keys",
102+
[],
102103
lambda c: c.list_developer_keys(),
103104
),
104105
],
105106
)
106-
def test_sends_headers_option(mocked, make_client, path, call):
107-
mocked.get(url(path), json=[])
107+
def test_sends_headers_option(mocked, make_client, path, body, call):
108+
mocked.get(url(path), json=body)
108109
client = make_client(
109110
headers={
110111
"X-Custom": "value",
@@ -122,23 +123,24 @@ def test_sends_headers_option(mocked, make_client, path, call):
122123

123124

124125
@pytest.mark.parametrize(
125-
"make_client, path, call",
126+
"make_client, path, body, call",
126127
[
127-
(make_translator, "v2/usage", lambda c: c.get_usage()),
128-
(make_deepl_client, "v2/usage", lambda c: c.get_usage()),
128+
(make_translator, "v2/usage", {}, lambda c: c.get_usage()),
129+
(make_deepl_client, "v2/usage", {}, lambda c: c.get_usage()),
129130
(
130131
make_admin_client,
131132
"v2/admin/developer-keys",
133+
[],
132134
lambda c: c.list_developer_keys(),
133135
),
134136
],
135137
)
136138
def test_appends_app_info_option_to_user_agent(
137-
mocked, make_client, path, call
139+
mocked, make_client, path, body, call
138140
):
139141
mocked.get(
140142
url(path),
141-
json=[],
143+
json=body,
142144
match=[
143145
matchers.header_matcher(
144146
{"User-Agent": f"{LIBRARY_USER_AGENT} my_plugin/1.0"}

‎tests/offline/test_usage.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import pytest
88

9+
import deepl
10+
911
from .client import make_translator, url
1012
from .matchers import bodyless_request
1113

@@ -190,6 +192,16 @@ def test_skips_non_object_product_entries(mocked):
190192
] == [("write", 3)]
191193

192194

195+
@pytest.mark.parametrize("body", [[], "usage", 5])
196+
def test_rejects_non_object_usage_response(mocked, body):
197+
mocked.get(url("v2/usage"), json=body)
198+
199+
with pytest.raises(
200+
deepl.DeepLException, match="^Usage response is not a JSON object$"
201+
):
202+
make_translator().get_usage()
203+
204+
193205
def test_ignores_unparseable_billing_period(mocked):
194206
mocked.get(
195207
url("v2/usage"),

0 commit comments

Comments
 (0)