Skip to content

Keep optional modules lazy during IPython autoreload - #3084

Open
Con-Benksl wants to merge 1 commit into
speechbrain:developfrom
Con-Benksl:fix/lazy-module-autoreload
Open

Con-Benksl wants to merge 1 commit into
speechbrain:developfrom
Con-Benksl:fix/lazy-module-autoreload

Conversation

@Con-Benksl

Copy link
Copy Markdown

What does this PR do?

Fixes #2995.

With %autoreload 2, IPython inspects __file__ on modules in sys.modules. For SpeechBrain's lazy redirects, this forces optional imports: import speechbrain produces 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 raise AttributeError; the class documentation now describes that behavior.

Validation on Python 3.11.15 / PyTorch 2.6.0 / IPython 9.17.1:

  • Six new regression cases fail before the fix and pass afterward, covering available modules, missing optional dependencies, relative imports and deprecated redirects. New and related existing tests: 16 passed, including the HyperPyYAML deprecated-import test.
  • Reproduced the original error in a real IPython shell. After the fix, %autoreload 2; import speechbrain succeeds without loading NLP/Flair. Editing a temporary module updates both its lazy export and an already-referenced function through IPython autoreload.
  • Baseline/current comparisons preserve inspect, importlib reload and exported-function pickle behavior. pre-commit run -a, changed-file hooks and git diff --check pass.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LazyModule not playing well with %autoreload

1 participant