Repository navigation
"Collecting" hangs forever if mark.parametrize is used with scope session or module #12355
Copy link
Copy link
Closed
Labels
topic: fixturesanything involving fixtures directly or indirectlyanything involving fixtures directly or indirectlytopic: parametrizerelated to @pytest.mark.parametrizerelated to @pytest.mark.parametrizetype: performanceperformance or memory problem/improvementperformance or memory problem/improvement
Description
Activity
Thanks for the report.
By gradually increasing the number of parameters, this looks like an "accidentally quadratic" (or worse...) situation.
A (not so...) quick profile run puts the blame on
_pytest/fixtures.py:238(reorder_items_atscope)which calls_pytest/fixtures.py:228(fix_cache_order).Profile output
225490710 function calls (225482776 primitive calls) in 79.889 seconds Ordered by: cumulative time List reduced from 3530 to 30 due to restriction <30> ncalls tottime percall cumtime percall filename:lineno(function) 501/1 0.022 0.000 79.891 79.891 {built-in method builtins.exec} 1 0.000 0.000 79.891 79.891 <string>:1(<module>) 1 0.000 0.000 79.891 79.891 <frozen runpy>:201(run_module) 1 0.000 0.000 79.703 79.703 <frozen runpy>:65(_run_code) 1 0.000 0.000 79.703 79.703 pytest/src/pytest/__main__.py:1(<module>) 1 0.000 0.000 79.703 79.703 pytest/src/_pytest/config/__init__.py:199(console_main) 1 0.000 0.000 79.703 79.703 pytest/src/_pytest/config/__init__.py:142(main) 475/71 0.001 0.000 79.672 1.122 pytest/.tox/venv/lib/python3.12/site-packages/pluggy/_manager.py:111(_hookexec) 475/71 0.004 0.000 79.672 1.122 pytest/.tox/venv/lib/python3.12/site-packages/pluggy/_callers.py:53(_multicall) 291/2 0.001 0.000 79.670 39.835 pytest/.tox/venv/lib/python3.12/site-packages/pluggy/_hooks.py:498(__call__) 1 0.000 0.000 79.550 79.550 pytest/src/_pytest/main.py:331(pytest_cmdline_main) 1 0.000 0.000 79.550 79.550 pytest/src/_pytest/main.py:272(wrap_session) 1 0.000 0.000 79.219 79.219 pytest/src/_pytest/main.py:335(_main) 1 0.000 0.000 79.201 79.201 pytest/src/_pytest/main.py:348(pytest_collection) 1 0.000 0.000 79.201 79.201 pytest/src/_pytest/main.py:746(perform_collect) 1 0.139 0.139 79.165 79.165 pytest/src/_pytest/fixtures.py:1617(pytest_collection_modifyitems) 1 0.000 0.000 79.025 79.025 pytest/src/_pytest/fixtures.py:210(reorder_items) 6/1 2.334 0.389 79.025 79.025 pytest/src/_pytest/fixtures.py:238(reorder_items_atscope) 1572861 27.109 0.000 74.492 0.000 pytest/src/_pytest/fixtures.py:228(fix_cache_order) 29886354 8.917 0.000 23.305 0.000 <string>:2(__hash__) 77074144/77073790 17.417 0.000 20.581 0.000 {built-in method builtins.hash} 36175863 8.681 0.000 13.444 0.000 /usr/lib/python3.12/enum.py:1267(__hash__) 31457220 5.894 0.000 5.894 0.000 {method 'appendleft' of 'collections.deque' objects} 6298142 2.611 0.000 5.103 0.000 {method 'get' of 'dict' objects} 11010191 2.636 0.000 4.066 0.000 pytest/src/_pytest/nodes.py:289(__hash__) 29886863 3.164 0.000 3.166 0.000 /usr/lib/python3.12/pathlib.py:524(__hash__)I don't have time to look into it right now and will look at it later, but this looks like a fun issue if someone else would like to try to figure it out. There are probably some python-level optimizations which could bring down the processing time, and algorithmic-level optimizations which could eliminate it entirely. Both are welcome.
- addedtopic: parametrizerelated to @pytest.mark.parametrizerelated to @pytest.mark.parametrizetype: performanceperformance or memory problem/improvementperformance or memory problem/improvementtopic: fixturesanything involving fixtures directly or indirectlyanything involving fixtures directly or indirectly
on May 25, 2024 - added 2 commits that reference this issue
on Jun 2, 2024 - added a commit that references this issue
on Jun 4, 2024
Metadata
Metadata
Assignees
Labels
topic: fixturesanything involving fixtures directly or indirectlyanything involving fixtures directly or indirectlytopic: parametrizerelated to @pytest.mark.parametrizerelated to @pytest.mark.parametrizetype: performanceperformance or memory problem/improvementperformance or memory problem/improvement
If you parametrize a test function with several parameters with
@mark.parametrizeand set scope to "session" or "module" the collection step will take forever*. Debugging shows that_pytest/fixtures.py:fix_cache_order()is called repeatedly:*forever scales with the number of parameters.
pip listfrom the virtual environment you are usingpytest and operating system versions
minimal example if possible
The issue is reproducible with pytest 7.4.0 and 8.2.1 with the following minimal example.