Skip to content

gh-139669: Make __name__ and __file__ as a proxy for _LazyModule.__spec__ - #151006

Closed
xuantengh wants to merge 3 commits into
python:mainfrom
xuantengh:xuantengh/ll
Closed

xuantengh wants to merge 3 commits into
python:mainfrom
xuantengh:xuantengh/ll

Conversation

@xuantengh

@xuantengh xuantengh commented Jun 6, 2026 •

Copy link
Copy Markdown
Contributor

This PR makes __name__ and __file__, as a proxy for _LazyModule.__spec__. That says, reading & writing both will be directly forwarded to _LazyModule.__spec__, which will NOT trigger the lazy loading.

@python-cla-bot

python-cla-bot Bot commented Jun 6, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@read-the-docs-community

read-the-docs-community Bot commented Jun 6, 2026 •

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Sep 5, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Sep 5, 2026
@kushaldas kushaldas self-assigned this Nov 13, 2026

@kushaldas kushaldas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This breaks the pre-existing promise:

def test_mutated_preexisting_attr(self):
that changing the attribute should persist.

For example if you add the following in Lib/test/test_importlib/test_lazy.py , this passes in the current implementation but fails after applying the patch.

def test_mutated_name_survives_loading(self):
    module = self.new_module()
    module.__name__ = 'bogus'

    # Reading __name__ no longer triggers loading.
    self.assertEqual(module.__name__, 'bogus')

    # Accessing attr executes TestingImporter's source, which assigns
    # __name__ = 'changed'. The earlier user assignment should survive.
    self.assertEqual(module.attr, 42)
    self.assertEqual(module.__name__, 'bogus'

The __name__ assignment should survive the loading.

@bedevere-app

bedevere-app Bot commented Nov 14, 2026

Copy link
Copy Markdown

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.

@xuantengh

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again

@bedevere-app

bedevere-app Bot commented Nov 15, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@kushaldas: please review the changes made to this pull request.

@bedevere-app
bedevere-app Bot requested a review from kushaldas November 15, 2026 15:23
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Nov 16, 2026
@kushaldas

Copy link
Copy Markdown
Member

@xuantengh also please rebase the PR for a cleaner merge.

@xuantengh

Copy link
Copy Markdown
Contributor Author

Rebased, thanks for the detailed review.

I have made the requested changes; please review again

@bedevere-app

bedevere-app Bot commented Nov 18, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@kushaldas: please review the changes made to this pull request.

@FFY00 FFY00 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Both of these attributes can be overwritten by the module on execution, so we can't just redirect to __spec__.

It seems like this change was motivated by inspect.getframeinfo triggering the import of all lazy modules. IMO this is the wrong approach to solve that issue, inspect should learn about lazy module instead.

@bedevere-app

bedevere-app Bot commented Nov 20, 2026

Copy link
Copy Markdown

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.

@FFY00 FFY00 closed this Nov 20, 2026
@FFY00

FFY00 commented Nov 20, 2026

Copy link
Copy Markdown
Member

If the issue reaches a consensus that this is the right approach, feel free to re-open.

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.

3 participants