Repository navigation
fix: iter_markers/get_closest_marker returns correct closest MRO marker (#14329) - #14630
RonnyPfannschmidt wants to merge 9 commits into
Conversation
a1f29c3 to
3c7d2d5
Compare
There was a problem hiding this comment.
Pull request overview
Fixes marker resolution for inherited test classes so iter_markers/get_closest_marker return the closest (most-derived) class’ marks first, while attempting to preserve backward-compatible own_markers ordering and existing fixture-setup behavior.
Changes:
- Add
Node._iter_own_markers_closest_first()and switch marker iteration to use it for closest-first semantics. - Override marker iteration for
python.Classto traverse class MRO in closest-first order while preserving per-class decorator stacking. - Adjust
usefixturesprocessing to preserve farthest-first fixture requesting, and add regression tests + changelog entry.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| testing/test_mark.py | Adds regression tests for closest-first MRO marker resolution and updates a mock-based test for the new iteration hook. |
| src/_pytest/python.py | Implements Class._iter_own_markers_closest_first() and introduces an override decorator compatibility shim. |
| src/_pytest/nodes.py | Routes marker iteration through _iter_own_markers_closest_first() and updates docstrings to state closest-first behavior. |
| src/_pytest/fixtures.py | Reverses usefixtures marker iteration to keep farthest-first fixture request ordering after marker iteration changes. |
| changelog/14329.bugfix.rst | Documents the bugfix for closest marker resolution with class inheritance. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
c18c2f5 to
9808ef4
Compare
|
Hi! |
9808ef4 to
79ce1f0
Compare
Address review feedback on pytest-dev#14630: * `Class._iter_own_markers_closest_first` matched MRO markers in `own_markers` by `id()` and consumed a `next()` per hit. When the same `Mark` object reached `own_markers` twice -- e.g. a module-level `mark = pytest.mark.foo(1)` used as a class decorator *and* passed to `add_marker()` -- the iterator ran dry and collection died with `RuntimeError: generator raised StopIteration`. Locate the contiguous MRO run by identity instead and substitute it wholesale, falling back to the stored order when the run is gone. * Do not force `obj` resolution from marker iteration; without a resolved `obj` there are no MRO markers to reorder anyway. * Extract `get_mro_mark_groups()` so the MRO/`pytestmark` normalization lives in one place instead of being duplicated in `python.py`, and build `get_unpacked_marks()` on top of it. * Move the `override` shim to `_pytest.compat`, next to `deprecated`, so type checkers see it on Python < 3.12. * Spell out why `_getusefixturesnames` reverses within a node too, and pin it with a test: stacked `usefixtures` decorators now request their fixtures in source order, matching `usefixtures("a", "b")`. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
eb15725 to
35cd12e
Compare
`_lookup_derived` scanned `own_markers` front-to-back and returned the first name match, but the eager `keywords.update((mark.name, mark) ...)` it replaces let *later* markers win. A Class stores MRO-inherited markers base class first, so `keywords["foo"]` silently changed from the subclass' marker to the base class' one. Scan in reverse instead. Note this direction is tied to `own_markers` order: it has to flip if pytest-dev#14332 lands rather than pytest-dev#14630. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
Hi! Is this Pull Request not forgotten? |
|
Im just mixed capacity as im at a family vacation atm |
|
Oh, I’m sorry. |
|
Np it's absolutely fine to gently ask This started t the pytest sprint and I intend to complete it soon |
Address review feedback on pytest-dev#14630: * `Class._iter_own_markers_closest_first` matched MRO markers in `own_markers` by `id()` and consumed a `next()` per hit. When the same `Mark` object reached `own_markers` twice -- e.g. a module-level `mark = pytest.mark.foo(1)` used as a class decorator *and* passed to `add_marker()` -- the iterator ran dry and collection died with `RuntimeError: generator raised StopIteration`. Record instead, once when `obj` is resolved, where the MRO run starts in `own_markers` and what it looks like closest-first, and splice it in on iteration. That makes the duplicate-`Mark` case a non-issue rather than a handled one, and turns a per-call rebuild plus O(n*m) scan -- on a path every skip/xfail lookup and fixture collection walks -- into a slice. * Do not force `obj` resolution from marker iteration; without a resolved `obj` there are no MRO markers to reorder anyway. * Extract `get_mro_mark_groups()` so the MRO/`pytestmark` normalization lives in one place instead of being duplicated in `python.py`, and build `get_unpacked_marks()` on top of it. * Move the `override` shim to `_pytest.compat`, next to `deprecated`, so type checkers see it on Python < 3.12. * `usefixtures` composes rather than shadows, so `_getusefixturesnames` asks for `own_markers` storage order: a base class' fixtures keep being requested before a subclass', and stacked decorators keep their bottom-up order. Pinned by a test that puts an MRO base class in the chain. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
d9408c3 to
d56c69a
Compare
Address review feedback on pytest-dev#14630: * `Class._iter_own_markers_closest_first` matched MRO markers in `own_markers` by `id()` and consumed a `next()` per hit. When the same `Mark` object reached `own_markers` twice -- e.g. a module-level `mark = pytest.mark.foo(1)` used as a class decorator *and* passed to `add_marker()` -- the iterator ran dry and collection died with `RuntimeError: generator raised StopIteration`. Record instead, once when `obj` is resolved, where the MRO run starts in `own_markers` and what it looks like closest-first, and splice it in on iteration. That makes the duplicate-`Mark` case a non-issue rather than a handled one, and turns a per-call rebuild plus O(n*m) scan -- on a path every skip/xfail lookup and fixture collection walks -- into a slice. * Do not force `obj` resolution from marker iteration; without a resolved `obj` there are no MRO markers to reorder anyway. * Extract `get_mro_mark_groups()` so the MRO/`pytestmark` normalization lives in one place instead of being duplicated in `python.py`, and build `get_unpacked_marks()` on top of it. * Move the `override` shim to `_pytest.compat`, next to `deprecated`, so type checkers see it on Python < 3.12. * `usefixtures` composes rather than shadows, so `_getusefixturesnames` asks for `own_markers` storage order: a base class' fixtures keep being requested before a subclass', and stacked decorators keep their bottom-up order. Pinned by a test that puts an MRO base class in the chain. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
ef5994f to
dd2e25a
Compare
`_lookup_derived` scanned `own_markers` front-to-back and returned the first name match, but the eager `keywords.update((mark.name, mark) ...)` it replaces let *later* markers win. A Class stores MRO-inherited markers base class first, so `keywords["foo"]` silently changed from the subclass' marker to the base class' one. Scan in reverse instead. Note this direction is tied to `own_markers` order: it has to flip if pytest-dev#14332 lands rather than pytest-dev#14630. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
dd2e25a to
0ded403
Compare
Address review feedback on pytest-dev#14630: * `Class._iter_own_markers_closest_first` matched MRO markers in `own_markers` by `id()` and consumed a `next()` per hit. When the same `Mark` object reached `own_markers` twice -- e.g. a module-level `mark = pytest.mark.foo(1)` used as a class decorator *and* passed to `add_marker()` -- the iterator ran dry and collection died with `RuntimeError: generator raised StopIteration`. Record instead, once when `obj` is resolved, where the MRO run starts in `own_markers` and what it looks like closest-first, and splice it in on iteration. That makes the duplicate-`Mark` case a non-issue rather than a handled one, and turns a per-call rebuild plus O(n*m) scan -- on a path every skip/xfail lookup and fixture collection walks -- into a slice. * Do not force `obj` resolution from marker iteration; without a resolved `obj` there are no MRO markers to reorder anyway. * Extract `get_mro_mark_groups()` so the MRO/`pytestmark` normalization lives in one place instead of being duplicated in `python.py`, and build `get_unpacked_marks()` on top of it. * Move the `override` shim to `_pytest.compat`, next to `deprecated`, so type checkers see it on Python < 3.12. * `usefixtures` composes rather than shadows, so `_getusefixturesnames` asks for `own_markers` storage order: a base class' fixtures keep being requested before a subclass', and stacked decorators keep their bottom-up order. Pinned by a test that puts an MRO base class in the chain. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
0ded403 to
16b7b74
Compare
Address review feedback on pytest-dev#14630: * `Class._iter_own_markers_closest_first` matched MRO markers in `own_markers` by `id()` and consumed a `next()` per hit. When the same `Mark` object reached `own_markers` twice -- e.g. a module-level `mark = pytest.mark.foo(1)` used as a class decorator *and* passed to `add_marker()` -- the iterator ran dry and collection died with `RuntimeError: generator raised StopIteration`. Record instead, once when `obj` is resolved, where the MRO run starts in `own_markers` and what it looks like closest-first, and splice it in on iteration. That makes the duplicate-`Mark` case a non-issue rather than a handled one, and turns a per-call rebuild plus O(n*m) scan -- on a path every skip/xfail lookup and fixture collection walks -- into a slice. * Do not force `obj` resolution from marker iteration; without a resolved `obj` there are no MRO markers to reorder anyway. * Extract `get_mro_mark_groups()` so the MRO/`pytestmark` normalization lives in one place instead of being duplicated in `python.py`, and build `get_unpacked_marks()` on top of it. * Move the `override` shim to `_pytest.compat`, next to `deprecated`, so type checkers see it on Python < 3.12. * `usefixtures` composes rather than shadows, so `_getusefixturesnames` asks for `own_markers` storage order: a base class' fixtures keep being requested before a subclass', and stacked decorators keep their bottom-up order. Pinned by a test that puts an MRO base class in the chain. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
16b7b74 to
42fe8f5
Compare
Address review feedback on pytest-dev#14630: * `Class._iter_own_markers_closest_first` matched MRO markers in `own_markers` by `id()` and consumed a `next()` per hit. When the same `Mark` object reached `own_markers` twice -- e.g. a module-level `mark = pytest.mark.foo(1)` used as a class decorator *and* passed to `add_marker()` -- the iterator ran dry and collection died with `RuntimeError: generator raised StopIteration`. Record instead, once when `obj` is resolved, where the MRO run starts in `own_markers` and what it looks like closest-first, and splice it in on iteration. That makes the duplicate-`Mark` case a non-issue rather than a handled one, and turns a per-call rebuild plus O(n*m) scan -- on a path every skip/xfail lookup and fixture collection walks -- into a slice. * Do not force `obj` resolution from marker iteration; without a resolved `obj` there are no MRO markers to reorder anyway. * Extract `get_mro_mark_groups()` so the MRO/`pytestmark` normalization lives in one place instead of being duplicated in `python.py`, and build `get_unpacked_marks()` on top of it. * Move the `override` shim to `_pytest.compat`, next to `deprecated`, so type checkers see it on Python < 3.12. * `usefixtures` composes rather than shadows, so `_getusefixturesnames` asks for `own_markers` storage order: a base class' fixtures keep being requested before a subclass', and stacked decorators keep their bottom-up order. Pinned by a test that puts an MRO base class in the chain. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
42fe8f5 to
0983b0e
Compare
Address review feedback on pytest-dev#14630: * `Class._iter_own_markers_closest_first` matched MRO markers in `own_markers` by `id()` and consumed a `next()` per hit. When the same `Mark` object reached `own_markers` twice -- e.g. a module-level `mark = pytest.mark.foo(1)` used as a class decorator *and* passed to `add_marker()` -- the iterator ran dry and collection died with `RuntimeError: generator raised StopIteration`. Record instead, once when `obj` is resolved, where the MRO run starts in `own_markers` and what it looks like closest-first, and splice it in on iteration. That makes the duplicate-`Mark` case a non-issue rather than a handled one, and turns a per-call rebuild plus O(n*m) scan -- on a path every skip/xfail lookup and fixture collection walks -- into a slice. * Do not force `obj` resolution from marker iteration; without a resolved `obj` there are no MRO markers to reorder anyway. * Extract `get_mro_mark_groups()` so the MRO/`pytestmark` normalization lives in one place instead of being duplicated in `python.py`, and build `get_unpacked_marks()` on top of it. * Move the `override` shim to `_pytest.compat`, next to `deprecated`, so type checkers see it on Python < 3.12. * `usefixtures` composes rather than shadows, so `_getusefixturesnames` asks for `own_markers` storage order: a base class' fixtures keep being requested before a subclass', and stacked decorators keep their bottom-up order. Pinned by a test that puts an MRO base class in the chain. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
| for marker_node, mark in node._iter_markers_with_node( | ||
| name="usefixtures", closest_first=False | ||
| ): |
There was a problem hiding this comment.
Found by Claude
The same fix needs to happen in warnings.py, because filterwarnings is applied in turn the last one applied wins. Example:
@pytest.mark.filterwarnings("error")
class TestBase:
pass
@pytest.mark.filterwarnings("ignore")
class TestChild(TestBase):
def test_subclass_ignore_beats_base_error(self):
warn() # warnings.warn("boom", UserWarning)
@pytest.mark.filterwarnings("error")
class TestFuncUnderClass:
@pytest.mark.filterwarnings("ignore")
def test_function_ignore_beats_class_error(self):
warn()TestChild::test_subclass_ignore_beats_base_error passes on main and fails on this branch.
TestFuncUnderClass::test_function_ignore_beats_class_error fails in both, and is separate and older, but still worthwhile reviewing/fixing while we are at this.
There was a problem hiding this comment.
Written by Claude Opus 5.5 via Claude Code for @nicoddemus; I prompted it, it did the work, I read it.
Confirmed both cases. The subclass-vs-base regression is fixed by applying filterwarnings in stored order. The older function-under-class case is #10406, now fixed here too: marks are applied farthest node first, each node's as stored. Both of your examples and #10406's are in test_filterwarnings_closest_mark_wins.
| yield from own_markers | ||
| return | ||
|
|
||
| start, closest_first = self._mro_marks |
There was a problem hiding this comment.
Found by Claude
self._mro_marks can go stale if a plugin calls .own_markers.insert(0, mark), which is likely something that should be rare but still possible.
There was a problem hiding this comment.
Written by Claude Opus 5.5 via Claude Code for @nicoddemus; I prompted it, it did the work, I read it.
Confirmed, and worse than stale: insert(0, mark) lost the inserted mark and yielded a class mark twice. The run is now checked by identity at its recorded position, found again if it moved, and iterated as stored if it was broken up. That also replaced the add_marker override. It's tested for insert, prepend and a broken run.
| :meth:`~_pytest.nodes.Node.iter_markers` and :meth:`~_pytest.nodes.Node.get_closest_marker` | ||
| now walk the class MRO closest-first, so a marker on a subclass takes precedence over the | ||
| same marker on one of its base classes. |
There was a problem hiding this comment.
Found by Claude
skipif and xfail pick a different mark:
They use the first matching mark, which is now the subclass's. Example: the base class has xfail(strict=True, raises=ValueError) and the subclass has xfail(reason=...). A test that passes unexpectedly used to fail because of strict; now it passes silently. No test covers either the old or the new behaviour.
There was a problem hiding this comment.
Written by Claude Opus 5.5 via Claude Code for @nicoddemus; I prompted it, it did the work, I read it.
That change is intended: it's #14329 for those marks, and a method's mark already beat its class's. Tests now pin the new behaviour, and the changelog names skipif/xfail with the strict=True example.
| ) -> Iterator[tuple[Node, Mark]]: | ||
| """Iterate over all markers of the node. | ||
|
|
||
| The markers are returned from closest to farthest. |
There was a problem hiding this comment.
Seems reasonable to add *, closest_first: bool = True as a public parameter.
There was a problem hiding this comment.
Nevermind: closest_first=False does not mean farthest-first, gives a mixed order, so making this public is misleading.
If we were to make it public, we could use an enum instead, which we could give a proper name to the current order and even introduce "farthest-first".
There was a problem hiding this comment.
Written by Claude Opus 5.5 via Claude Code for @nicoddemus; I prompted it, it did the work, I read it.
Agreed on the mixed order. It stays private, but it's now an enum (CLOSEST_FIRST, STORED, FARTHEST_FIRST), since #10406 needed a third order anyway.
|
Hi! |
…O marker Fix get_closest_marker and iter_markers to return markers in correct closest-first order when class inheritance (MRO) is involved (pytest-dev#14329). Previously, own_markers on Class nodes stored MRO-inherited markers in base-first (farthest) order, and iter_markers yielded them in that same order. This caused get_closest_marker to return a base class marker instead of the overriding child class marker. The fix introduces _iter_own_markers_closest_first() on Node, overridden by Class to walk the MRO in natural closest-first order while preserving decorator stacking order within each class. This avoids changing own_markers, which keeps its base-first construction order, so that keywords (populated last-wins) still resolves to the closest mark. Closest-first is what marker *lookup* wants, but not what the consumers that *compose* markers want: they have to keep running base-first or they silently reorder their output. So _iter_markers_with_node() takes a closest_first flag, and pytest_generate_tests() asks for storage order -- an inherited class level parametrize keeps composing base-first and the generated IDs stay what they were. Alternative structural approach to PR pytest-dev#14332 that preserves own_markers order for backward compatibility. filterwarnings composes too -- the filter applied last wins -- so it asks for storage order as well, keeping a subclass' filter ahead of its base class'. Co-authored-by: Cursor AI <ai@cursor.sh> Co-authored-by: Anthropic Claude Opus 4.6 <claude@anthropic.com> Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 via Claude Code <noreply@anthropic.com>
- Add @OverRide decorator to Class._iter_own_markers_closest_first (with version-gated import for Python <3.12) - Broaden isinstance check from list to Sequence for cls_marks - Add test for dynamically added markers on Class collectors Co-authored-by: Cursor AI <ai@cursor.sh> Co-authored-by: Anthropic Claude Opus 4.6 <claude@anthropic.com>
Co-authored-by: Cursor AI <ai@cursor.sh> Co-authored-by: Anthropic Claude Opus 4.6 <claude@anthropic.com> Co-authored-by: Cursor <cursoragent@cursor.com>
Dynamically added markers on a Class collector were always yielded after the MRO-inherited markers, so add_marker(append=False) no longer put the marker closest as it does on a plain node. Iterate own_markers in stored order and substitute the MRO markers in closest-first order at their slots, keeping dynamic markers in the position add_marker gave them. Adds a test asserting prepend/append ordering relative to the MRO markers. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Address review feedback on pytest-dev#14630: * `Class._iter_own_markers_closest_first` matched MRO markers in `own_markers` by `id()` and consumed a `next()` per hit. When the same `Mark` object reached `own_markers` twice -- e.g. a module-level `mark = pytest.mark.foo(1)` used as a class decorator *and* passed to `add_marker()` -- the iterator ran dry and collection died with `RuntimeError: generator raised StopIteration`. Record instead, once when `obj` is resolved, where the MRO run starts in `own_markers` and what it looks like closest-first, and splice it in on iteration. That makes the duplicate-`Mark` case a non-issue rather than a handled one, and turns a per-call rebuild plus O(n*m) scan -- on a path every skip/xfail lookup and fixture collection walks -- into a slice. * Do not force `obj` resolution from marker iteration; without a resolved `obj` there are no MRO markers to reorder anyway. * Extract `get_mro_mark_groups()` so the MRO/`pytestmark` normalization lives in one place instead of being duplicated in `python.py`, and build `get_unpacked_marks()` on top of it. * Move the `override` shim to `_pytest.compat`, next to `deprecated`, so type checkers see it on Python < 3.12. * `usefixtures` composes rather than shadows, so `_getusefixturesnames` asks for `own_markers` storage order: a base class' fixtures keep being requested before a subclass', and stacked decorators keep their bottom-up order. Pinned by a test that puts an MRO base class in the chain. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The regression test for pytest-dev#14329 is Wintreist's, taken verbatim from pytest-dev#14332; carry the attribution and add them to AUTHORS. Also spell out in the changelog what does *not* change: `own_markers` keeps its base-first order, and so do the consumers that compose markers rather than look one up. `usefixtures` requests its fixtures in the same order as before, and an inherited class level `parametrize` composes the same test IDs as before -- no pinned ID selection and no `--last-failed` cache is invalidated by this fix. Co-authored-by: Wintreist <49996562+Wintreist@users.noreply.github.com> Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 via Claude Code <noreply@anthropic.com>
Answering Bruno's review question: any sequence will do, not just a list. Previously a tuple worked nowhere, and the closest-first MRO walk in this branch had quietly started accepting one for inherited class marks only -- so make it consistent across modules, classes and functions instead of leaving the paths disagreeing. `str`/`bytes` are deliberately not treated as sequences, so an accidental `pytestmark = "xfail"` keeps reporting the string itself rather than its first character. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
14c7943 to
c6971c5
Compare
`own_markers` is a public list. The MRO run's start index was recorded once and only `add_marker(append=False)` kept it current, so a plugin calling `own_markers.insert(0, mark)` made marker iteration splice the run in at the stale offset: the inserted mark was lost and a class mark was yielded twice. Only a run that no longer fit at all was caught. Remember the run as stored, check by identity that it is still at the recorded offset, and otherwise look for it once and remember the new offset. That also covers `add_marker(append=False)`, so its override goes. A run that was broken up is iterated as stored. The run is matched whole rather than mark by mark -- per-mark `id()` matching is what broke when one `Mark` object sat in `own_markers` twice. Pin the fallbacks the suite never reached, from `pytest_collectstart`, where a plugin inspecting or rewriting markers during collection runs: * iterating a Class collector's markers before `obj` is resolved -- the "do not force resolution as a side effect" behaviour that came out of review; * a plugin emptying `own_markers`, inserting before the run, prepending with `add_marker`, or breaking the run. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-Authored-By: Claude Code <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 via Claude Code <noreply@anthropic.com>
`warnings.filterwarnings()` inserts at the front of the filter list, so
the filter applied last wins. `catch_warnings_for_item` applied the marks
closest node first, so the farthest one won: a module or class level
`filterwarnings("error")` overrode a test's own `"ignore"`. Within a
Function the parametrization marks are stored after the decorator marks,
which inverted the order a second time and made the function-level case
work by accident; the class-level one in pytest-dev#10406 did not.
Walk the node chain farthest-first and keep each node's own markers as
stored. Stored order is already farthest-first where it matters -- MRO
base class first, decorator marks before parametrization marks -- and it
keeps the documented rule for decorators stacked on one object.
That is a third marker order, so replace the private `closest_first`
flag with a `MarkerOrder` enum naming all three: CLOSEST_FIRST for
lookup, STORED for the consumers that compose (usefixtures,
parametrize), FARTHEST_FIRST for filterwarnings. Lookups still yield a
Function's decorator marks before its parametrization marks; moving
those apart is left to the FunctionDefinition node (pytest-dev#14769).
Co-Authored-By: Claude Opus 5.5 via Claude Code <noreply@anthropic.com>
c6971c5 to
6c2c37b
Compare
Written by Claude Opus 5, 4.6, 4.8 and 5.5 via Cursor and Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
Alternative approach to #14332 for fixing #14329 (
get_closest_markerreturning the wrong marker with class inheritance).Refs #10406: fixes its
filterwarningscase. Marks on a closer level now take precedence over farther ones (parametrization over function over class over module).The regression test is Wintreist's, taken from #14332 — thanks for it, and for the patience.
Node._iter_own_markers_closest_first(), overridden byClassto walk the MRO in natural closest-first order while preserving decorator stacking order within each classown_markersorder — it stays in base-first (construction) orderusefixturesorparametrizeordering — see belowKey difference from #14332
#14332 reverses the MRO traversal in
get_unpacked_marks, which changesown_markersorder onClassnodes. This approach keepsown_markersunchanged and fixes the iteration layer (iter_markers/get_closest_marker), which is the actual consumer that needs closest-first semantics.The concrete difference is
keywords.PyobjMixin.objdoesself.keywords.update((mark.name, mark) for mark in self.own_markers)— last wins — so withbase-first
own_markersthe closest mark is the one that ends up inkeywords. Reversingown_markerssilently flips that to the base mark:No test covers this today (
test_collection.pyuses distinct mark names across levels), so itgoes unnoticed. It is fixable in #14332 with a
reversed(), but it is the reason this PRtargets iteration instead.
Lookup is closest-first; composition stays base-first
Closest-first is what marker lookup wants. It is emphatically not what the consumers that
compose markers want — they have to keep running base-first or they silently reorder their
own output. So
_iter_markers_with_node()takes a privateMarkerOrder, and every consumer names theorder it needs:
CLOSEST_FIRST: lookup (iter_markers,get_closest_marker,skipif/xfail).STORED: closest node first, each node's markers as stored. Used by the consumers that compose:usefixtures(FixtureManager._getusefixturesnames) — fixture setup order is byteidentical to
main, including stacked decorators on one node.parametrize(pytest_generate_tests) — an inherited class levelparametrizecomposesthe same IDs as on
main, so no pinned ID selection and no--last-failedcache isinvalidated.
Both are pinned by tests (
test_mark_usefixtures_order_unaffected_by_mro,test_mark_parametrize_ids_unaffected_by_mro) that put an MRO base class in the chain, so afuture change to marker iteration cannot leak into them unnoticed.
filterwarnings: the closest mark wins (refs #10406)The filter applied last wins, and marks used to be applied closest node first, so the farthest
mark won. A module or class
filterwarnings("error")overrode a test's own"ignore". Thefunction-level case in #10406 worked only because a
Functionstores its parametrization marksafter its decorator marks, which inverted the order a second time.
filterwarningsnow usesFARTHEST_FIRST: farthest node first, each node's markers as stored. For decorators stacked onone object, the documented order still applies (
test_filterwarnings_closest_mark_winspins it).Not covered here: for lookups, a
Function's parametrization marks still come after itsdecorator marks. So for
get_closest_marker,skipifandxfail, apytest.param(marks=...)mark still loses to a decorator mark on the same function. Reordering inside
Function.own_markers(as #14782 did) would get in the way of #14769. With theFunctionDefinition node owning the decorator marks, the node chain gives that order for free.
Behaviour change:
skipif/xfailwith class inheritanceThese use the first matching mark, which is now the subclass's. For example, a base class's
xfail(strict=True)no longer applies when the subclass has its ownxfail. This is the #14329fix applied to those marks: a method's mark already beat its class's. It is pinned by
test_xfail_subclass_mark_beats_base_classandtest_skipif_subclass_mark_beats_base_class,and named in the changelog.
Correction to an earlier version of this description
An earlier revision of this PR claimed inherited
parametrizeIDs changed from[1-x]to[x-1]. That had the direction backwards:mainproduces[x-1], and it was this branchthat produced
[1-x]. Either way it is moot now — the ID order is restored and pinned.An earlier revision also reversed
usefixturesiteration wholesale, which flipped setup orderfrom
function → class → moduletomodule → class → function. That was a much largerbehaviour change than #14329 calls for and is likewise reverted; if the current order is worth
changing, it deserves its own issue.
Also here:
pytestmarkaccepts any sequenceAnswering @nicoddemus' review question — any sequence will do, not just a
list:This started as an accident: routing the MRO walk through the new
get_mro_mark_groups()made tuples work for inherited class marks only, while
consider_mro=Falseandmodule/function
pytestmarkstill rejected them. Rather than leave the three pathsdisagreeing, they now share one
_as_mark_sequence()helper.str/bytesare deliberatelyexcluded, so an accidental
pytestmark = "xfail"still reports the string itself rather thanits first character. Separate
changelog/14630.improvement.rst.Test plan
test_mark_closest_mro— closest-first MRO marker resolution (from BugFix #14329 #14332)test_mark_closest_mro_with_dynamic_class_marker—add_marker(append=)keeps its position relative to MRO markerstest_mark_mro_marker_object_reused_by_add_marker— the sameMarkobject reachingown_markersvia both the MRO andadd_markerno longer aborts collection withRuntimeError: generator raised StopIterationtest_mark_usefixtures_order_unaffected_by_mro— usefixtures setup order unchangedtest_mark_parametrize_ids_unaffected_by_mro— inherited parametrize IDs unchangedtest_filterwarnings_subclass_mark_beats_base_class,test_filterwarnings_closest_mark_wins: filterwarnings precedencetest_mark_class_own_markers_rewritten_by_plugin:own_markers.insert(0, …),add_marker(append=False)and a broken MRO run neither lose nor duplicate markstest_mark_mro/get_unpacked_markstests pass unchanged (own_markersorder preserved)Interaction with other open PRs
keywordslazily and resolves a name by scanningown_markersfirst-wins,where today's
dict.updateis last-wins. On top of this PR that flipsClass.keywords["foo"]to the base class mark; it needsreversed()there (and the oppositeif BugFix #14329 #14332 lands instead).