Merge pull request #4440 from nicoddemus/config-warning-stacklevel

Adjust stacklevel of "config" warnings
This commit is contained in:
Ronny Pfannschmidt 2018-11-22 21:02:52 +01:00 committed by GitHub
commit abaf496fe8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 15 additions and 7 deletions

View File

@ -0,0 +1 @@
Adjust the stack level of some internal pytest warnings.

View File

@ -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):

View File

@ -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):

View File

@ -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
)

View File

@ -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:

View File

@ -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):

View File

@ -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)
)

View File

@ -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(