Skip to content

Commit b73dec8

Browse files
Improve error message if translate_text_with_glossary is called without an instance of GlossaryInfo
1 parent a91f1dc commit b73dec8

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
* Add `error_message` property to `DocumentStatus`, describing the error in case of document translation failure.
1111
### Changed
12+
* Improve error message if `translate_text_with_glossary` is called without an instance of `GlossaryInfo`.
1213
### Deprecated
1314
### Removed
1415
### Fixed

deepl/translator.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,11 +741,22 @@ def translate_text_with_glossary(
741741
:type text: UTF-8 :class:`str`; string sequence (list, tuple, iterator,
742742
generator)
743743
:param glossary: glossary to use for translation.
744+
:type glossary: :class:`GlossaryInfo`.
744745
:param target_lang: override target language of glossary.
745746
:return: List of TextResult objects containing results, unless input
746747
text was one string, then a single TextResult object is returned.
747748
"""
748749

750+
if not isinstance(glossary, GlossaryInfo):
751+
msg = (
752+
"This function expects the glossary parameter to be an "
753+
"instance of GlossaryInfo. Use get_glossary() to obtain a "
754+
"GlossaryInfo using the glossary ID of an existing "
755+
"glossary. Alternatively, use translate_text() and "
756+
"specify the glossary ID using the glossary parameter. "
757+
)
758+
raise ValueError(msg)
759+
749760
if target_lang is None:
750761
target_lang = glossary.target_lang
751762
if target_lang == "EN":

tests/test_glossary.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,9 @@ def test_glossary_translate_text_invalid(translator, glossary_manager):
259259
target_lang="EN",
260260
glossary=glossary_deen.glossary_id,
261261
)
262+
263+
with pytest.raises(ValueError, match="GlossaryInfo"):
264+
translator.translate_text_with_glossary(
265+
text,
266+
glossary=glossary_deen.glossary_id,
267+
)

0 commit comments

Comments
 (0)