Repository navigation
fix: iter_markers/get_closest_marker returns correct closest MRO marker (#14329) #14630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9f6f9d3
24e9175
3216ce5
4ea119b
acee034
d93ab4b
70c5da4
60357de
6c2c37b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -537,6 +537,7 @@ Wil Cooley | |
| Will Riley | ||
| William Lee | ||
| Wim Glenn | ||
| Wintreist | ||
| Wouter van Ackooy | ||
| Xixi Zhao | ||
| Xuan Luong | ||
|
|
||
| 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")``. |
| 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'. |
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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. | ||
|
|
||
|
|
@@ -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. | ||
| """ | ||
|
|
@@ -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. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems reasonable to add
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nevermind: 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".
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( |
||
|
|
||
| :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: ... | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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, becausefilterwarningsis applied in turn the last one applied wins. Example:TestChild::test_subclass_ignore_beats_base_errorpasses on main and fails on this branch.TestFuncUnderClass::test_function_ignore_beats_class_errorfails in both, and is separate and older, but still worthwhile reviewing/fixing while we are at this.There was a problem hiding this comment.
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
filterwarningsin 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 intest_filterwarnings_closest_mark_wins.