Skip to content

Commit 643afdf

Browse files
committed
Fix current_item_index
1 parent ba0ba86 commit 643afdf

3 files changed

Lines changed: 22 additions & 11 deletions

File tree

src/_pytest/fixtures.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
from _pytest.scope import _ScopeName
6868
from _pytest.scope import HIGH_SCOPES
6969
from _pytest.scope import Scope
70+
from _pytest.stash import StashKey
7071
from _pytest.warning_types import PytestWarning
7172

7273

@@ -75,8 +76,6 @@
7576

7677

7778
if TYPE_CHECKING:
78-
from _pytest.nodes import Item
79-
from _pytest.nodes import Node
8079
from _pytest.python import CallSpec2
8180
from _pytest.python import Function
8281
from _pytest.python import Metafunc
@@ -424,7 +423,7 @@ def fixturenames(self) -> list[str]:
424423

425424
@property
426425
@abc.abstractmethod
427-
def node(self) -> Node:
426+
def node(self):
428427
"""Underlying collection node (depends on current request scope)."""
429428
raise NotImplementedError()
430429

@@ -700,7 +699,7 @@ def _check_scope(
700699
pass
701700

702701
@property
703-
def node(self) -> Node:
702+
def node(self):
704703
return self._pyfuncitem
705704

706705
def __repr__(self) -> str:
@@ -753,7 +752,7 @@ def _scope(self) -> Scope:
753752
return self._scope_field
754753

755754
@property
756-
def node(self) -> Node:
755+
def node(self):
757756
scope = self._scope
758757
if scope is Scope.Function:
759758
# This might also be a non-function Item despite its attribute name.
@@ -973,6 +972,21 @@ def _get_cached_value(
973972
return cached_result[0]
974973

975974

975+
_item_index = StashKey[int]()
976+
977+
978+
def _get_item_index(item: nodes.Item) -> int:
979+
if (index := item.stash.get(_item_index, None)) is not None:
980+
return index
981+
982+
for index, session_item in enumerate(item.session.items):
983+
session_item.stash[_item_index] = index
984+
985+
index = item.stash.get(_item_index, None)
986+
assert index is not None, f"Item {item.nodeid} is not inside its session"
987+
return index
988+
989+
976990
class FixtureDef(Generic[FixtureValue]):
977991
"""A container for a fixture definition.
978992
@@ -1147,7 +1161,7 @@ def _should_finalize_in_current_item(self, request: SubRequest) -> bool:
11471161

11481162
node = request.node
11491163
session = request.session
1150-
current_item_index = session._current_item_index
1164+
current_item_index = _get_item_index(request._pyfuncitem)
11511165
assert current_item_index is not None
11521166

11531167
for nextitem in itertools.islice(session.items, current_item_index + 1, None):
@@ -1188,7 +1202,7 @@ def _check_cache_hit(self, request: SubRequest, old_cache_key: object) -> None:
11881202
def cache_key(self, request: SubRequest) -> object:
11891203
return getattr(request, "param", None)
11901204

1191-
def _cache_key_internal(self, item: Item) -> object:
1205+
def _cache_key_internal(self, item: nodes.Item) -> object:
11921206
callspec: CallSpec2 | None = getattr(item, "callspec", None)
11931207
if callspec is not None and self.argname in callspec.params:
11941208
return callspec.params[self.argname]

src/_pytest/main.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,6 @@ def pytest_runtestloop(session: Session) -> bool:
392392

393393
for i, item in enumerate(session.items):
394394
nextitem = session.items[i + 1] if i + 1 < len(session.items) else None
395-
session._current_item_index = i
396395
item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
397396
if session.shouldfail:
398397
raise session.Failed(session.shouldfail)
@@ -609,7 +608,6 @@ def __init__(self, config: Config) -> None:
609608
self._initial_parts: list[CollectionArgument] = []
610609
self._collection_cache: dict[nodes.Collector, CollectReport] = {}
611610
self.items: list[nodes.Item] = []
612-
self._current_item_index: int | None = None
613611

614612
self._bestrelpathcache: dict[Path, str] = _bestrelpath_cache(config.rootpath)
615613

src/_pytest/pytester.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,7 @@ def genitems(self, colitems: Sequence[Item | Collector]) -> list[Item]:
10401040
result: list[Item] = []
10411041
for colitem in colitems:
10421042
result.extend(session.genitems(colitem))
1043+
session.items.extend(result)
10431044
return result
10441045

10451046
def runitem(self, source: str) -> Any:
@@ -1280,8 +1281,6 @@ def getitem(
12801281
items = self.getitems(source)
12811282
for item in items:
12821283
if item.name == funcname:
1283-
# HACK: keep item.session._setupstate.setup(item) working.
1284-
item.session._current_item_index = 0
12851284
return item
12861285
assert 0, f"{funcname!r} item not found in module:\n{source}\nitems: {items}"
12871286

0 commit comments

Comments
 (0)