GeezerVison is showing github.com — gh-117182: Allow lazily loaded modules to modify their own __class__ by effigies · Pull Request #117185 · python/cpython
Conversation
effigies
commented
Mar 23, 2024
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
This PR resolves #117182. The meat of the PR is to check whether the module modified its own __class__ during load, and only set the default class if not.
I also realized that the LazyLoader saves the original __class__ in the loader_state dict. I haven't found a case where that __class__ isn't ModuleType, but if it ever does occur, it does make sense to restore that type. And if that type for some reason overrides __getattribute__, it would make sense to use that instead of object.__getattribute__.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@effigies thank you!
I agree with the changes, and the implementation looks good to me.
Regarding backporting, I think it'd be better to just merge this into main as an improvement, without backporting it to older versions. I don't feel strongly about this though, so if another dev thinks it makes sense to backport, that'd be fine by me.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
effigies reacted with thumbs up emoji
FFY00
changed the title
gh-117182: Allow modules to modify their own __class__ when lazily loaded
gh-117182: Allow lazily loaded modules to modify their own __class__
Apr 9, 2024
|
Btw, I was forced to rename as the commit message was too big.
|
effigies reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
lunaris
commented
Apr 17, 2024
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
Hey, I think this issue is affecting our codebase (https://github.com/pulumi/pulumi, specifically its Python SDK), which makes use of lazy loading modules that modify their own __class__. Responding to the comments above, would it be possible to backport this to 3.11/3.12/any affected versions? Presently Pulumi's Python SDK won't work for our users if they are on 3.11.9 or 3.12 due to this.
If not, is there a workaround at all? I've posted what I think is a minimal reproduction for our use case which I've tested with 3.8.19 ✔️, 3.9,19 ✔️, 3.10.14 ✔️, 3.11.8 ✔️, 3.11.9 🔴, and 3.12.3 🔴 at https://github.com/lunaris/python-lazy-loading-repro if that's useful.
Thanks for any support and thoughts!
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
effigies
commented
Apr 17, 2024
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
Ah, that reproduction demonstrates that it's a regression introduced by #114781, which makes sense. We deferred resetting the module class until after load, to prevent races, but that would override subclassed module types where we did not previously.
@FFY00 I think it would be worth back-porting this.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@lunaris If it doesn't get backported, you can simply copy the LazyLoader and _LazyModule classes and use those instead of importlib.util.LazyLoader. There's nothing magic about the stdlib ones.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@effigies Thanks for this -- really useful to know!
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|