Skip to content

Commit bbdcf96

Browse files
Remove language cache and glossary language check
1 parent dc17787 commit bbdcf96

File tree

3 files changed

+42
-73
lines changed

3 files changed

+42
-73
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
* Add `Translator.get_glossary_languages()` to query language pairs supported for glossaries.
1111
### Changed
12+
* Internal language caching and client-side checking of language codes are removed.
1213
### Deprecated
14+
* Some optional arguments related to language caching are now deprecated, and will be removed in a future version:
15+
* `Translator()`: the `skip_language_check` argument
16+
* `Translator.get_source_languages()` and `Translator.get_target_languages()`: the `skip_cache` argument
1317
### Removed
1418
### Fixed
1519
* Fix HTTP request retries for document uploads.

deepl/translator.py

Lines changed: 36 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -302,21 +302,6 @@ def remove_regional_variant(language: Union[str]) -> str:
302302
"""Removes the regional variant from a language, e.g. EN-US gives EN"""
303303
return str(language).upper()[0:2]
304304

305-
@staticmethod
306-
def check_supported_glossary_languages(source_lang: str, target_lang: str):
307-
if f"{source_lang}>{target_lang}" not in [
308-
"DE>EN",
309-
"EN>DE",
310-
"EN>ES",
311-
"EN>FR",
312-
"ES>EN",
313-
"FR>EN",
314-
]:
315-
raise DeepLException(
316-
"Invalid source or target language, glossaries are supported "
317-
"for the following language pairs: EN<->DE, EN<->FR, EN<->ES"
318-
)
319-
320305

321306
class GlossaryLanguagePair:
322307
"""Information about a pair of languages supported for DeepL glossaries.
@@ -380,8 +365,9 @@ class Translator:
380365
:param auth_key: Authentication key as found in your DeepL API account.
381366
:param server_url: (Optional) Base URL of DeepL API, can be overridden e.g.
382367
for testing purposes.
383-
:param skip_language_check: (Optional) Set to True to override automatic
384-
request of available languages.
368+
:param skip_language_check: Deprecated, and now has no effect as the
369+
corresponding internal functionality has been removed. This parameter
370+
will be removed in a future version.
385371
386372
All functions may raise DeepLException or a subclass if a connection error
387373
occurs.
@@ -415,10 +401,6 @@ def __init__(
415401
self._client = http_client.HttpClient()
416402
self.headers = {"Authorization": f"DeepL-Auth-Key {auth_key}"}
417403

418-
self._skip_language_check = skip_language_check
419-
self._source_languages_cached = None
420-
self._target_languages_cached = None
421-
422404
def __del__(self):
423405
self.close()
424406

@@ -515,26 +497,10 @@ def _raise_for_status(
515497
f"content: {content}."
516498
)
517499

518-
def _request_languages(self, target: bool) -> List[Language]:
519-
"""Internal function to make a /languages request and cache the result."""
520-
data = {"type": "target"} if target else {}
521-
status, content, json = self._api_call("v2/languages", data=data)
522-
523-
self._raise_for_status(status, content, json)
524-
525-
return [
526-
Language(
527-
language["language"],
528-
language["name"],
529-
language.get("supports_formality", None),
530-
)
531-
for language in json
532-
]
533-
534500
def _check_valid_languages(
535501
self, source_lang: Optional[str], target_lang: str
536502
):
537-
"""Internal function to check given languages match available languages."""
503+
"""Internal function to check given languages are valid."""
538504
if target_lang == "EN":
539505
raise DeepLException(
540506
'target_lang="EN" is deprecated, please use "EN-GB" or "EN-US" instead.'
@@ -544,24 +510,6 @@ def _check_valid_languages(
544510
'target_lang="PT" is deprecated, please use "PT-PT" or "PT-BR" instead.'
545511
)
546512

547-
if self._skip_language_check:
548-
return
549-
550-
if source_lang is not None and not any(
551-
source_lang == lang.code for lang in self.get_source_languages()
552-
):
553-
raise DeepLException(
554-
f"source_lang ({source_lang}) must be one of the supported "
555-
"language codes, or None for auto-detection"
556-
)
557-
558-
if not any(
559-
target_lang == lang.code for lang in self.get_target_languages()
560-
):
561-
raise DeepLException(
562-
f"target_lang ({target_lang}) must be one of the supported language codes"
563-
)
564-
565513
def _check_language_and_formality(
566514
self,
567515
source_lang: Union[str, Language, None],
@@ -586,10 +534,6 @@ def _check_language_and_formality(
586534
raise ValueError(
587535
"source_lang and target_lang must match glossary"
588536
)
589-
elif glossary is not None:
590-
Language.check_supported_glossary_languages(
591-
source_lang, Language.remove_regional_variant(target_lang)
592-
)
593537

594538
self._check_valid_languages(source_lang, target_lang)
595539

@@ -944,20 +888,42 @@ def translate_document_download(
944888
return response
945889

946890
def get_source_languages(self, skip_cache=False) -> List[Language]:
947-
"""Request the list of available source languages."""
948-
if self._source_languages_cached is None or skip_cache:
949-
self._source_languages_cached = self._request_languages(
950-
target=False
891+
"""Request the list of available source languages.
892+
893+
:param skip_cache: Deprecated, and now has no effect as the
894+
corresponding internal functionality has been removed. This
895+
parameter will be removed in a future version.
896+
:return: List of supported source languages.
897+
"""
898+
status, content, json = self._api_call("v2/languages")
899+
self._raise_for_status(status, content, json)
900+
return [
901+
Language(
902+
language["language"],
903+
language["name"],
951904
)
952-
return self._source_languages_cached
905+
for language in json
906+
]
953907

954908
def get_target_languages(self, skip_cache=False) -> List[Language]:
955-
"""Request the list of available target languages."""
956-
if self._target_languages_cached is None or skip_cache:
957-
self._target_languages_cached = self._request_languages(
958-
target=True
909+
"""Request the list of available target languages.
910+
911+
:param skip_cache: Deprecated, and now has no effect as the
912+
corresponding internal functionality has been removed. This
913+
parameter will be removed in a future version.
914+
:return: List of supported target languages.
915+
"""
916+
data = {"type": "target"}
917+
status, content, json = self._api_call("v2/languages", data=data)
918+
self._raise_for_status(status, content, json)
919+
return [
920+
Language(
921+
language["language"],
922+
language["name"],
923+
language.get("supports_formality", None),
959924
)
960-
return self._target_languages_cached
925+
for language in json
926+
]
961927

962928
def get_glossary_languages(self) -> List[GlossaryLanguagePair]:
963929
"""Request the list of language pairs supported for glossaries."""
@@ -1013,7 +979,6 @@ def create_glossary(
1013979
# glossaries are only supported for base language types
1014980
target_lang = Language.remove_regional_variant(target_lang)
1015981
source_lang = Language.remove_regional_variant(source_lang)
1016-
Language.check_supported_glossary_languages(source_lang, target_lang)
1017982

1018983
if not name:
1019984
raise ValueError("glossary name must not be empty")

tests/test_translate_text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ def check_result(result):
9797
def test_invalid_language(translator):
9898
with pytest.raises(
9999
deepl.DeepLException,
100-
match="target_lang.*must be one of the supported language codes",
100+
match="target_lang.*not supported",
101101
):
102102
translator.translate_text(example_text["EN"], target_lang="XX")
103103

104104
with pytest.raises(
105105
deepl.DeepLException,
106-
match="source_lang.*must be one of the supported language codes",
106+
match="source_lang.*not supported",
107107
):
108108
translator.translate_text(
109109
example_text["EN"], source_lang="XX", target_lang="DE"

0 commit comments

Comments
 (0)