Skip to content

gh-154675: Improve import time of unittest and unittest.mock by around 4 times - #162871

Open
hugovk wants to merge 6 commits into
python:mainfrom
hugovk:3.16-lazy-unittest
Open

hugovk wants to merge 6 commits into
python:mainfrom
hugovk:3.16-lazy-unittest

Conversation

@hugovk

@hugovk hugovk commented Nov 30, 2026 •

Copy link
Copy Markdown
Member
module before after
unittest
14 ms image 3 ms image
unittest.mock
14 ms image 4 ms image

Also:

@read-the-docs-community

read-the-docs-community Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #36296458 | 📁 Comparing c5bf1ec against main (352cc06)

  🔍 Preview build  

7 files changed · ± 7 modified

± Modified

Comment thread Lib/unittest/loader.py Outdated
# we would need to avoid loading the same tests multiple times
# from '.py', *and* '.pyc'
VALID_MODULE_NAME = re.compile(r'[_a-z]\w*\.py$', re.IGNORECASE)
def __getattr__(name):

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.

I find these kind of constructions quite ugly, especially if you put them at the top of the module where anyone following the unittest loader would find it. Is it necessary to make re import faster here? (I think it's 11% only right?)

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.

I agree, in this case I don't think it is worth it, unittest isn't particularly performance sensitive. Also, how many of these does a simple test suite trigger anyway?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is it necessary to make re import faster here? (I think it's 11% only right?)

Yes, this bit is to avoid needlessly importing re, which costs 2 ms or 14%. It means the PR title is "around 4 times" not "around 3 times" :)

if you put them at the top of the module where anyone following the unittest loader would find it.

We can drop it, or how about we put def __getattr__ at the end, like we did for the __version__ deprecations?

https://github.com/python/cpython/pull/142658/changes#diff-10e6db6c775e351ba2b55c4ad5e177191afe25d4942d1d2162d6e10505788a81R1081

I agree, in this case I don't think it is worth it, unittest isn't particularly performance sensitive. Also, how many of these does a simple test suite trigger anyway?

It mainly helps for bare imports:

  • Libraries that with testing helpers in public APIs, like numpy.testing or django.test that import unittest for TestCase, SkipTest or the assert methods, or unittest.mock.

  • import doctest is often in production code and subclasses unittest.

Here's some timings with and without VALID_MODULE_NAME deferral:

Command Base Branch, eager pattern Branch, deferred pattern
python -c "import unittest" 14.7 ms 5.7 ms 4.5 ms
python -c "import unittest.mock" 16.9 ms 7.0 ms 4.7 ms
python test_x.py 23.5 ms 19.6 ms 18.8 ms
python -m unittest test_x 23.8 ms 19.6 ms 18.9 ms
python -m unittest (discovery) 24.3 ms 19.7 ms 19.2 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants