Skip to content

fix: Windows-incompatible python's inspect file guard in LazyModule - #3065

Open
Need-an-AwP wants to merge 1 commit into
speechbrain:developfrom
Need-an-AwP:develop
Open

fix: Windows-incompatible python's inspect file guard in LazyModule#3065
Need-an-AwP wants to merge 1 commit into
speechbrain:developfrom
Need-an-AwP:develop

Conversation

@Need-an-AwP

Copy link
Copy Markdown

What does this PR do?

On Windows, inspect.py lives at paths like C:\Python311\Lib\inspect.py, so this guard never triggers. Lazy modules with optional dependencies (notably speechbrain.integrations.k2_fsa, which requires k2) can be imported unintentionally during normal model loading, causing hard ImportError crashes on Windows even when k2 is never used.

Replace the slash-specific endswith("/inspect.py") check with os.path.basename(importer_frame.filename) == "inspect.py".

os is already imported in this file; no new dependencies.

Before submitting
  • Did you read the contributor guideline?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Does your code adhere to project-specific code style and conventions?

PR review

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified
  • Confirm that the changes adhere to compatibility requirements (e.g., Python version, platform)
  • Review the self-review checklist to ensure the code is ready for review

@MHammett

MHammett commented Aug 2, 2026

Copy link
Copy Markdown

Independent confirmation, from a different and more consequential failure mode than #2798 (which only affected test collection).

This breaks import pyannote.audio outright in normal usage — not in a test suite. pyannote.audio's speaker-embedding loading path touches speechbrain's deprecation-redirect shims (speechbrain.k2_integration, .wordemb, .lobes.models.spacy/.flair) during torch's fake-kernel registration, and any shim whose target is a genuinely-missing optional package (k2, flair, spacy) raises ImportError, which propagates through inspect.getmodule's hasattr() check and kills whatever unrelated import triggered it.

Minimal repro needs no pyannote at all:

python -c "import speechbrain, torchvision"   # ImportError
python -c "import torchvision, speechbrain"   # fine

Confirmed this PR's fix (os.path.basename(...) == "inspect.py") resolves it on Windows 10 / Python 3.13 / speechbrain 1.1.0, with real lazy exports, deprecation warnings, and genuine ImportErrors for explicitly-requested missing integrations all still behaving correctly.

One thing worth flagging for anyone hitting this in the meantime: pyannote.audio 4.0.7 looks like it fixes this (a quick import pyannote.audio succeeds), but it only reorders pyannote's own imports to dodge the trigger — import speechbrain; import pyannote.audio still fails on 4.0.7. Any code importing speechbrain earlier (ours did, via audio-loading libraries) hits this regardless of pyannote version.

Would be great to see this merged — it's a one-line fix for a guard whose intent (per the existing code comment about "PyTorch's op registering machinery") is already exactly right.

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.

2 participants