runner: use node's Store to keep private SetupState state instead of an attribute

This way it gets proper typing and decoupling.
This commit is contained in:
Ran Benita 2020-12-30 17:20:19 +02:00
parent da70f61f67
commit f7b0b1dd1f
1 changed files with 8 additions and 4 deletions

View File

@ -33,8 +33,10 @@ from _pytest.nodes import Collector
from _pytest.nodes import Item from _pytest.nodes import Item
from _pytest.nodes import Node from _pytest.nodes import Node
from _pytest.outcomes import Exit from _pytest.outcomes import Exit
from _pytest.outcomes import OutcomeException
from _pytest.outcomes import Skipped from _pytest.outcomes import Skipped
from _pytest.outcomes import TEST_OUTCOME from _pytest.outcomes import TEST_OUTCOME
from _pytest.store import StoreKey
if TYPE_CHECKING: if TYPE_CHECKING:
from typing_extensions import Literal from typing_extensions import Literal
@ -465,14 +467,16 @@ class SetupState:
if exc: if exc:
raise exc raise exc
_prepare_exc_key = StoreKey[Union[OutcomeException, Exception]]()
def prepare(self, colitem: Item) -> None: def prepare(self, colitem: Item) -> None:
"""Setup objects along the collector chain to the test-method.""" """Setup objects along the collector chain to the test-method."""
# Check if the last collection node has raised an error. # Check if the last collection node has raised an error.
for col in self.stack: for col in self.stack:
if hasattr(col, "_prepare_exc"): prepare_exc = col._store.get(self._prepare_exc_key, None)
exc = col._prepare_exc # type: ignore[attr-defined] if prepare_exc:
raise exc raise prepare_exc
needed_collectors = colitem.listchain() needed_collectors = colitem.listchain()
for col in needed_collectors[len(self.stack) :]: for col in needed_collectors[len(self.stack) :]:
@ -480,7 +484,7 @@ class SetupState:
try: try:
col.setup() col.setup()
except TEST_OUTCOME as e: except TEST_OUTCOME as e:
col._prepare_exc = e # type: ignore[attr-defined] col._store[self._prepare_exc_key] = e
raise e raise e