Skip to content

Commit

Permalink
Merge pull request #8218 from bluetech/reports2
Browse files Browse the repository at this point in the history
Misc small code improvements
  • Loading branch information
bluetech committed Jan 4, 2021
2 parents 8e00df4 + 2ff8809 commit 5336ba2
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 32 deletions.
17 changes: 8 additions & 9 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing import Iterable
from typing import Iterator
from typing import List
from typing import MutableMapping
from typing import Optional
from typing import overload
from typing import Sequence
Expand Down Expand Up @@ -525,9 +526,10 @@ def fspath(self) -> py.path.local:
return self._pyfuncitem.fspath # type: ignore

@property
def keywords(self):
def keywords(self) -> MutableMapping[str, Any]:
"""Keywords/markers dictionary for the underlying node."""
return self.node.keywords
node: nodes.Node = self.node
return node.keywords

@property
def session(self) -> "Session":
Expand Down Expand Up @@ -607,14 +609,11 @@ def _get_active_fixturedef(
def _get_fixturestack(self) -> List["FixtureDef[Any]"]:
current = self
values: List[FixtureDef[Any]] = []
while 1:
fixturedef = getattr(current, "_fixturedef", None)
if fixturedef is None:
values.reverse()
return values
values.append(fixturedef)
assert isinstance(current, SubRequest)
while isinstance(current, SubRequest):
values.append(current._fixturedef) # type: ignore[has-type]
current = current._parent_request
values.reverse()
return values

def _compute_fixture_value(self, fixturedef: "FixtureDef[object]") -> None:
"""Create a SubRequest based on "self" and call the execute method
Expand Down
24 changes: 15 additions & 9 deletions src/_pytest/nose.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
from _pytest import python
from _pytest import unittest
from _pytest.config import hookimpl
from _pytest.fixtures import getfixturemarker
from _pytest.nodes import Item


@hookimpl(trylast=True)
def pytest_runtest_setup(item):
def pytest_runtest_setup(item) -> None:
if is_potential_nosetest(item):
if not call_optional(item.obj, "setup"):
# Call module level setup if there is no object level one.
Expand All @@ -15,7 +16,7 @@ def pytest_runtest_setup(item):
item.session._setupstate.addfinalizer((lambda: teardown_nose(item)), item)


def teardown_nose(item):
def teardown_nose(item) -> None:
if is_potential_nosetest(item):
if not call_optional(item.obj, "teardown"):
call_optional(item.parent.obj, "teardown")
Expand All @@ -29,11 +30,16 @@ def is_potential_nosetest(item: Item) -> bool:
)


def call_optional(obj, name):
def call_optional(obj: object, name: str) -> bool:
method = getattr(obj, name, None)
isfixture = hasattr(method, "_pytestfixturefunction")
if method is not None and not isfixture and callable(method):
# If there's any problems allow the exception to raise rather than
# silently ignoring them.
method()
return True
if method is None:
return False
is_fixture = getfixturemarker(method) is not None
if is_fixture:
return False
if not callable(method):
return False
# If there are any problems allow the exception to raise rather than
# silently ignoring it.
method()
return True
7 changes: 2 additions & 5 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,10 +922,6 @@ def copy(self) -> "CallSpec2":
cs._idlist = list(self._idlist)
return cs

def _checkargnotcontained(self, arg: str) -> None:
if arg in self.params or arg in self.funcargs:
raise ValueError(f"duplicate {arg!r}")

def getparam(self, name: str) -> object:
try:
return self.params[name]
Expand All @@ -947,7 +943,8 @@ def setmulti2(
param_index: int,
) -> None:
for arg, val in zip(argnames, valset):
self._checkargnotcontained(arg)
if arg in self.params or arg in self.funcargs:
raise ValueError(f"duplicate {arg!r}")
valtype_for_arg = valtypes[arg]
if valtype_for_arg == "params":
self.params[arg] = val
Expand Down
21 changes: 16 additions & 5 deletions src/_pytest/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class BaseReport:
]
sections: List[Tuple[str, str]]
nodeid: str
outcome: "Literal['passed', 'failed', 'skipped']"

def __init__(self, **kw: Any) -> None:
self.__dict__.update(kw)
Expand Down Expand Up @@ -141,9 +142,17 @@ def capstderr(self) -> str:
content for (prefix, content) in self.get_sections("Captured stderr")
)

passed = property(lambda x: x.outcome == "passed")
failed = property(lambda x: x.outcome == "failed")
skipped = property(lambda x: x.outcome == "skipped")
@property
def passed(self) -> bool:
return self.outcome == "passed"

@property
def failed(self) -> bool:
return self.outcome == "failed"

@property
def skipped(self) -> bool:
return self.outcome == "skipped"

@property
def fspath(self) -> str:
Expand Down Expand Up @@ -348,8 +357,10 @@ class CollectReport(BaseReport):
def __init__(
self,
nodeid: str,
outcome: "Literal['passed', 'skipped', 'failed']",
longrepr,
outcome: "Literal['passed', 'failed', 'skipped']",
longrepr: Union[
None, ExceptionInfo[BaseException], Tuple[str, int, str], str, TerminalRepr
],
result: Optional[List[Union[Item, Collector]]],
sections: Iterable[Tuple[str, str]] = (),
**extra,
Expand Down
4 changes: 3 additions & 1 deletion testing/python/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from _pytest import runner
from _pytest._code import getfslineno
from _pytest.fixtures import getfixturemarker
from _pytest.pytester import Pytester


Expand Down Expand Up @@ -334,7 +335,8 @@ def test_fix(fix):
def test_pytestconfig_is_session_scoped() -> None:
from _pytest.fixtures import pytestconfig

marker = pytestconfig._pytestfixturefunction # type: ignore
marker = getfixturemarker(pytestconfig)
assert marker is not None
assert marker.scope == "session"


Expand Down
6 changes: 3 additions & 3 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2230,19 +2230,19 @@ class X:

ev1 = cast(CollectReport, X())
ev1.when = "execute"
ev1.skipped = True
ev1.skipped = True # type: ignore[misc]
ev1.longrepr = longrepr

ev2 = cast(CollectReport, X())
ev2.when = "execute"
ev2.longrepr = longrepr
ev2.skipped = True
ev2.skipped = True # type: ignore[misc]

# ev3 might be a collection report
ev3 = cast(CollectReport, X())
ev3.when = "collect"
ev3.longrepr = longrepr
ev3.skipped = True
ev3.skipped = True # type: ignore[misc]

values = _folded_skips(Path.cwd(), [ev1, ev2, ev3])
assert len(values) == 1
Expand Down

0 comments on commit 5336ba2

Please sign in to comment.