Skip to content

Commit

Permalink
ENH: Improve warning stacklevel (pytest-dev#12014)
Browse files Browse the repository at this point in the history
* ENH: Improve warning stacklevel

* TST: Add test

* TST: Ping

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* MAINT: Changelog

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and flying-sheep committed Apr 9, 2024
1 parent 47cf5df commit cd12a7d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
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

0 comments on commit cd12a7d

Please sign in to comment.