Repository navigation
Conversation
Documentation build overview
7 files changed ·
|
| # 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): |
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
I agree, in this case I don't think it is worth it,
unittestisn'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.testingordjango.testthat importunittestforTestCase,SkipTestor theassertmethods, orunittest.mock. -
import doctestis often in production code and subclassesunittest.
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 |
Also:
test_lazy_importintest_string.py(added in gh-118761: Add test_lazy_import for more modules #133057) had a copy/paste typo and was testing thebase64module instead ofstring. I fixed it here rather than creating its own single-line PR.