Skip to content

Commit e88cd81

Browse files
authored
fix(plugins): Don't copy test list during collection (#1936)
safer implementation
1 parent 9ad9f5b commit e88cd81

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/pytest_plugins/execute/execute.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,25 +349,30 @@ def pytest_generate_tests(metafunc: pytest.Metafunc):
349349

350350
def pytest_collection_modifyitems(config: pytest.Config, items: List[pytest.Item]):
351351
"""Remove transition tests and add the appropriate execute markers to the test."""
352-
for item in items[:]: # use a copy of the list, as we'll be modifying it
352+
items_for_removal = []
353+
for i, item in enumerate(items):
353354
if isinstance(item, EIPSpecTestItem):
354355
continue
355356
params: Dict[str, Any] = item.callspec.params # type: ignore
356357
if "fork" not in params or params["fork"] is None:
357-
items.remove(item)
358+
items_for_removal.append(i)
358359
continue
359360
fork: Fork = params["fork"]
360361
spec_type, execute_format = get_spec_format_for_item(params)
361362
assert issubclass(execute_format, BaseExecute)
362363
markers = list(item.iter_markers())
363364
if spec_type.discard_execute_format_by_marks(execute_format, fork, markers):
364-
items.remove(item)
365+
items_for_removal.append(i)
365366
continue
366367
for marker in markers:
367368
if marker.name == "execute":
368369
for mark in marker.args:
369370
item.add_marker(mark)
370371
elif marker.name == "valid_at_transition_to":
371-
items.remove(item)
372+
items_for_removal.append(i)
373+
continue
372374
if "yul" in item.fixturenames: # type: ignore
373375
item.add_marker(pytest.mark.yul_test)
376+
377+
for i in reversed(items_for_removal):
378+
items.pop(i)

src/pytest_plugins/filler/filler.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,27 +1102,28 @@ def pytest_collection_modifyitems(
11021102
These can't be handled in this plugins pytest_generate_tests() as the fork
11031103
parametrization occurs in the forks plugin.
11041104
"""
1105-
for item in items[:]: # use a copy of the list, as we'll be modifying it
1105+
items_for_removal = []
1106+
for i, item in enumerate(items):
11061107
params: Dict[str, Any] | None = None
11071108
if isinstance(item, pytest.Function):
11081109
params = item.callspec.params
11091110
elif hasattr(item, "params"):
11101111
params = item.params
11111112
if not params or "fork" not in params or params["fork"] is None:
1112-
items.remove(item)
1113+
items_for_removal.append(i)
11131114
continue
11141115
fork: Fork = params["fork"]
11151116
spec_type, fixture_format = get_spec_format_for_item(params)
11161117
if isinstance(fixture_format, NotSetType):
1117-
items.remove(item)
1118+
items_for_removal.append(i)
11181119
continue
11191120
assert issubclass(fixture_format, BaseFixture)
11201121
if not fixture_format.supports_fork(fork):
1121-
items.remove(item)
1122+
items_for_removal.append(i)
11221123
continue
11231124
markers = list(item.iter_markers())
11241125
if spec_type.discard_fixture_format_by_marks(fixture_format, fork, markers):
1125-
items.remove(item)
1126+
items_for_removal.append(i)
11261127
continue
11271128
for marker in markers:
11281129
if marker.name == "fill":
@@ -1144,6 +1145,9 @@ def pytest_collection_modifyitems(
11441145
f"fork_{base_fork.name()}",
11451146
)
11461147

1148+
for i in reversed(items_for_removal):
1149+
items.pop(i)
1150+
11471151

11481152
def pytest_sessionfinish(session: pytest.Session, exitstatus: int):
11491153
"""

0 commit comments

Comments
 (0)