fix(testing): initialize BidiComponentManager in AppTest mock runtime#14301
Conversation
AppTest._run() creates a MagicMock runtime but does not set up bidi_component_registry, causing apps that use st.components.v2 custom components to crash with TypeError during testing. Initialize a real BidiComponentManager on the mock runtime so that component registration and lookup work correctly in AppTest. Fixes streamlit#14274
|
Thanks for contributing to Streamlit! 🎈 Please make sure you have read our Contributing Guide. You can find additional information about Streamlit development in the wiki. The review process:
We're receiving many contributions and have limited review bandwidth — please expect some delay. We appreciate your patience! 🙏 |
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
There was a problem hiding this comment.
Pull request overview
This PR fixes a crash in streamlit.testing.v1.AppTest when running apps that use st.components.v2 custom components by ensuring the mock Runtime includes a real v2 component manager.
Changes:
- Initialize
mock_runtime.bidi_component_registrywith a realBidiComponentManagerduringAppTest._run(). - Add a regression test that registers and mounts a v2 custom component via
AppTest.from_function()(issue #14274).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| lib/streamlit/testing/v1/app_test.py | Seeds the AppTest mock Runtime with a real BidiComponentManager so v2 component registration/lookup works correctly. |
| lib/tests/streamlit/testing/app_test_test.py | Adds a regression test ensuring v2 custom components can be registered and mounted in AppTest without exceptions. |
SummaryThis PR fixes a crash when using The fix adds a single line initializing Files changed:
Code QualityConsensus (3/3 reviewers agree): The change is minimal, surgical, and follows established patterns exactly. The mock runtime setup in Test CoverageConsensus (3/3 reviewers agree): The new
The test follows existing patterns (uses Backwards CompatibilityConsensus (3/3 reviewers agree): No breaking changes. This is purely additive — it fixes a crash for a previously non-functional scenario. Existing Security & RiskConsensus (3/3 reviewers agree): No security concerns. External test recommendation
AccessibilityNo frontend changes are included in this PR. No accessibility concerns. Recommendations
Reviewer Agreement
No disagreements or conflicts between reviewers. Minor differences were in optional recommendations only. Missing ReviewsAll expected models completed their reviews:
VerdictAPPROVED: All three reviewers unanimously approve. The fix is correct, minimal, and well-tested. It follows established patterns, introduces no risk, and includes a solid regression test. The only recommendation (non-blocking) is adding a This is a consolidated AI review by 📋 Review by `gemini-3.1-pro`SummaryThis PR fixes a bug in Code QualityThe code changes are clean, concise, and directly address the root cause of the issue. The initialization of Test CoverageThe PR includes a new unit test Backwards CompatibilityThis is a bug fix in the testing framework ( Security & RiskNo security concerns or regression risks identified. The changes are isolated to the testing framework's mock runtime setup. External test recommendation
AccessibilityN/A - No frontend changes. RecommendationsNone. The fix is well-implemented and adequately tested. VerdictAPPROVED: The PR successfully fixes a crash in This is an automated AI review by 📋 Review by `gpt-5.3-codex-high`SummaryThis PR fixes an Code QualityThe implementation is small, targeted, and follows existing patterns in the testing runtime setup.
Test CoverageCoverage is appropriate for this bug fix.
Backwards CompatibilityNo breaking changes identified.
Security & RiskNo security concerns found.
External test recommendation
AccessibilityNo frontend/UI code changed, so there are no new accessibility impacts in this PR. Recommendations
VerdictAPPROVED: The fix is correct, low risk, and sufficiently covered by a targeted regression test. This is an automated AI review by 📋 Review by `opus-4.6-thinking`SummaryThis PR fixes a crash when using The fix is a single line that initializes Files changed:
Code QualityThe change is minimal, surgical, and follows the established pattern exactly. The mock runtime setup in The import of Minor note: The new test function Test CoverageThe new test
The test follows existing patterns in the file (uses The test adequately covers the fix. No additional unit tests are needed since the fix is a single-line initialization. Backwards CompatibilityNo backwards compatibility concerns. This is purely additive — it fixes a crash when v2 custom components are used with Security & RiskNo security concerns:
Regression risk: Very low. The External test recommendation
AccessibilityNo frontend changes are included in this PR. No accessibility concerns. Recommendations
VerdictAPPROVED: Clean, minimal, and well-tested fix for a real crash when using v2 custom components in This is an automated AI review by |
lukasmasuch
left a comment
There was a problem hiding this comment.
LGTM 👍 Thanks for the contribution
Summary
AppTest._run()creates aMagicMock(spec=Runtime)but does not configurebidi_component_registry, so any app that usesst.components.v2custom components crashes with:The crash occurs because
get_bidi_component_manager()returns a MagicMock attribute instead of a realBidiComponentManager. Component registration silently no-ops on the mock, and later, when_bidi_component()tries to set protobuf string fields from MagicMock objects, the built-in protobuf C code raises the TypeError.Fix
Initialize a real
BidiComponentManagerinstance on the mock runtime, alongside the existingMediaFileManagerandMemoryCacheStorageManager:This ensures component registration and lookup work correctly during
AppTestruns.Test
Added
test_v2_custom_componentthat registers and mounts a v2 custom component insideAppTest.from_function(), verifying no exception is raised.Fixes #14274