Skip to content

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

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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting change review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants