fix: import Tencent SDK lazily so other engines are not broken by incompatible SDK - #1187
Open
jabrailkhalil wants to merge 2 commits into
Open
jabrailkhalil wants to merge 2 commits into
jabrailkhalil wants to merge 2 commits into
Conversation
…ompatible SDK The Tencent TMT bindings (TextTranslateRequest/TextTranslateResponse) were imported at module load. tencentcloud-sdk-python-tmt releases newer than 3.1 removed these classes, so any environment with such a version installed (or with the SDK missing) crashed on import regardless of the selected translation engine, e.g.: ImportError: cannot import name 'TextTranslateRequest' from 'tencentcloud.tmt.v20180321.models' Move the import into the TencentTranslator constructor and, when the SDK is missing or incompatible, raise an informative ImportError that points to encentcloud-sdk-python-tmt<3.1`. Other engines now initialize and translate without the Tencent SDK, and the module imports even when it is missing. Adds regression tests covering both behaviours. Fixes PDFMathTranslate#1167 Signed-off-by: jabrailkhalil <jabrailkhalil@gmail.com>
Recommend tencentcloud-sdk-python-tmt==3.1.70, matching pyproject.toml, instead of an incompatible <3.1 requirement. Avoid claiming that all 3.1.x SDK releases removed the TextTranslate API. Assert the exact recovery pin and preserve the original import error. Restore the translator module namespace after the lazy-import reload test to avoid leaking replacement classes into subsequent tests. Signed-off-by: jabrailkhalil <jabrailkhalil@gmail.com>
Author
|
The CI workflows for the current head |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pdf2zh/translator.pypreviously imported the Tencent TMT bindings at module load. A missing or incompatible Tencent SDK could therefore prevent every translation engine from loading, even when Tencent was not selected, as reported in #1167.This PR imports the SDK on demand from
TencentTranslator.__init__. Import failures are chained into an informativeImportErrorwithout blocking unrelated engines.The recovery command recommends
tencentcloud-sdk-python-tmt==3.1.70, matching the existing pin inpyproject.toml. It does not assume that every 3.1.x release lacks the TextTranslate API. The dependency pin itself is unchanged.Regression coverage
test/test_translator.pychecks that:ModuleNotFoundErroras its cause.Validation
Initial PR validation, reported before the follow-up commit:
python -m pytest test/test_translator.py -q: 11 passed.flake8 --ignore E203,E261,E501,W503,E741: clean.Follow-up validation on September 10, 2026:
Fixes #1167