nose: type annotate with some resulting refactoring

This commit is contained in:
Ran Benita 2021-01-14 17:42:38 +02:00
parent 096bae6c68
commit 3dde519f53
1 changed files with 21 additions and 19 deletions

View File

@ -1,33 +1,35 @@
"""Run testsuites written for nose.""" """Run testsuites written for nose."""
from _pytest import python
from _pytest import unittest
from _pytest.config import hookimpl from _pytest.config import hookimpl
from _pytest.fixtures import getfixturemarker from _pytest.fixtures import getfixturemarker
from _pytest.nodes import Item from _pytest.nodes import Item
from _pytest.python import Function
from _pytest.unittest import TestCaseFunction
@hookimpl(trylast=True) @hookimpl(trylast=True)
def pytest_runtest_setup(item) -> None: def pytest_runtest_setup(item: Item) -> None:
if is_potential_nosetest(item): if not isinstance(item, Function):
if not call_optional(item.obj, "setup"): return
# Call module level setup if there is no object level one. # Don't do nose style setup/teardown on direct unittest style classes.
call_optional(item.parent.obj, "setup") if isinstance(item, TestCaseFunction):
# XXX This implies we only call teardown when setup worked. return
item.addfinalizer(lambda: teardown_nose(item))
# Capture the narrowed type of item for the teardown closure,
# see https://github.com/python/mypy/issues/2608
func = item
def teardown_nose(item) -> None: if not call_optional(func.obj, "setup"):
if is_potential_nosetest(item): # Call module level setup if there is no object level one.
if not call_optional(item.obj, "teardown"): assert func.parent is not None
call_optional(item.parent.obj, "teardown") call_optional(func.parent.obj, "setup") # type: ignore[attr-defined]
def teardown_nose() -> None:
if not call_optional(func.obj, "teardown"):
assert func.parent is not None
call_optional(func.parent.obj, "teardown") # type: ignore[attr-defined]
def is_potential_nosetest(item: Item) -> bool: # XXX This implies we only call teardown when setup worked.
# Extra check needed since we do not do nose style setup/teardown func.addfinalizer(teardown_nose)
# on direct unittest style classes.
return isinstance(item, python.Function) and not isinstance(
item, unittest.TestCaseFunction
)
def call_optional(obj: object, name: str) -> bool: def call_optional(obj: object, name: str) -> bool: