Skip to content

Commit 1fd3140

Browse files
Add optional filename argument to translate_document_upload() to fix uploading file content as string or bytes
1 parent 71c0857 commit 1fd3140

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
### Deprecated
1515
### Removed
1616
### Fixed
17+
* Add optional filename argument to `translate_document_upload()` to fix uploading file content as string or bytes.
1718
### Security
1819

1920

deepl/translator.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ def translate_document_upload(
847847
target_lang: str,
848848
formality: Union[str, Formality] = Formality.DEFAULT,
849849
glossary: Union[str, GlossaryInfo, None] = None,
850+
filename: Optional[str] = None,
850851
) -> DocumentHandle:
851852
"""Upload document to be translated and return handle associated with
852853
request.
@@ -862,14 +863,24 @@ def translate_document_upload(
862863
Formality enum, "less" or "more".
863864
:param glossary: (Optional) glossary or glossary ID to use for
864865
translation. Must match specified source_lang and target_lang.
866+
:param filename: (Optional) Filename including extension, only required
867+
if uploading string or bytes containing file content.
865868
:return: DocumentHandle with ID and key identifying document.
866869
"""
867870

868871
request_data = self._check_language_and_formality(
869872
source_lang, target_lang, formality, glossary
870873
)
871874

872-
files = {"file": input_document}
875+
if isinstance(input_document, (str, bytes)):
876+
if filename is None:
877+
raise ValueError(
878+
"filename is required if uploading file content as string "
879+
"or bytes"
880+
)
881+
files = {"file": (filename, input_document)}
882+
else:
883+
files = {"file": input_document}
873884
status, content, json = self._api_call(
874885
"v2/document", data=request_data, files=files
875886
)

tests/test_translate_document.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,32 @@ def test_translate_document_low_level(
180180
assert output_document_path.read_text() == example_document_translation
181181

182182

183+
def test_translate_document_string(translator, server):
184+
input_string = example_text["EN"]
185+
handle = translator.translate_document_upload(
186+
input_string,
187+
source_lang="EN",
188+
target_lang="DE",
189+
filename="test.txt",
190+
)
191+
192+
status = translator.translate_document_get_status(handle)
193+
while status.ok and not status.done:
194+
status = translator.translate_document_get_status(handle)
195+
time.sleep(status.seconds_remaining or 1)
196+
197+
assert status.ok
198+
response = translator.translate_document_download(handle)
199+
try:
200+
output = bytes()
201+
for chunk in response.iter_content(chunk_size=128):
202+
output += chunk
203+
output_string = output.decode()
204+
assert output_string == example_text["DE"]
205+
finally:
206+
response.close()
207+
208+
183209
@needs_mock_server
184210
def test_translate_document_request_fields(
185211
translator, example_document_path, server, output_document_path

0 commit comments

Comments
 (0)