Skip to content

gh-118761: Add test_lazy_import for more modules #133057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

danielhollas
Copy link
Contributor

@danielhollas danielhollas commented Apr 27, 2025

I went through my past PRs where I sped up the import time of threading and pathlib modules (#114509 and #123520) and added regression tests to ensure that the respective module imports stay lazy, using @JelleZijlstra's new ensure_lazy_import test fixture in #132614.

Happy to do this for more modules if it is better to do it in one PR instead of many.

I wasn't quite sure where to put these tests so happy to take guidance on that.

@danielhollas
Copy link
Contributor Author

@Wulian233 I am not sure what you mean. Please see #132614 and #118761 (comment) for the motivation.

@@ -129,6 +129,9 @@ class StrSubclass(str):
for part in p.parts:
self.assertIs(type(part), str)

def test_lazy_import(self):
Copy link
Member

Choose a reason for hiding this comment

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

Would you mind adding the @cpython_only decorator, since these tests don't necessarily make sense on other Python implementations?

I should have done that on my PRs too but didn't think about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@JelleZijlstra
Copy link
Member

Thanks! Feel free to make the same change for more modules in this PR.

@danielhollas danielhollas requested review from ethanfurman, rhettinger and a team as code owners April 28, 2025 00:12
@danielhollas
Copy link
Contributor Author

@JelleZijlstra thanks for taking a look! I've added the tests for enum, functools and email.utils modules. This should cover the modules handled in the original issue #109653. I'll stop for now.

Unless somebody else beats me to it, I can go through the rest of the modules handled in #118761, maybe next weekend.

@@ -1274,6 +1275,10 @@ class Holiday(date, Enum):
IDES_OF_MARCH = 2013, 3, 15
self.Holiday = Holiday

@cpython_only
Copy link
Member

Choose a reason for hiding this comment

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

I think this fits better in MiscTestCase (currently at around line 5286). In general I'd put this test close to test__all__ if present; it's sort of similar in testing the behavior of the whole module.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that's a very helpful comment as I am not familiar with cpython's test suite. Done.

@danielhollas danielhollas changed the title gh-118761: test_lazy_import for threading and pathlib modules gh-118761: Add test_lazy_import for more modules Apr 28, 2025
@@ -129,6 +130,7 @@ class StrSubclass(str):
for part in p.parts:
self.assertIs(type(part), str)

@cpython_only
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't fit well in this class, can you add a new test class instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@rhettinger rhettinger removed their request for review April 29, 2025 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants