Skip to content
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

ENH: Improve warning stacklevel #12014

Merged
merged 5 commits into from
Feb 23, 2024
Merged
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 changelog/12014.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the ``stacklevel`` used when warning about marks used on fixtures.
6 changes: 3 additions & 3 deletions src/_pytest/mark/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def __call__(self, *args: object, **kwargs: object):
func = args[0]
is_class = inspect.isclass(func)
if len(args) == 1 and (istestfunc(func) or is_class):
store_mark(func, self.mark)
store_mark(func, self.mark, stacklevel=3)
return func
return self.with_args(*args, **kwargs)

Expand Down Expand Up @@ -410,7 +410,7 @@ def normalize_mark_list(
yield mark_obj


def store_mark(obj, mark: Mark) -> None:
def store_mark(obj, mark: Mark, *, stacklevel: int = 2) -> None:
"""Store a Mark on an object.

This is used to implement the Mark declarations/decorators correctly.
Expand All @@ -420,7 +420,7 @@ def store_mark(obj, mark: Mark) -> None:
from ..fixtures import getfixturemarker

if getfixturemarker(obj) is not None:
warnings.warn(MARKED_FIXTURE, stacklevel=2)
warnings.warn(MARKED_FIXTURE, stacklevel=stacklevel)

# Always reassign name to avoid updating pytestmark in a reference that
# was only borrowed.
Expand Down
2 changes: 2 additions & 0 deletions testing/deprecated_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ def foo():
raise NotImplementedError()

assert len(record) == 2 # one for each mark decorator
# should point to this file
assert all(rec.filename == __file__ for rec in record)


def test_fixture_disallowed_between_marks():
Expand Down
Loading