Accept MarkDecorator as get_closest_marker default - #14833
Open
ace2016 wants to merge 1 commit into
Open
Conversation
`Mark` is private and warns when instantiated directly, which left no supported way to build the `default` argument. Accept a `MarkDecorator` such as `pytest.mark.foo(1)` and unwrap it to its `Mark`. Closes pytest-dev#13354 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Author
|
@RonnyPfannschmidt could you please take a look when you have the time? thank you |
RonnyPfannschmidt
approved these changes
Aug 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #13354
Problem
Node.get_closest_marker(name, default)documents and type-hints itsdefaultas aMark:But
Mark.__init__is private-gated — it ends incheck_ispytest(_ispytest), which raises aPytestDeprecationWarning("A private pytest class or function was used.") when a user constructs aMarkdirectly. That leaves the public parameter with no supported way to produce a value: the API asks for aMark, and the obvious way to build one warns.Approach
So
get_closest_markernow also accepts aMarkDecoratorand unwraps it:pytest.mark.foo(1)builds itsMarkinternally with_ispytest=True, so nothing warns.Changes
src/_pytest/nodes.pydefaulttoMark | MarkDecorator, unwrap to.mark, document ittesting/test_mark.pytest_mark_closestchangelog/13354.improvement.rstAUTHORSdoc/en/reference/reference.rstneeded no edit — it autodocs the method, so the updated docstring flows through.Testing
testing/test_mark.pyandtesting/test_nodes.pypass (124 passed, 1 xfailed).nodes.pychange makes it fail, restoring it makes it pass.pre-commiton all changed files passes, includingmypy,ruff,ruff format, andcodespell.The new test uses the registered dummy marker
foofrompyproject.tomlrather than an ad-hoc name, because the decorator is constructed in the outer test process where pytest's own suite runs with--strict-markers.##Checklist:
-> The method docstring is updated; reference docs autodoc it.
closes #XYZWto the PR description and/or commits.Co-authored-bycommit trailers.changelogdirectory.AUTHORSin alphabetical order.