Skip to content

gh-127036: Update importlib.util._LazyModule.__getattribute__ to special-case requests for __spec__. #127038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Sachaa-Thanasius
Copy link
Contributor

@Sachaa-Thanasius Sachaa-Thanasius commented Nov 19, 2024

Allow importlib.util._LazyModule to special-case its __spec__ attribute so that when the regular import machinery requests that attribute, it doesn't trigger the full module load. This will make it easier to use importlib.util.LazyLoader.

Allow `importlib.util._LazyModule` to special-case its `__spec__` attribute so that the regular import machinery requesting that attribute doesn't trigger the full module load.
@Sachaa-Thanasius
Copy link
Contributor Author

Sachaa-Thanasius commented Nov 19, 2024

A few things I'm unsure of:

  • Is this worthy of a news fragment?
  • What kind of tests would suffice? I can imagine adding two along the lines of test_spec_passthrough_without_load and test_e2e_same_module in Lib/test/test_importlib/test_lazy.py, but I'm not confident in that, so I'd appreciate any guidance.

@brettcannon
Copy link
Member

  • Is this worthy of a news fragment?

I think so since it changes the semantics and someone may have been relying on it.

  • What kind of tests would suffice?

I think what you already have in the PR suffices as it verifies things are working.

@brettcannon
Copy link
Member

And apologies for taking so long to get to this PR!

@brettcannon
Copy link
Member

FYI my approval is still gated on a news entry thanks to CI checking for it.

@brettcannon brettcannon self-assigned this Aug 18, 2025
@Sachaa-Thanasius
Copy link
Contributor Author

Sachaa-Thanasius commented Aug 18, 2025

Cheers! Looking at this with fresh eyes, the semantic change definitely warrants more than just a code change.

In addition to the news entry, I'll attempt to tweak the LazyLoader docs and LazyLoader.exec_module comment to mention the __spec__ special case, since both currently specify that any attribute access triggers the load. Hope that's fine.

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

Successfully merging this pull request may close these issues.

2 participants