ENH: Improve warning stacklevel (#12014)

* 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>
This commit is contained in:
Eric Larson 2024-02-23 01:11:05 -05:00 committed by GitHub
parent 59e9a58cc9
commit 1640f2e454
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 6 additions and 3 deletions

View File

@ -0,0 +1 @@
Fix the ``stacklevel`` used when warning about marks used on fixtures.

View File

@ -355,7 +355,7 @@ class MarkDecorator:
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)
@ -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.
@ -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.

View File

@ -118,6 +118,8 @@ def test_fixture_disallow_marks_on_fixtures():
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():