diff --git a/changelog/4440.trivial.rst b/changelog/4440.trivial.rst new file mode 100644 index 000000000..7187d664f --- /dev/null +++ b/changelog/4440.trivial.rst @@ -0,0 +1 @@ +Adjust the stack level of some internal pytest warnings. diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 7b9aa5006..d1231b774 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -270,6 +270,7 @@ class AssertionRewritingHook(object): _issue_config_warning( PytestWarning("Module already imported so cannot be rewritten: %s" % name), self.config, + stacklevel=5, ) def load_module(self, name): diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index d762d867d..4e51af771 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -56,7 +56,9 @@ class Cache(object): from _pytest.warning_types import PytestWarning _issue_config_warning( - PytestWarning(fmt.format(**args) if args else fmt), self._config + PytestWarning(fmt.format(**args) if args else fmt), + self._config, + stacklevel=3, ) def makedir(self, name): diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 26a0973ca..562b50c38 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -191,7 +191,7 @@ def _prepareconfig(args=None, plugins=None): if warning: from _pytest.warnings import _issue_config_warning - _issue_config_warning(warning, config=config) + _issue_config_warning(warning, config=config, stacklevel=4) return pluginmanager.hook.pytest_cmdline_parse( pluginmanager=pluginmanager, args=args ) diff --git a/src/_pytest/config/findpaths.py b/src/_pytest/config/findpaths.py index 4f371ec7f..a9d674e77 100644 --- a/src/_pytest/config/findpaths.py +++ b/src/_pytest/config/findpaths.py @@ -42,6 +42,7 @@ def getcfg(args, config=None): CFG_PYTEST_SECTION.format(filename=inibasename) ), config=config, + stacklevel=2, ) return base, p, iniconfig["pytest"] if ( @@ -116,7 +117,9 @@ def determine_setup(inifile, args, rootdir_cmd_arg=None, config=None): # TODO: [pytest] section in *.cfg files is deprecated. Need refactoring once # the deprecation expires. _issue_config_warning( - CFG_PYTEST_SECTION.format(filename=str(inifile)), config + CFG_PYTEST_SECTION.format(filename=str(inifile)), + config, + stacklevel=2, ) break except KeyError: diff --git a/src/_pytest/resultlog.py b/src/_pytest/resultlog.py index 3efdbea6e..ab2d0f98b 100644 --- a/src/_pytest/resultlog.py +++ b/src/_pytest/resultlog.py @@ -36,7 +36,7 @@ def pytest_configure(config): from _pytest.deprecated import RESULT_LOG from _pytest.warnings import _issue_config_warning - _issue_config_warning(RESULT_LOG, config) + _issue_config_warning(RESULT_LOG, config, stacklevel=2) def pytest_unconfigure(config): diff --git a/src/_pytest/warnings.py b/src/_pytest/warnings.py index 9de9d01d5..e3e206933 100644 --- a/src/_pytest/warnings.py +++ b/src/_pytest/warnings.py @@ -160,7 +160,7 @@ def pytest_terminal_summary(terminalreporter): yield -def _issue_config_warning(warning, config): +def _issue_config_warning(warning, config, stacklevel): """ This function should be used instead of calling ``warnings.warn`` directly when we are in the "configure" stage: at this point the actual options might not have been set, so we manually trigger the pytest_warning_captured @@ -168,10 +168,11 @@ def _issue_config_warning(warning, config): :param warning: the warning instance. :param config: + :param stacklevel: stacklevel forwarded to warnings.warn """ with warnings.catch_warnings(record=True) as records: warnings.simplefilter("always", type(warning)) - warnings.warn(warning, stacklevel=2) + warnings.warn(warning, stacklevel=stacklevel) config.hook.pytest_warning_captured.call_historic( kwargs=dict(warning_message=records[0], when="config", item=None) ) diff --git a/testing/test_warnings.py b/testing/test_warnings.py index d79e956e3..53d9c71cd 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -310,7 +310,7 @@ def test_warning_captured_hook(testdir): """ from _pytest.warnings import _issue_config_warning def pytest_configure(config): - _issue_config_warning(UserWarning("config warning"), config) + _issue_config_warning(UserWarning("config warning"), config, stacklevel=2) """ ) testdir.makepyfile(