Skip to content

fix: iter_markers/get_closest_marker returns correct closest MRO marker (#14329) - #14630

Open
RonnyPfannschmidt wants to merge 9 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:fix/iter-markers-mro-closest-first
Open

RonnyPfannschmidt wants to merge 9 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:fix/iter-markers-mro-closest-first

Conversation

@RonnyPfannschmidt

@RonnyPfannschmidt RonnyPfannschmidt commented Jun 20, 2026 •

Copy link
Copy Markdown
Member

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_marker returning the wrong marker with class inheritance).

Refs #10406: fixes its filterwarnings case. 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.

  • Introduces Node._iter_own_markers_closest_first(), overridden by Class to walk the MRO in natural closest-first order while preserving decorator stacking order within each class
  • Does not change own_markers order — it stays in base-first (construction) order
  • Does not change usefixtures or parametrize ordering — see below

Key difference from #14332

#14332 reverses the MRO traversal in get_unpacked_marks, which changes own_markers order on Class nodes. This approach keeps own_markers unchanged 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.obj does
self.keywords.update((mark.name, mark) for mark in self.own_markers) — last wins — so with
base-first own_markers the closest mark is the one that ends up in keywords. Reversing
own_markers silently flips that to the base mark:

@pytest.mark.foo("base")
class TestBase: pass

@pytest.mark.foo("child")
class TestChild(TestBase):
    def test_it(self, request):
        request.node.parent.keywords["foo"].args[0]  # "child" here / on main, "base" with #14332

No test covers this today (test_collection.py uses distinct mark names across levels), so it
goes unnoticed. It is fixable in #14332 with a reversed(), but it is the reason this PR
targets 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 private MarkerOrder, and every consumer names the
order 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 byte
    identical to main, including stacked decorators on one node.

  • parametrize (pytest_generate_tests) — an inherited class level parametrize composes
    the same IDs as on main, so no pinned ID selection and no --last-failed cache is
    invalidated.

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 a
future 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". The
function-level case in #10406 worked only because a Function stores its parametrization marks
after its decorator marks, which inverted the order a second time. filterwarnings now uses
FARTHEST_FIRST: farthest node first, each node's markers as stored. For decorators stacked on
one object, the documented order still applies (test_filterwarnings_closest_mark_wins pins it).

Not covered here: for lookups, a Function's parametrization marks still come after its
decorator marks. So for get_closest_marker, skipif and xfail, a pytest.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 the
FunctionDefinition node owning the decorator marks, the node chain gives that order for free.

Behaviour change: skipif/xfail with class inheritance

These 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 own xfail. This is the #14329
fix applied to those marks: a method's mark already beat its class's. It is pinned by
test_xfail_subclass_mark_beats_base_class and test_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 parametrize IDs changed from [1-x] to
[x-1]. That had the direction backwards: main produces [x-1], and it was this branch
that produced [1-x]. Either way it is moot now — the ID order is restored and pinned.

An earlier revision also reversed usefixtures iteration wholesale, which flipped setup order
from function → class → module to module → class → function. That was a much larger
behaviour change than #14329 calls for and is likewise reverted; if the current order is worth
changing, it deserves its own issue.

Also here: pytestmark accepts any sequence

Answering @nicoddemus' review question — any sequence will do, not just a list:

class TestThings:
    pytestmark = (pytest.mark.a, pytest.mark.b)   # TypeError before, works now

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=False and
module/function pytestmark still rejected them. Rather than leave the three paths
disagreeing, they now share one _as_mark_sequence() helper. str/bytes are deliberately
excluded, so an accidental pytestmark = "xfail" still reports the string itself rather than
its 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 markers
  • test_mark_mro_marker_object_reused_by_add_marker — the same Mark object reaching own_markers via both the MRO and add_marker no longer aborts collection with RuntimeError: generator raised StopIteration
  • test_mark_usefixtures_order_unaffected_by_mro — usefixtures setup order unchanged
  • test_mark_parametrize_ids_unaffected_by_mro — inherited parametrize IDs unchanged
  • test_filterwarnings_subclass_mark_beats_base_class, test_filterwarnings_closest_mark_wins: filterwarnings precedence
  • test_mark_class_own_markers_rewritten_by_plugin: own_markers.insert(0, …), add_marker(append=False) and a broken MRO run neither lose nor duplicate marks
  • Existing test_mark_mro / get_unpacked_marks tests pass unchanged (own_markers order preserved)
  • Full suite green locally (4748 passed), mypy and pre-commit clean
  • Full CI

Interaction with other open PRs

Comment thread src/_pytest/python.py Resolved
Comment thread src/_pytest/python.py Resolved
Comment thread src/_pytest/python.py Resolved
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the fix/iter-markers-mro-closest-first branch from a1f29c3 to 3c7d2d5 Compare July 14, 2026 08:40
@RonnyPfannschmidt
RonnyPfannschmidt marked this pull request as ready for review July 14, 2026 10:25
Copilot AI review requested due to automatic review settings July 14, 2026 10:25
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jul 14, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.Class to traverse class MRO in closest-first order while preserving per-class decorator stacking.
  • Adjust usefixtures processing 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.

Comment thread src/_pytest/fixtures.py Resolved
Comment thread src/_pytest/python.py Resolved
Comment thread src/_pytest/python.py Resolved
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the fix/iter-markers-mro-closest-first branch from c18c2f5 to 9808ef4 Compare July 14, 2026 10:34
@Wintreist

Copy link
Copy Markdown

Hi!
Thank you for continuing to work on this PR. I am really looking forward to its appearance in the main branch of pytest. Once again, I ran into the get_closest_marker problem and will have to write a crutch until you finish the PR.

@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the fix/iter-markers-mro-closest-first branch from 9808ef4 to 79ce1f0 Compare July 21, 2026 08:55
@RonnyPfannschmidt RonnyPfannschmidt mentioned this pull request Jul 24, 2026
5 tasks done
RonnyPfannschmidt added a commit to RonnyPfannschmidt/pytest that referenced this pull request Aug 9, 2026
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>
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the fix/iter-markers-mro-closest-first branch from eb15725 to 35cd12e Compare August 9, 2026 06:19
RonnyPfannschmidt added a commit to RonnyPfannschmidt/pytest that referenced this pull request Aug 9, 2026
`_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>
@RonnyPfannschmidt RonnyPfannschmidt mentioned this pull request Aug 9, 2026
@Wintreist

Copy link
Copy Markdown

Hi! Is this Pull Request not forgotten?

@RonnyPfannschmidt

Copy link
Copy Markdown
Member Author

Im just mixed capacity as im at a family vacation atm

@Wintreist

Copy link
Copy Markdown

Oh, I’m sorry.
It just seemed to me that this PR was coming to an end, and everything had been put on hold for three weeks.
Have a good rest! I’m looking forward to your return.

@RonnyPfannschmidt

Copy link
Copy Markdown
Member Author

Np it's absolutely fine to gently ask

This started t the pytest sprint and I intend to complete it soon

RonnyPfannschmidt added a commit to RonnyPfannschmidt/pytest that referenced this pull request Sep 1, 2026
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>
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the fix/iter-markers-mro-closest-first branch from d9408c3 to d56c69a Compare September 1, 2026 19:18
RonnyPfannschmidt added a commit to RonnyPfannschmidt/pytest that referenced this pull request Sep 15, 2026
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>
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the fix/iter-markers-mro-closest-first branch from ef5994f to dd2e25a Compare September 15, 2026 11:36
RonnyPfannschmidt added a commit to RonnyPfannschmidt/pytest that referenced this pull request Sep 15, 2026
`_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>
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the fix/iter-markers-mro-closest-first branch from dd2e25a to 0ded403 Compare September 23, 2026 07:00
RonnyPfannschmidt added a commit to RonnyPfannschmidt/pytest that referenced this pull request Sep 23, 2026
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>
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the fix/iter-markers-mro-closest-first branch from 0ded403 to 16b7b74 Compare October 4, 2026 13:06
RonnyPfannschmidt added a commit to RonnyPfannschmidt/pytest that referenced this pull request Oct 4, 2026
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>
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the fix/iter-markers-mro-closest-first branch from 16b7b74 to 42fe8f5 Compare October 11, 2026 07:26
RonnyPfannschmidt added a commit to RonnyPfannschmidt/pytest that referenced this pull request Oct 11, 2026
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>
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the fix/iter-markers-mro-closest-first branch from 42fe8f5 to 0983b0e Compare October 14, 2026 20:28
RonnyPfannschmidt added a commit to RonnyPfannschmidt/pytest that referenced this pull request Oct 14, 2026
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>
Comment thread src/_pytest/fixtures.py
Comment on lines +1956 to +1958
for marker_node, mark in node._iter_markers_with_node(
name="usefixtures", closest_first=False
):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/_pytest/python.py Outdated
yield from own_markers
return

start, closest_first = self._mro_marks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread changelog/14329.bugfix.rst Outdated
Comment on lines +1 to +3
: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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found by Claude

skipif and xfail pick a different mark:

https://github.com/pytest-dev/pytest/blob/f4897e825b0a418861f8abaccd14cddcd59e748a/src/_pytest/skipping.py#L171-L170

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/_pytest/nodes.py
) -> Iterator[tuple[Node, Mark]]:
"""Iterate over all markers of the node.

The markers are returned from closest to farthest.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to add *, closest_first: bool = True as a public parameter.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Wintreist

Copy link
Copy Markdown

Hi!
Is there any information about this PR?

RonnyPfannschmidt and others added 7 commits November 16, 2026 10:08
…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>
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the fix/iter-markers-mro-closest-first branch from 14c7943 to c6971c5 Compare November 16, 2026 09:09
RonnyPfannschmidt and others added 2 commits November 16, 2026 10:23
`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>
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the fix/iter-markers-mro-closest-first branch from c6971c5 to 6c2c37b Compare November 16, 2026 09:59

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants