Fix _LazyModule to not trigger load for introspection attributes#21620
Fix _LazyModule to not trigger load for introspection attributes#21620Frassle merged 4 commits intopulumi:masterfrom
Conversation
Tools like debuggers, file watchers, and IDEs (e.g., Streamlit) iterate over sys.modules and check module attributes like __file__, __spec__, __path__, etc. The previous implementation triggered a full module load on any attribute access, causing ~340MB memory overhead and ~3 second delays when these tools inspected lazy-loaded submodules. This fix adds an early return in __getattribute__ that checks if the requested attribute is a standard introspection attribute and returns it directly from the parent class without triggering the lazy load. See: streamlit/streamlit#13530 Co-Authored-By: Claude (claude-opus-4-5) <noreply@anthropic.com>
|
PR is now waiting for a maintainer to take action. Note for the maintainer: Commands available:
|
Co-Authored-By: Claude (claude-opus-4-5) <noreply@anthropic.com>
|
PR is now waiting for a maintainer to take action. Note for the maintainer: Commands available:
|
|
Hey @Frassle . Wondering if we can get this change reviewed (or if there are any processes to get this PR noticed)? It seems small and innocent enough, and it would benefit Streamlit developers who use Pulumi. |
tgummerer
left a comment
There was a problem hiding this comment.
Just a small comment from me, I don't know enough about this code to review it for correctness. This could use some more description for why it is correct to shortcircuit for these attributes as well.
| if mod_name.startswith("lazy_import_test"): | ||
| del sys.modules[mod_name] | ||
|
|
||
| # Create a lazy module |
There was a problem hiding this comment.
All of these comments below are not very useful. AI likes to add exactly what it does as a comment for whatever reason, but just remove them please, they are just noise.
There was a problem hiding this comment.
Ack. Happy to remove.
Frassle
left a comment
There was a problem hiding this comment.
I'm not sure this is correct? Doesn't this mean any lookup of like __file__ or __doc__ returns the information from types.ModuleType rather than the module itself?
This is using the I saw that @justinvp has done some work in this space and can probably verify. |
Can we update the test to show that then? Once that's done and it works then I think we can merge this. |
- Expand comment explaining why short-circuiting is correct: the introspection attributes are set on the module instance by importlib.util.module_from_spec() before exec_module() is called - Remove inline comments from test per reviewer request - Add assertions verifying introspection attrs return correct values (__name__, __spec__.name, __file__, __package__) Co-Authored-By: Claude (claude-opus-4-5) <noreply@anthropic.com>
|
PR is now waiting for a maintainer to take action. Note for the maintainer: Commands available:
|
|
/run-acceptance-tests |
|
PR is now waiting for a maintainer to take action. Note for the maintainer: Commands available:
|
|
/run-acceptance-tests |
|
@Frassle Looks like the tests failed on something with dotnet, so I am not sure if it's related to my change. Let me know if I should rebase or do something else. |
|
No just a flake we've been hitting from too much parallelism in the test runner. I think this should be ok to merge now. |
|
Thanks for the contribution @kmcgrady |
To be merged after #21727 and #21764 made it in. Tentative changelog: ## (2026-02-10) ### Features - [cli] Show environment variables that were set if a snapshot integrity error happens [#21709](#21709) - [engine] Pass replacement trigger through to Construct [#21408](#21408) - [engine] Add EnvVarMappings resource option for provider resources, allowing environment variables to be remapped before being passed to the provider [#21572](#21572) - [pkg] BREAKING: Deprecate github.com/pulumi/pulumi/pkg/v3/codegen/dotnet in favor of github.com/pulumi/pulumi-dotnet/pulumi-language-dotnet/v3/codegen. This package will be removed from pulumi/pulumi soon! [#21720](#21720) ### Bug Fixes - [cli] Retry `yarn install` when it fails (e.g. during `pulumi install`) [#21707](#21707) - [engine] Deal with errors in elided journal entries correctly [#21576](#21576) - [sdk/python] Fix `_LazyModule` to not trigger full module load for introspection attributes [#21620](#21620) - [sdkgen/python] Remove workaround for slow typechecking with MyPy and PyCharm [#21722](#21722) ### Miscellaneous - [cli] Write logfile location if verbosity is >= 1 to stderr instead of stdout [#21663](#21663)
|
This PR has been shipped in release v3.220.0. |
Summary
_LazyModule.__getattribute__to skip lazy loading for standard module introspection attributes (__file__,__spec__,__path__,__name__,__loader__,__package__,__cached__,__doc__,__dict__,__class__)sys.modulesFixes: streamlit/streamlit#13530
Test plan
test_lazy_module_introspection_attrs_do_not_trigger_loadtest that verifies:test_utils.pytests pass🤖 Generated with Claude Code