Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust stacklevel of "config" warnings #4440

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/4440.trivial.rst
@@ -0,0 +1 @@
Adjust the stack level of some internal pytest warnings.
1 change: 1 addition & 0 deletions src/_pytest/assertion/rewrite.py
Expand Up @@ -270,6 +270,7 @@ def _warn_already_imported(self, name):
_issue_config_warning(
PytestWarning("Module already imported so cannot be rewritten: %s" % name),
self.config,
stacklevel=5,
)

def load_module(self, name):
Expand Down
4 changes: 3 additions & 1 deletion src/_pytest/cacheprovider.py
Expand Up @@ -56,7 +56,9 @@ def warn(self, fmt, **args):
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):
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/config/__init__.py
Expand Up @@ -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
)
Expand Down
5 changes: 4 additions & 1 deletion src/_pytest/config/findpaths.py
Expand Up @@ -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 (
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/resultlog.py
Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions src/_pytest/warnings.py
Expand Up @@ -160,18 +160,19 @@ 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
hook so we can display this warnings in the terminal. This is a hack until we can sort out #2891.

: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)
)
2 changes: 1 addition & 1 deletion testing/test_warnings.py
Expand Up @@ -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(
Expand Down