Skip to content

Commit 5336ba2

Browse files
authored
Merge pull request #8218 from bluetech/reports2
Misc small code improvements
2 parents 8e00df4 + 2ff8809 commit 5336ba2

File tree

6 files changed

+47
-32
lines changed

6 files changed

+47
-32
lines changed

src/_pytest/fixtures.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from typing import Iterable
1717
from typing import Iterator
1818
from typing import List
19+
from typing import MutableMapping
1920
from typing import Optional
2021
from typing import overload
2122
from typing import Sequence
@@ -525,9 +526,10 @@ def fspath(self) -> py.path.local:
525526
return self._pyfuncitem.fspath # type: ignore
526527

527528
@property
528-
def keywords(self):
529+
def keywords(self) -> MutableMapping[str, Any]:
529530
"""Keywords/markers dictionary for the underlying node."""
530-
return self.node.keywords
531+
node: nodes.Node = self.node
532+
return node.keywords
531533

532534
@property
533535
def session(self) -> "Session":
@@ -607,14 +609,11 @@ def _get_active_fixturedef(
607609
def _get_fixturestack(self) -> List["FixtureDef[Any]"]:
608610
current = self
609611
values: List[FixtureDef[Any]] = []
610-
while 1:
611-
fixturedef = getattr(current, "_fixturedef", None)
612-
if fixturedef is None:
613-
values.reverse()
614-
return values
615-
values.append(fixturedef)
616-
assert isinstance(current, SubRequest)
612+
while isinstance(current, SubRequest):
613+
values.append(current._fixturedef) # type: ignore[has-type]
617614
current = current._parent_request
615+
values.reverse()
616+
return values
618617

619618
def _compute_fixture_value(self, fixturedef: "FixtureDef[object]") -> None:
620619
"""Create a SubRequest based on "self" and call the execute method

src/_pytest/nose.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
from _pytest import python
33
from _pytest import unittest
44
from _pytest.config import hookimpl
5+
from _pytest.fixtures import getfixturemarker
56
from _pytest.nodes import Item
67

78

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

1718

18-
def teardown_nose(item):
19+
def teardown_nose(item) -> None:
1920
if is_potential_nosetest(item):
2021
if not call_optional(item.obj, "teardown"):
2122
call_optional(item.parent.obj, "teardown")
@@ -29,11 +30,16 @@ def is_potential_nosetest(item: Item) -> bool:
2930
)
3031

3132

32-
def call_optional(obj, name):
33+
def call_optional(obj: object, name: str) -> bool:
3334
method = getattr(obj, name, None)
34-
isfixture = hasattr(method, "_pytestfixturefunction")
35-
if method is not None and not isfixture and callable(method):
36-
# If there's any problems allow the exception to raise rather than
37-
# silently ignoring them.
38-
method()
39-
return True
35+
if method is None:
36+
return False
37+
is_fixture = getfixturemarker(method) is not None
38+
if is_fixture:
39+
return False
40+
if not callable(method):
41+
return False
42+
# If there are any problems allow the exception to raise rather than
43+
# silently ignoring it.
44+
method()
45+
return True

src/_pytest/python.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -922,10 +922,6 @@ def copy(self) -> "CallSpec2":
922922
cs._idlist = list(self._idlist)
923923
return cs
924924

925-
def _checkargnotcontained(self, arg: str) -> None:
926-
if arg in self.params or arg in self.funcargs:
927-
raise ValueError(f"duplicate {arg!r}")
928-
929925
def getparam(self, name: str) -> object:
930926
try:
931927
return self.params[name]
@@ -947,7 +943,8 @@ def setmulti2(
947943
param_index: int,
948944
) -> None:
949945
for arg, val in zip(argnames, valset):
950-
self._checkargnotcontained(arg)
946+
if arg in self.params or arg in self.funcargs:
947+
raise ValueError(f"duplicate {arg!r}")
951948
valtype_for_arg = valtypes[arg]
952949
if valtype_for_arg == "params":
953950
self.params[arg] = val

src/_pytest/reports.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class BaseReport:
6565
]
6666
sections: List[Tuple[str, str]]
6767
nodeid: str
68+
outcome: "Literal['passed', 'failed', 'skipped']"
6869

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

144-
passed = property(lambda x: x.outcome == "passed")
145-
failed = property(lambda x: x.outcome == "failed")
146-
skipped = property(lambda x: x.outcome == "skipped")
145+
@property
146+
def passed(self) -> bool:
147+
return self.outcome == "passed"
148+
149+
@property
150+
def failed(self) -> bool:
151+
return self.outcome == "failed"
152+
153+
@property
154+
def skipped(self) -> bool:
155+
return self.outcome == "skipped"
147156

148157
@property
149158
def fspath(self) -> str:
@@ -348,8 +357,10 @@ class CollectReport(BaseReport):
348357
def __init__(
349358
self,
350359
nodeid: str,
351-
outcome: "Literal['passed', 'skipped', 'failed']",
352-
longrepr,
360+
outcome: "Literal['passed', 'failed', 'skipped']",
361+
longrepr: Union[
362+
None, ExceptionInfo[BaseException], Tuple[str, int, str], str, TerminalRepr
363+
],
353364
result: Optional[List[Union[Item, Collector]]],
354365
sections: Iterable[Tuple[str, str]] = (),
355366
**extra,

testing/python/integration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
from _pytest import runner
55
from _pytest._code import getfslineno
6+
from _pytest.fixtures import getfixturemarker
67
from _pytest.pytester import Pytester
78

89

@@ -334,7 +335,8 @@ def test_fix(fix):
334335
def test_pytestconfig_is_session_scoped() -> None:
335336
from _pytest.fixtures import pytestconfig
336337

337-
marker = pytestconfig._pytestfixturefunction # type: ignore
338+
marker = getfixturemarker(pytestconfig)
339+
assert marker is not None
338340
assert marker.scope == "session"
339341

340342

testing/test_terminal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,19 +2230,19 @@ class X:
22302230

22312231
ev1 = cast(CollectReport, X())
22322232
ev1.when = "execute"
2233-
ev1.skipped = True
2233+
ev1.skipped = True # type: ignore[misc]
22342234
ev1.longrepr = longrepr
22352235

22362236
ev2 = cast(CollectReport, X())
22372237
ev2.when = "execute"
22382238
ev2.longrepr = longrepr
2239-
ev2.skipped = True
2239+
ev2.skipped = True # type: ignore[misc]
22402240

22412241
# ev3 might be a collection report
22422242
ev3 = cast(CollectReport, X())
22432243
ev3.when = "collect"
22442244
ev3.longrepr = longrepr
2245-
ev3.skipped = True
2245+
ev3.skipped = True # type: ignore[misc]
22462246

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

0 commit comments

Comments
 (0)