test(telegram): prefer the real PTB library over process-wide mocks - #75615
test(telegram): prefer the real PTB library over process-wide mocks#75615ai-ag2026 wants to merge 1 commit into
Conversation
The gateway conftest (and test_telegram_approval_buttons' file-local copy)
install a permanent MagicMock for the telegram module family whenever the real
library has not been imported yet. Collection order decides: when a gateway
file collects first, every later test needing genuine PTB classes is poisoned
— tests/test_telegram_polling_progress_ptb.py subclasses the real BaseRequest
and fails 6/6 with 'object MagicMock can't be used in await expression'.
Reproducible with just two files:
pytest tests/gateway/test_telegram_approval_buttons.py \
tests/test_telegram_polling_progress_ptb.py
Both _ensure_telegram_mock variants now import the real library first and
install fakes only when PTB is genuinely absent. python-telegram-bot is a
production dependency, so in a normal dev/CI venv tests now always run
against real PTB semantics.
Flip side: the real TelegramError capitalize()s its message where the mock
did not — the token-redaction assertions in test_telegram_rich_messages now
compare case-insensitively (the property under test is the redaction, not
the URL casing).
Full telegram test set (56 files): 524 passed, 0 failed; the two-file
reproduction above goes 6-failed -> all-passed.
|
Thanks for isolating the PTB collection-order failure. Current main still has the underlying cross-file state hazard: The changed rich-message assertions continue to assert both absence of the raw token and presence of the redacted token ( No blocking defect found in the reviewed diff. This is an automated hermes-sweeper review. |
Summary
tests/gateway/conftest.py(and the file-local copy intest_telegram_approval_buttons.py) install a permanent, process-wide MagicMock for thetelegrammodule family whenever the real library hasn't been imported yet. Collection order decides the outcome: when any gateway file collects first, every later-collected test that needs genuine PTB classes is poisoned. Concretely,tests/test_telegram_polling_progress_ptb.pysubclasses the realBaseRequestand fails 6/6 withobject MagicMock can't be used in 'await' expression:Both
_ensure_telegram_mockvariants now try to import the real library first and install fakes only when PTB is genuinely absent. Sincepython-telegram-botis a production dependency, a normal dev/CI venv now always tests against real PTB semantics; the mock remains as a fallback for PTB-less environments.Flip side (included)
Real
TelegramErrorcapitalize()s its message where the mock did not. The bot-token-redaction assertions intest_telegram_rich_messages.pynow compare case-insensitively — the property under test is the token redaction (bot123456789:***), not the URL casing.Validation
Notes
Found by bisecting a 6-test order-coupling; the mock's own guard ("skips when the real library is already imported") shows real-PTB runs were always intended to be valid — this just makes them the default instead of an accident of import order.