Skip to content

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.

@brettcannon brettcannon changed the title gh-127036: Update importlib.util._LazyModule.__getattribute__ to special-case requests for __spec__. gh-127036: Update importlib.util._LazyModule.__getattribute__ to special-case requests for __spec__ Aug 19, 2025
@brettcannon
Copy link
Member

Hope that's fine.

Sounds great!

@brettcannon brettcannon self-requested a review August 19, 2025 17:55
Copy link
Member

@brettcannon brettcannon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs at least a news entry and doc changes.

@bedevere-app
Copy link

bedevere-app bot commented Sep 9, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

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