Skip to content

Commit d3eb0f8

Browse files
hive-botclaude
andcommitted
feat: accept content in upload_translation_memory_file() and deprecate input_file
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent daab05f commit d3eb0f8

4 files changed

Lines changed: 60 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
143143
`expired`, and the plain string for any other value. Members are strings
144144
that compare equal to the API value, so existing comparisons with strings
145145
and the `TranslationMemoryJobResult.STATUS_*` constants keep working.
146+
- Added the `content` parameter name to `upload_translation_memory_file()`
147+
for the file content, replacing `input_file`. Positional calls are
148+
unaffected.
146149

147150
### Changed
148151
- HTTP 404 responses raise `NotFoundException` instead of `DeepLException`
@@ -214,6 +217,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
214217
- The argument names `glossary_dicts`, `glossary_dict`, `csv_data` and
215218
`glossary_id` of the multilingual glossary functions are deprecated, use
216219
`dictionaries`, `dictionary`, `csv` and `glossary`.
220+
- The `input_file` keyword of `upload_translation_memory_file()` is
221+
deprecated in favour of `content` and will be removed in the next major
222+
version. Passing it emits a `DeprecationWarning`.
217223

218224
### Fixed
219225
- HTTP 401 responses now raise `AuthorizationException`, like 403. Before they

‎README.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,12 @@ an upload URL that the file must be uploaded to before processing starts, then
12351235
`create_translation_memory_import()` takes the file name and size, and the
12361236
optional keyword arguments `content_type`, `display_name` and
12371237
`extra_body_parameters`, with the same meaning as above.
1238+
`upload_translation_memory_file()` takes the import job (or its upload URL),
1239+
the file `content` as bytes or a binary file object, and an optional
1240+
`content_type` (default `application/xml`) that must match the one declared
1241+
when creating the job. The keyword `input_file` is a deprecated alias of
1242+
`content`: it emits a `DeprecationWarning` and will be removed in the next
1243+
major version.
12381244

12391245
```python
12401246
created = deepl_client.create_translation_memory_import(
@@ -1243,7 +1249,7 @@ created = deepl_client.create_translation_memory_import(
12431249
display_name="Legal TM",
12441250
)
12451251
with open("/path/to/legal.tmx", "rb") as input_file:
1246-
deepl_client.upload_translation_memory_file(created, input_file)
1252+
deepl_client.upload_translation_memory_file(created, content=input_file)
12471253

12481254
# The job reports "awaiting_input" both before the upload and for a while
12491255
# afterwards, until the API detects it; this polls through that status.

‎deepl/deepl_client.py‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,19 +2068,40 @@ def create_translation_memory_import(
20682068
def upload_translation_memory_file(
20692069
self,
20702070
translation_memory_import: Union[str, TranslationMemoryImport],
2071-
input_file: Union[BinaryIO, bytes, Any],
2071+
content: Union[BinaryIO, bytes, Any] = None,
20722072
content_type: str = "application/xml",
2073+
*,
2074+
input_file: Union[BinaryIO, bytes, Any] = None,
20732075
) -> None:
20742076
"""Uploads a TMX file to the upload URL of an import job, which starts
20752077
processing.
20762078
20772079
:param translation_memory_import: TranslationMemoryImport returned by
20782080
create_translation_memory_import(), or its upload URL.
2079-
:param input_file: TMX file content as bytes or a file-like object
2081+
:param content: TMX file content as bytes or a file-like object
20802082
opened in binary mode.
20812083
:param content_type: (Optional) MIME type of the file. Must match the
20822084
content_type declared when the import job was created.
2085+
:param input_file: Deprecated alias of content.
2086+
2087+
.. deprecated::
2088+
Use content instead. input_file will be removed in the next
2089+
major version.
20832090
"""
2091+
if input_file is not None:
2092+
if content is not None:
2093+
raise ValueError("pass either content or input_file, not both")
2094+
warnings.warn(
2095+
"input_file is deprecated, use content",
2096+
DeprecationWarning,
2097+
stacklevel=2,
2098+
)
2099+
content = input_file
2100+
if content is None:
2101+
raise TypeError(
2102+
"upload_translation_memory_file() missing required argument: "
2103+
"'content'"
2104+
)
20842105
if isinstance(translation_memory_import, TranslationMemoryImport):
20852106
upload_url = translation_memory_import.upload_url
20862107
else:
@@ -2091,17 +2112,17 @@ def upload_translation_memory_file(
20912112
# Read a file-like object into bytes up front. The request is prepared
20922113
# once and reused across retries, so a stream body would already be at
20932114
# EOF on the second attempt and upload nothing.
2094-
if hasattr(input_file, "read"):
2095-
input_file = input_file.read()
2115+
if hasattr(content, "read"):
2116+
content = content.read()
20962117

2097-
status_code, content = self._asset_call(
2118+
status_code, response_content = self._asset_call(
20982119
"PUT",
20992120
upload_url,
2100-
data=input_file,
2121+
data=content,
21012122
headers={"Content-Type": content_type},
21022123
)
21032124
self._raise_for_asset_status(
2104-
status_code, content, "uploading translation memory file"
2125+
status_code, response_content, "uploading translation memory file"
21052126
)
21062127

21072128
def create_translation_memory_export(

‎tests/offline/test_translation_memories.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,28 @@ def test_sends_given_content_type_when_uploading_file(mocked):
329329
)
330330

331331
make_deepl_client().upload_translation_memory_file(
332-
UPLOAD_URL, b"<tmx/>", content_type="text/xml"
332+
UPLOAD_URL, content=b"<tmx/>", content_type="text/xml"
333333
)
334334

335335

336+
def test_warns_when_uploading_file_with_input_file_keyword(mocked):
337+
mocked.put(UPLOAD_URL, match=storage_request("application/xml", "<tmx/>"))
338+
339+
with pytest.warns(
340+
DeprecationWarning, match="input_file is deprecated, use content"
341+
):
342+
make_deepl_client().upload_translation_memory_file(
343+
UPLOAD_URL, input_file=b"<tmx/>"
344+
)
345+
346+
347+
def test_raises_when_uploading_file_with_content_and_input_file():
348+
with pytest.raises(ValueError, match="either content or input_file"):
349+
make_deepl_client().upload_translation_memory_file(
350+
UPLOAD_URL, b"a", input_file=b"b"
351+
)
352+
353+
336354
def test_raises_when_uploading_file_without_url():
337355
with pytest.raises(ValueError, match="upload_url must not be empty"):
338356
make_deepl_client().upload_translation_memory_file("", b"x")

0 commit comments

Comments
 (0)