Skip to content

fix: import Tencent SDK lazily so other engines are not broken by incompatible SDK - #1187

Open
jabrailkhalil wants to merge 2 commits into
PDFMathTranslate:mainfrom
jabrailkhalil:fix/lazy-tencent-import
Open

jabrailkhalil wants to merge 2 commits into
PDFMathTranslate:mainfrom
jabrailkhalil:fix/lazy-tencent-import

Conversation

@jabrailkhalil

@jabrailkhalil jabrailkhalil commented Sep 9, 2026 •

Copy link
Copy Markdown

Summary

pdf2zh/translator.py previously 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 informative ImportError without blocking unrelated engines.

The recovery command recommends tencentcloud-sdk-python-tmt==3.1.70, matching the existing pin in pyproject.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.py checks that:

  • The translator module can reload while all Tencent SDK imports are blocked. The original module namespace is restored afterward to avoid leaking replacement classes into other tests.
  • Selecting Tencent with a missing SDK raises an informative error containing the exact recovery pin and preserves the original ModuleNotFoundError as its cause.

Validation

Initial PR validation, reported before the follow-up commit:

  • python -m pytest test/test_translator.py -q: 11 passed.
  • Black check: 2 files unchanged.
  • flake8 --ignore E203,E261,E501,W503,E741: clean.
  • The full suite was not run because document-layout dependencies were unavailable in that environment.

Follow-up validation on September 10, 2026:

  • Three standalone unit checks of the updated import helper passed: missing SDK, incompatible SDK, and successful imports using mocked SDK bindings.
  • The complete follow-up diff was inspected before updating the branch; changes are limited to the recovery diagnostic and lazy-import tests.
  • The project test suite and linters were not rerun for the follow-up: the test environment could not resolve GitHub to obtain a checkout/dependencies. The standalone checks above are not a replacement for upstream CI.

Fixes #1167

jabrailkhalil and others added 2 commits September 9, 2026 23:57
…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>
@jabrailkhalil

Copy link
Copy Markdown
Author

The CI workflows for the current head 87ad125a6d are waiting for contributor workflow approval (action_required): Format Code with Black, Test and Build Python Package. Could a maintainer approve the pending runs when convenient? These runs have not executed their checks yet; I will address any failures once they run.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tencentcloud-sdk-python-tmt unpinned dependency breaks import (TextTranslateRequest removed in 3.1.x)

1 participant