From c1046903bf9cb52450ec02b46b88885bf83248a9 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Wed, 28 May 2025 11:25:37 +0300 Subject: [PATCH] Merge pull request #13442 from bluetech/reorder-items-pypy fixtures: workaround PyPy bug which sometimes causes a `KeyError` crash during collection (cherry picked from commit f59e1ad661832f18bce28a9522be2c6d37485d09) --- changelog/13312.bugfix.rst | 1 + src/_pytest/fixtures.py | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 changelog/13312.bugfix.rst diff --git a/changelog/13312.bugfix.rst b/changelog/13312.bugfix.rst new file mode 100644 index 00000000000..62ad36879f5 --- /dev/null +++ b/changelog/13312.bugfix.rst @@ -0,0 +1 @@ +Fixed a possible ``KeyError`` crash on PyPy during collection of tests involving higher-scoped parameters. diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 6b882fa3515..c82144aae78 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -278,10 +278,18 @@ def reorder_items_atscope( for other_scope in HIGH_SCOPES: other_scoped_items_by_argkey = items_by_argkey[other_scope] for argkey in argkeys_by_item[other_scope].get(i, ()): - other_scoped_items_by_argkey[argkey][i] = None - other_scoped_items_by_argkey[argkey].move_to_end( - i, last=False - ) + argkey_dict = other_scoped_items_by_argkey[argkey] + if not hasattr(sys, "pypy_version_info"): + argkey_dict[i] = None + argkey_dict.move_to_end(i, last=False) + else: + # Work around a bug in PyPy: + # https://github.com/pypy/pypy/issues/5257 + # https://github.com/pytest-dev/pytest/issues/13312 + bkp = argkey_dict.copy() + argkey_dict.clear() + argkey_dict[i] = None + argkey_dict.update(bkp) break if no_argkey_items: reordered_no_argkey_items = reorder_items_atscope(