fix: use lazy FTP connection initialization to avoid connecting on import#244
fix: use lazy FTP connection initialization to avoid connecting on import#244ndpvt-web wants to merge 2 commits intoAlertaDengue:mainfrom
Conversation
…port Resolves AlertaDengue#242. FTP connections are now deferred until data is actually requested, preventing import failures when the FTP server is unavailable. Changes: - Added lazy wrapper classes for all online_data database modules - SINAN, SIM, CNES, SIA, SIH, PNI, CIHA, SINASC, and IBGE modules now use lazy initialization - FTP connections only occur when methods are called, not at import time - Maintains backward compatibility with existing API Co-Authored-By: AI Assistant (Claude) <ai-assistant@contributor-bot.dev> Signed-off-by: ndpvt-web <ndpvt-web@users.noreply.github.com>
fccoelho
left a comment
There was a problem hiding this comment.
I think the implementation could be simplified by moving all methods of the _Lazy* classes to a base class and let each derived _Lazy wrapper overload only the _ensure_loaded method that is specific to each database. This will avoid a lot of code duplication.
Also, the _ensure_loaded methods is optimistic that the connection will always work. I think it should handle a potential failure in the connection to the FTP server.
- Move all duplicated lazy wrapper logic into a shared _LazyDatabase base class in pysus/online_data/_lazy.py - Use __getattr__ to transparently proxy all attribute access to the underlying database instance, eliminating per-class method stubs - Add FTP connection error handling: _ensure_loaded now catches exceptions and raises ConnectionError with a descriptive message - Replace 9 individual _Lazy* classes with simple one-liner instantiations: e.g. ciha = _LazyDatabase(CIHA) Addresses review feedback from @fccoelho requesting base class extraction and FTP error handling. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Thanks for the review! I've addressed both points:
Updated in the latest commit. |
Summary
Resolves #242
This PR implements lazy FTP connection initialization for all
online_datadatabase modules, preventing the library from connecting to the FTP server at import time.Problem
When importing PySUS modules, the library immediately connects to the FTP server via module-level
.load()calls. If the FTP server is unavailable, the import crashes, making the entire library unusable even for operations that don't require FTP access.Solution
Added lazy wrapper classes for each database module that defer the FTP connection until data is actually requested:
Changes
Testing
AI Disclosure
This contribution was developed with the assistance of Claude (AI by Anthropic). The implementation approach, code, and PR description were AI-assisted. All changes are focused on resolving the specific issue described above.
Co-Authored-By: AI Assistant (Claude) ai-assistant@contributor-bot.dev