Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ Wil Cooley
Will Riley
William Lee
Wim Glenn
Wintreist
Wouter van Ackooy
Xixi Zhao
Xuan Luong
Expand Down
4 changes: 4 additions & 0 deletions changelog/10406.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:ref:`filterwarnings <pytest.mark.filterwarnings ref>` marks on a closer level now take precedence
over marks on a farther one: a mark from parametrization over one on the test function, the function
over its class, and a class over the module. Previously the farthest mark won, so for example a
module or class level ``filterwarnings("error")`` overrode a test's own ``filterwarnings("ignore")``.
13 changes: 13 additions & 0 deletions changelog/14329.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
: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. This includes :ref:`skipif <pytest.mark.skipif ref>`
and :ref:`xfail <pytest.mark.xfail ref>`: a subclass' mark now decides, where previously the
base class' did, for example a base class' ``strict=True`` no longer applies when the
subclass has its own ``xfail``.

``own_markers`` keeps its previous (base class first) order, and so do the consumers that
compose markers rather than look one up: :ref:`usefixtures <usefixtures>` fixtures are still
requested in the same order as before, inherited class level
:ref:`parametrize <pytest.mark.parametrize ref>` markers still compose their test IDs the
same way, and a subclass' :ref:`filterwarnings <pytest.mark.filterwarnings ref>` still takes
precedence over its base class'.
2 changes: 2 additions & 0 deletions changelog/14630.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``pytestmark`` may now be any sequence of marks, for example a tuple, instead of only a
``list`` or a single mark. This applies consistently to modules, classes and functions.
4 changes: 4 additions & 0 deletions doc/en/how-to/capture-warnings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ You can also pass multiple filters to a single mark by providing multiple argume
This means in practice that filters from earlier :ref:`@pytest.mark.filterwarnings <pytest.mark.filterwarnings ref>` decorators
take precedence over filters from later decorators, as illustrated in the example above.

Across levels, the closest mark takes precedence: a mark from :ref:`parametrization <pytest.mark.parametrize ref>`
over one on the test function, the function over its class, a class over its base classes,
and a class over the module.


Filters applied using a mark take precedence over filters passed on the command line or configured
by the :confval:`filterwarnings` configuration option.
Expand Down
11 changes: 11 additions & 0 deletions src/_pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,17 @@ def running_on_ci() -> bool:
return any(os.environ.get(var) for var in env_vars)


if sys.version_info >= (3, 12):
from typing import override as override
else:
if TYPE_CHECKING:
from typing_extensions import override as override
else:

def override(func):
return func


if sys.version_info >= (3, 13): # pragma: no cover
from warnings import deprecated as deprecated
else: # pragma: no cover
Expand Down
5 changes: 4 additions & 1 deletion src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1959,7 +1959,10 @@ def _getautousenames(self, node: nodes.Node) -> Iterator[str]:

def _getusefixturesnames(self, node: nodes.Item) -> Iterator[str]:
"""Return the names of usefixtures fixtures visible to node."""
for marker_node, mark in node.iter_markers_with_node(name="usefixtures"):
# A base class' usefixtures keep being requested before a subclass'.
for marker_node, mark in node._iter_markers_with_node(
name="usefixtures", order=nodes.MarkerOrder.STORED
):
Comment on lines +1963 to +1965

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.

if not mark.args:
marker_node.warn(
PytestWarning(
Expand Down
52 changes: 36 additions & 16 deletions src/_pytest/mark/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,34 @@ def __call__(self, *args: object, **kwargs: object):
return self.with_args(*args, **kwargs)


def _as_mark_sequence(pytestmark: Any) -> Sequence[Mark | MarkDecorator]:
"""Normalize a raw ``pytestmark`` attribute value into a sequence.

A single mark may be assigned directly (``pytestmark = pytest.mark.foo``);
anything else is expected to be a sequence of marks. ``str``/``bytes`` are
not treated as sequences here, so that an accidental
``pytestmark = "foo"`` reports the string itself as the offending value
rather than its first character.
"""
if isinstance(pytestmark, Sequence) and not isinstance(pytestmark, (str, bytes)):
return pytestmark
return [pytestmark]


def get_mro_mark_groups(cls: type) -> list[list[Mark]]:
"""Obtain the marks applied directly to each class of ``cls``'s MRO.

The groups are returned closest-first (``cls`` itself, then its bases in
MRO order); within a group the marks keep their decorator stacking order.
"""
return [
list(
normalize_mark_list(_as_mark_sequence(klass.__dict__.get("pytestmark", [])))
)
for klass in cls.__mro__
]


def get_unpacked_marks(
obj: object | type,
*,
Expand All @@ -452,22 +480,16 @@ def get_unpacked_marks(
"""Obtain the unpacked marks that are stored on an object.

If obj is a class and consider_mro is true, return marks applied to
this class and all of its super-classes in MRO order. If consider_mro
is false, only return marks applied directly to this class.
this class and all of its super-classes in reversed MRO order (farthest
base class first). If consider_mro is false, only return marks applied
directly to this class.
"""
if isinstance(obj, type):
if not consider_mro:
mark_lists = [obj.__dict__.get("pytestmark", [])]
else:
mark_lists = [
x.__dict__.get("pytestmark", []) for x in reversed(obj.__mro__)
if consider_mro:
return [
mark for group in reversed(get_mro_mark_groups(obj)) for mark in group
]
mark_list = []
for item in mark_lists:
if isinstance(item, list):
mark_list.extend(item)
else:
mark_list.append(item)
mark_list = _as_mark_sequence(obj.__dict__.get("pytestmark", []))
else:
mark_attribute = getattr(obj, "pytestmark", [])
if mark_attribute is None:
Expand All @@ -481,10 +503,8 @@ def get_unpacked_marks(
stacklevel=2,
)
mark_list = []
elif isinstance(mark_attribute, list):
mark_list = mark_attribute
else:
mark_list = [mark_attribute]
mark_list = _as_mark_sequence(mark_attribute)
return list(normalize_mark_list(mark_list))


Expand Down
70 changes: 68 additions & 2 deletions src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from collections.abc import Iterable
from collections.abc import Iterator
from collections.abc import MutableMapping
from collections.abc import Sequence
import enum
from functools import cached_property
from functools import lru_cache
import os
Expand Down Expand Up @@ -52,6 +54,31 @@
SEP = "/"


class MarkerOrder(enum.Enum):
"""The orders :meth:`Node._iter_markers_with_node` can yield markers in.

A node's ``own_markers`` are stored farthest-first: a :class:`~pytest.Class`
stores its MRO base class first, a :class:`~pytest.Function` stores its
decorator marks before its parametrization marks. Marks stacked as
decorators on one object stay bottom-up in all three orders.
"""

#: Closest node first, and each node's markers closest-first (a subclass'
#: before its base class'). For looking a marker up: the first match is
#: the one that applies (#14329). A Function's parametrization marks are
#: not moved ahead of its decorator marks; that waits on the
#: FunctionDefinition node owning the decorator marks (#14769).
CLOSEST_FIRST = enum.auto()
#: Closest node first, each node's markers as stored. For consumers that
#: compose markers and must keep the order they always composed in:
#: ``usefixtures`` and ``parametrize``.
STORED = enum.auto()
#: Farthest node first, each node's markers as stored, so a closer marker
#: always comes later. For consumers where the last marker applied wins:
#: ``filterwarnings`` (#10406).
FARTHEST_FIRST = enum.auto()


def norm_sep(path: str | os.PathLike[str]) -> str:
"""Normalize path separators to forward slashes for nodeid compatibility.

Expand Down Expand Up @@ -349,9 +376,19 @@ def add_marker(self, marker: str | MarkDecorator, append: bool = True) -> None:
else:
self.own_markers.insert(0, marker_.mark)

def _extend_own_markers(self, marks: Sequence[Mark]) -> None:
"""Append freshly unpacked markers to ``own_markers``.

A hook for nodes that need to remember where in ``own_markers`` those
markers landed (e.g. :class:`~pytest.Class`, for its MRO run).
"""
self.own_markers.extend(marks)

def iter_markers(self, name: str | None = None) -> Iterator[Mark]:
"""Iterate over all markers of the node.

The markers are returned from closest to farthest.

:param name: If given, filter the results by the name attribute.
:returns: An iterator of the markers of the node.
"""
Expand All @@ -362,14 +399,43 @@ def iter_markers_with_node(
) -> 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.


:param name: If given, filter the results by the name attribute.
:returns: An iterator of (node, mark) tuples.
"""
for node in self.iter_parents():
for mark in node.own_markers:
yield from self._iter_markers_with_node(name)

def _iter_markers_with_node(
self,
name: str | None = None,
*,
order: MarkerOrder = MarkerOrder.CLOSEST_FIRST,
) -> Iterator[tuple[Node, Mark]]:
"""Iterate over all markers of the node, as (node, mark) tuples,
in the given :class:`MarkerOrder`."""
nodes: Iterable[Node] = self.iter_parents()
if order is MarkerOrder.FARTHEST_FIRST:
nodes = reversed(list(nodes))
for node in nodes:
marks = (
node._iter_own_markers_closest_first()
if order is MarkerOrder.CLOSEST_FIRST
else node.own_markers
)
for mark in marks:
if name is None or getattr(mark, "name", None) == name:
yield node, mark

def _iter_own_markers_closest_first(self) -> Iterable[Mark]:
"""Yield own markers in closest-first order.

For most nodes this is just own_markers in order.
Overridden by nodes whose own_markers contain markers from
multiple levels (e.g. Class nodes with MRO-inherited markers).
"""
return self.own_markers

@overload
def get_closest_marker(self, name: str) -> Mark | None: ...

Expand Down
84 changes: 82 additions & 2 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from _pytest.compat import is_async_function
from _pytest.compat import NOTSET
from _pytest.compat import NotSetType
from _pytest.compat import override
from _pytest.compat import safe_getattr
from _pytest.compat import safe_isclass
from _pytest.config import Config
Expand All @@ -66,6 +67,7 @@
from _pytest.main import Session
from _pytest.mark import ParameterSet
from _pytest.mark.structures import _HiddenParam
from _pytest.mark.structures import get_mro_mark_groups
from _pytest.mark.structures import get_unpacked_marks
from _pytest.mark.structures import HIDDEN_PARAM
from _pytest.mark.structures import Mark
Expand Down Expand Up @@ -140,7 +142,11 @@ def pytest_addoption(parser: Parser) -> None:


def pytest_generate_tests(metafunc: Metafunc) -> None:
for marker in metafunc.definition.iter_markers(name="parametrize"):
# An inherited class-level parametrize keeps composing base-first, so the
# generated IDs stay stable (#14329).
for _node, marker in metafunc.definition._iter_markers_with_node(
name="parametrize", order=nodes.MarkerOrder.STORED
):
metafunc.parametrize(*marker.args, **marker.kwargs, _param_mark=marker)


Expand Down Expand Up @@ -311,7 +317,7 @@ def obj(self):
# XXX evil hack
# used to avoid Function marker duplication
if self._ALLOW_MARKERS:
self.own_markers.extend(get_unpacked_marks(self.obj))
self._extend_own_markers(get_unpacked_marks(self.obj))
# This assumes that `obj` is called before there is a chance
# to add custom keys to `self.keywords`, so no fear of overriding.
self.keywords.update((mark.name, mark) for mark in self.own_markers)
Expand Down Expand Up @@ -766,11 +772,85 @@ def _get_first_non_fixture_func(obj: object, names: Iterable[str]) -> object | N
class Class(PyCollector):
"""Collector for test methods (and nested classes) in a Python class."""

#: Where the MRO-inherited markers were last seen in ``own_markers``, the
#: run as stored (base-first), and the same run closest-first. ``None``
#: until ``obj`` is resolved and the markers are unpacked.
_mro_marks: tuple[int, tuple[Mark, ...], tuple[Mark, ...]] | None = None

@classmethod
def from_parent(cls, parent, *, name, obj=None, **kw) -> Self: # type: ignore[override]
"""The public constructor."""
return super().from_parent(name=name, parent=parent, **kw)

@override
def _extend_own_markers(self, marks: Sequence[Mark]) -> None:
"""Record where the MRO run lands, so it can be reordered by a slice."""
groups = get_mro_mark_groups(self._obj)
self._mro_marks = (
len(self.own_markers),
tuple(marks),
tuple(mark for group in groups for mark in group),
)
super()._extend_own_markers(marks)

def _find_mro_run(self) -> int | None:
"""Return where the recorded MRO run starts in ``own_markers`` now.

``own_markers`` is a public list: ``add_marker(append=False)`` or a
plugin's ``insert()``/``remove()`` shift or break the run. The run is
matched by identity as a whole, so a ``Mark`` object that also appears
outside the run (one passed to ``add_marker`` as well) cannot be
mistaken for part of it. Returns ``None`` when the run is gone.
"""
assert self._mro_marks is not None
start, stored, _ = self._mro_marks
own_markers = self.own_markers

def run_at(index: int) -> bool:
window = own_markers[index : index + len(stored)]
return len(window) == len(stored) and all(
a is b for a, b in zip(window, stored, strict=True)
)

if run_at(start):
return start
for index in range(len(own_markers) - len(stored) + 1):
if run_at(index):
self._mro_marks = (index, *self._mro_marks[1:])
return index
return None

@override
def _iter_own_markers_closest_first(self) -> Iterator[Mark]:
"""Yield own markers closest-first.

``own_markers`` holds the MRO-inherited markers as one contiguous run
in base-first order (the order :func:`get_unpacked_marks` produced them
in), possibly surrounded by markers added dynamically via
:meth:`~_pytest.nodes.Node.add_marker`. Yield ``own_markers`` as stored
-- so dynamic markers keep their ``append`` position, just like on a
plain node -- but with that run replaced by its closest-first
(child before parent) counterpart.

If the run was broken up, ``own_markers`` is yielded as stored.
"""
own_markers = self.own_markers
if self._mro_marks is None or not self._mro_marks[1]:
# ``obj`` was not resolved yet, so own_markers cannot contain MRO
# markers - and resolving it here would be a nasty side effect.
yield from own_markers
return

start = self._find_mro_run()
if start is None:
yield from own_markers
return

closest_first = self._mro_marks[2]
yield from own_markers[:start]
yield from closest_first
yield from own_markers[start + len(closest_first) :]

def newinstance(self):
return self.obj()

Expand Down
6 changes: 5 additions & 1 deletion src/_pytest/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from _pytest.config import parse_warning_filter
from _pytest.main import Session
from _pytest.nodes import Item
from _pytest.nodes import MarkerOrder
from _pytest.terminal import TerminalReporter
from _pytest.tracemalloc import tracemalloc_message
import pytest
Expand All @@ -34,7 +35,10 @@ def catch_warnings_for_item(
# apply filters from "filterwarnings" marks
nodeid = "" if item is None else item.nodeid
if item is not None:
for mark in item.iter_markers(name="filterwarnings"):
# The filter applied last wins, so apply the closest one last.
for _node, mark in item._iter_markers_with_node(
name="filterwarnings", order=MarkerOrder.FARTHEST_FIRST
):
for arg in mark.args:
warnings.filterwarnings(*parse_warning_filter(arg, escape=False))

Expand Down
Loading
Loading