runner: complete type annotations of SetupState
This commit is contained in:
parent
d4f8e4b40c
commit
da70f61f67
|
@ -403,22 +403,22 @@ def pytest_make_collect_report(collector: Collector) -> CollectReport:
|
||||||
class SetupState:
|
class SetupState:
|
||||||
"""Shared state for setting up/tearing down test items or collectors."""
|
"""Shared state for setting up/tearing down test items or collectors."""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
self.stack: List[Node] = []
|
self.stack: List[Node] = []
|
||||||
self._finalizers: Dict[Node, List[Callable[[], object]]] = {}
|
self._finalizers: Dict[Node, List[Callable[[], object]]] = {}
|
||||||
|
|
||||||
def addfinalizer(self, finalizer: Callable[[], object], colitem) -> None:
|
def addfinalizer(self, finalizer: Callable[[], object], colitem: Node) -> None:
|
||||||
"""Attach a finalizer to the given colitem."""
|
"""Attach a finalizer to the given colitem."""
|
||||||
assert colitem and not isinstance(colitem, tuple)
|
assert colitem and not isinstance(colitem, tuple)
|
||||||
assert callable(finalizer)
|
assert callable(finalizer)
|
||||||
# assert colitem in self.stack # some unit tests don't setup stack :/
|
# assert colitem in self.stack # some unit tests don't setup stack :/
|
||||||
self._finalizers.setdefault(colitem, []).append(finalizer)
|
self._finalizers.setdefault(colitem, []).append(finalizer)
|
||||||
|
|
||||||
def _pop_and_teardown(self):
|
def _pop_and_teardown(self) -> None:
|
||||||
colitem = self.stack.pop()
|
colitem = self.stack.pop()
|
||||||
self._teardown_with_finalization(colitem)
|
self._teardown_with_finalization(colitem)
|
||||||
|
|
||||||
def _callfinalizers(self, colitem) -> None:
|
def _callfinalizers(self, colitem: Node) -> None:
|
||||||
finalizers = self._finalizers.pop(colitem, None)
|
finalizers = self._finalizers.pop(colitem, None)
|
||||||
exc = None
|
exc = None
|
||||||
while finalizers:
|
while finalizers:
|
||||||
|
@ -433,7 +433,7 @@ class SetupState:
|
||||||
if exc:
|
if exc:
|
||||||
raise exc
|
raise exc
|
||||||
|
|
||||||
def _teardown_with_finalization(self, colitem) -> None:
|
def _teardown_with_finalization(self, colitem: Node) -> None:
|
||||||
self._callfinalizers(colitem)
|
self._callfinalizers(colitem)
|
||||||
colitem.teardown()
|
colitem.teardown()
|
||||||
for colitem in self._finalizers:
|
for colitem in self._finalizers:
|
||||||
|
@ -446,11 +446,11 @@ class SetupState:
|
||||||
self._teardown_with_finalization(key)
|
self._teardown_with_finalization(key)
|
||||||
assert not self._finalizers
|
assert not self._finalizers
|
||||||
|
|
||||||
def teardown_exact(self, item, nextitem) -> None:
|
def teardown_exact(self, item: Item, nextitem: Optional[Item]) -> None:
|
||||||
needed_collectors = nextitem and nextitem.listchain() or []
|
needed_collectors = nextitem and nextitem.listchain() or []
|
||||||
self._teardown_towards(needed_collectors)
|
self._teardown_towards(needed_collectors)
|
||||||
|
|
||||||
def _teardown_towards(self, needed_collectors) -> None:
|
def _teardown_towards(self, needed_collectors: List[Node]) -> None:
|
||||||
exc = None
|
exc = None
|
||||||
while self.stack:
|
while self.stack:
|
||||||
if self.stack == needed_collectors[: len(self.stack)]:
|
if self.stack == needed_collectors[: len(self.stack)]:
|
||||||
|
@ -465,7 +465,7 @@ class SetupState:
|
||||||
if exc:
|
if exc:
|
||||||
raise exc
|
raise exc
|
||||||
|
|
||||||
def prepare(self, colitem) -> 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.
|
||||||
|
|
Loading…
Reference in New Issue