Skip to content

Commit

Permalink
Improve error message if translate_text_with_glossary is called witho…
Browse files Browse the repository at this point in the history
…ut an instance of GlossaryInfo
  • Loading branch information
daniel-jones-dev committed Apr 11, 2022
1 parent a91f1dc commit b73dec8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
* Add `error_message` property to `DocumentStatus`, describing the error in case of document translation failure.
### Changed
* Improve error message if `translate_text_with_glossary` is called without an instance of `GlossaryInfo`.
### Deprecated
### Removed
### Fixed
Expand Down
11 changes: 11 additions & 0 deletions deepl/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,11 741,22 @@ def translate_text_with_glossary(
:type text: UTF-8 :class:`str`; string sequence (list, tuple, iterator,
generator)
:param glossary: glossary to use for translation.
:type glossary: :class:`GlossaryInfo`.
:param target_lang: override target language of glossary.
:return: List of TextResult objects containing results, unless input
text was one string, then a single TextResult object is returned.
"""

if not isinstance(glossary, GlossaryInfo):
msg = (
"This function expects the glossary parameter to be an "
"instance of GlossaryInfo. Use get_glossary() to obtain a "
"GlossaryInfo using the glossary ID of an existing "
"glossary. Alternatively, use translate_text() and "
"specify the glossary ID using the glossary parameter. "
)
raise ValueError(msg)

if target_lang is None:
target_lang = glossary.target_lang
if target_lang == "EN":
Expand Down
6 changes: 6 additions & 0 deletions tests/test_glossary.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 259,9 @@ def test_glossary_translate_text_invalid(translator, glossary_manager):
target_lang="EN",
glossary=glossary_deen.glossary_id,
)

with pytest.raises(ValueError, match="GlossaryInfo"):
translator.translate_text_with_glossary(
text,
glossary=glossary_deen.glossary_id,
)

0 comments on commit b73dec8

Please sign in to comment.