Skip to content

Commit 892fc31

Browse files
expand marker test debugging and restore maker discovery when obj is passed trough
1 parent cbd9e89 commit 892fc31

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

‎src/_pytest/nodes.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,15 @@ def __call__(cls, *k: object, **kw: object) -> NoReturn:
107107
def _create(cls: type[_T], *k: Any, **kw: Any) -> _T:
108108
try:
109109
return super().__call__(*k, **kw) # type: ignore[no-any-return,misc]
110-
except TypeError:
110+
except TypeError as e:
111111
sig = signature(getattr(cls, "__init__"))
112112
known_kw = {k: v for k, v in kw.items() if k in sig.parameters}
113113
from .warning_types import PytestDeprecationWarning
114114

115115
warnings.warn(
116116
PytestDeprecationWarning(
117117
f"{cls} is not using a cooperative constructor and only takes {set(known_kw)}.\n"
118+
f"Exception: {e}\n"
118119
"See https://docs.pytest.org/en/stable/deprecations.html"
119120
"#constructors-of-custom-pytest-node-subclasses-should-take-kwargs "
120121
"for more details."

‎src/_pytest/python.py‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ class PyobjMixin(nodes.Node):
255255
as its intended to always mix in before a node
256256
its position in the mro is unaffected"""
257257

258+
def __init__(self, *k, obj: Any | None = None, **kw):
259+
super().__init__(*k, **kw)
260+
self._assign_obj_with_markers(obj)
261+
258262
_ALLOW_MARKERS = True
259263

260264
@property
@@ -279,19 +283,23 @@ def instance(self) -> object | None:
279283
# Overridden by Function.
280284
return None
281285

286+
def _assign_obj_with_markers(self, obj: Any | None):
287+
self._obj = obj
288+
# XXX evil hack
289+
# used to avoid Function marker duplication
290+
if self._ALLOW_MARKERS and obj is not None:
291+
self.own_markers.extend(get_unpacked_marks(self.obj))
292+
# This assumes that `obj` is called before there is a chance
293+
# to add custom keys to `self.keywords`, so no fear of overriding.
294+
self.keywords.update((mark.name, mark) for mark in self.own_markers)
295+
282296
@property
283297
def obj(self) -> Any:
284298
"""Underlying Python object."""
285299
obj = getattr(self, "_obj", None)
286300
if obj is None:
287-
self._obj = obj = self._getobj()
288-
# XXX evil hack
289-
# used to avoid Function marker duplication
290-
if self._ALLOW_MARKERS:
291-
self.own_markers.extend(get_unpacked_marks(self.obj))
292-
# This assumes that `obj` is called before there is a chance
293-
# to add custom keys to `self.keywords`, so no fear of overriding.
294-
self.keywords.update((mark.name, mark) for mark in self.own_markers)
301+
obj = self._getobj()
302+
self._assign_obj_with_markers(obj)
295303
return obj
296304

297305
@obj.setter

‎testing/test_mark.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,14 @@ def test_has_inherited(self):
582582
has_own, has_inherited = items
583583
has_own_marker = has_own.get_closest_marker("c")
584584
has_inherited_marker = has_inherited.get_closest_marker("c")
585+
586+
for item in items:
587+
print(item)
588+
for node in item.iter_parents():
589+
print(" ", node)
590+
for marker in node.own_markers:
591+
print(" ", marker)
592+
585593
assert has_own_marker is not None
586594
assert has_inherited_marker is not None
587595
assert has_own_marker.kwargs == {"location": "function"}

0 commit comments

Comments
 (0)