Skip to content

Commit cf37399

Browse files
author
rodrigo.nogueira
committed
Fix flaky import time test for Python 3.12+
1 parent 7b4db6f commit cf37399

3 files changed

Lines changed: 17 additions & 18 deletions

File tree

CHANGES/11992.contrib.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fixed flaky import time test for Python 3.12+ -- by :user:`rodrigobnogueira`.
2+
3+
Refactored to use version comparison instead of explicit version list,
4+
making the test future-proof for new Python releases.
5+

CONTRIBUTORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ Raúl Cumplido
318318
Required Field
319319
Robert Lu
320320
Robert Nikolich
321+
Rodrigo Nogueira
321322
Roman Markeloff
322323
Roman Podoliaka
323324
Roman Postnov

tests/test_imports.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,14 @@ def test_web___all__(pytester: pytest.Pytester) -> None:
2828
result.assert_outcomes(passed=0, errors=0)
2929

3030

31-
_IS_CI_ENV = os.getenv("CI") == "true"
32-
_XDIST_WORKER_COUNT = int(os.getenv("PYTEST_XDIST_WORKER_COUNT", 0))
33-
_IS_XDIST_RUN = _XDIST_WORKER_COUNT > 1
34-
35-
_TARGET_TIMINGS_BY_PYTHON_VERSION = {
36-
"3.12": (
37-
# 3.12+ is expected to be a bit slower due to performance trade-offs,
38-
# and even slower under pytest-xdist, especially in CI
39-
_XDIST_WORKER_COUNT * 100 * (1 if _IS_CI_ENV else 1.53)
40-
if _IS_XDIST_RUN
41-
else 295
42-
),
43-
}
44-
_TARGET_TIMINGS_BY_PYTHON_VERSION["3.13"] = _TARGET_TIMINGS_BY_PYTHON_VERSION["3.12"]
31+
_IMPORT_TIME_THRESHOLD_PY312 = 350
32+
_IMPORT_TIME_THRESHOLD_DEFAULT = 200
33+
34+
35+
def _get_import_time_threshold() -> float:
36+
if sys.version_info >= (3, 12):
37+
return _IMPORT_TIME_THRESHOLD_PY312
38+
return _IMPORT_TIME_THRESHOLD_DEFAULT
4539

4640

4741
@pytest.mark.internal
@@ -67,7 +61,7 @@ def test_import_time(pytester: pytest.Pytester) -> None:
6761
for _ in range(3):
6862
r = pytester.run(sys.executable, "-We", "-c", cmd)
6963

70-
assert not r.stderr.str()
64+
assert not r.stderr.str(), r.stderr.str()
7165
runtime_ms = int(r.stdout.str())
7266
if runtime_ms < best_time_ms:
7367
best_time_ms = runtime_ms
@@ -77,7 +71,6 @@ def test_import_time(pytester: pytest.Pytester) -> None:
7771
else:
7872
os.environ["PYTHONPATH"] = old_path
7973

80-
expected_time = _TARGET_TIMINGS_BY_PYTHON_VERSION.get(
81-
f"{sys.version_info.major}.{sys.version_info.minor}", 200
82-
)
74+
expected_time = _get_import_time_threshold()
8375
assert best_time_ms < expected_time
76+

0 commit comments

Comments
 (0)