Keep optional modules lazy during IPython autoreload - #3084
Open
Con-Benksl wants to merge 1 commit into
Open
Con-Benksl wants to merge 1 commit into
Con-Benksl wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #2995.
With
%autoreload 2, IPython inspects__file__on modules insys.modules. For SpeechBrain's lazy redirects, this forces optional imports:import speechbrainproduces a post-execution callback error when Flair is not installed.Keep
__file__absent until the lazy target has actually loaded. Ordinary attribute access still loads the target, after which its real filename is available. This applies to absolute/relative lazy imports and deprecated redirects without adding an IPython-specific caller check or a dependency.This intentionally changes an explicit
__file__lookup on an unloaded proxy to raiseAttributeError; the class documentation now describes that behavior.Validation on Python 3.11.15 / PyTorch 2.6.0 / IPython 9.17.1:
%autoreload 2; import speechbrainsucceeds without loading NLP/Flair. Editing a temporary module updates both its lazy export and an already-referenced function through IPython autoreload.pre-commit run -a, changed-file hooks andgit diff --checkpass.No models or datasets were downloaded. The full repository suite, GPU execution and pretrained inference were not run.
Implemented and tested autonomously with OpenAI Codex, with independent read-only review by another Codex agent.