perf: import package re-exports on first use - #724
Conversation
The web interface's API blueprint imports src.common.sync_manager only for STATUS_FILE and SYNC_PORT, which loaded numpy into the web process. numpy is used in one place, the leader's send_frame, so import it there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uc5DAbSwGGUTm3MrCtpC2m
src/common/__init__.py and src/plugin_system/__init__.py imported every re-exported helper eagerly, so any submodule import -- the web interface's path_safety, store_manager, schema_manager -- also loaded ScrollHelper (numpy), LogoHelper, APIHelper, the adaptive layout helpers (freetype) and PluginManager. Resolve those names through a PEP 562 module __getattr__ instead, with __dir__, an unchanged __all__ and TYPE_CHECKING imports so mypy still sees the real types. Each name resolves to the same object as before, and is cached in the module namespace on first access. With the sync_manager change, importing web_interface.app on a Pi 4 drops from ~67 MB to ~54 MB RSS and no longer loads numpy. The display process ends up with the same modules loaded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uc5DAbSwGGUTm3MrCtpC2m
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 9 minutes. View limit detailsLimit details: You’ve used the included review currently available. Review configuration: ⚙️ Run configuration
📒 Files selected for processing (6)
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (6)
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthrough
ChangesLazy imports
Priority: ➖ Normal Estimated code review effort: 3 (Moderate) | ~20 minutes Change: Refactor Sequence Diagram(s)sequenceDiagram
participant Caller
participant Package as src.plugin_system
participant Importlib
participant ExportModule as mapped module
Caller->>Package: access BasePlugin
Package->>Importlib: import mapped module
Importlib->>ExportModule: load module
Package->>Caller: return cached BasePlugin
Merge Risk: ⚪ Minimal · up to The lazy exports and deferred NumPy import appear ready to merge after normal checks; hardware testing remains outstanding. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The exported objects and frame protocol are preserved, and no new security exposure was established. Risk is low because dependency failures now occur later, while interrupted imports and simultaneous first access have not been directly validated. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring CoverageExplanation Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 12 functions across 4 files. (2 skipped: 2 unsupported.) ✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 4 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
…Semgrep Codacy flagged importlib.import_module(module_name) as a non-literal import; module_name only ever comes from the module's own _LAZY map. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summary
Resolve the re-exports in
src/common/__init__.pyandsrc/plugin_system/__init__.pylazily (PEP 562__getattr__), and import numpy insync_manageronly wheresend_frameuses it. The web process stops loading numpy, freetype helpers andPluginManagerjust to importpath_safety,store_managerorschema_manager. Importingweb_interface.appon a Pi 4 drops from ~67 MB to ~54 MB RSS. The display process ends up with the same modules loaded.Type of change
Test plan
pytest): full suite passes, apart from 8test_live_status_fields.pytests that needpsutiland fail the same way onmain. Newtest/test_lazy_package_imports.pypins that each name resolves to the same object as before,__all__is unchanged, and the web import path doesn't load numpy.Documentation
docs/if developer behavior changed (src/common/README.md,CHANGELOG.md)Plugin compatibility
from src.common import Xandfrom src.plugin_system import Xkeep working)Notes for reviewer
__all__and__dir__are preserved andTYPE_CHECKINGimports keep mypy seeing the real types.🤖 Generated with Claude Code
Summary by CodeRabbit