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:
parent
59e9a58cc9
commit
1640f2e454
|
@ -0,0 +1 @@
|
|||
Fix the ``stacklevel`` used when warning about marks used on fixtures.
|
|
@ -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.
|
||||
|
|
|
@ -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():
|
||||
|
|
Loading…
Reference in New Issue