Skip to content

Commit aa765cf

Browse files
committed
Adjust stacklevel of "config" warnings
Related to pytest-dev#4439
1 parent 45c33c4 commit aa765cf

File tree

7 files changed

+14
-7
lines changed

7 files changed

+14
-7
lines changed

src/_pytest/assertion/rewrite.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ def _warn_already_imported(self, name):
270270
_issue_config_warning(
271271
PytestWarning("Module already imported so cannot be rewritten: %s" % name),
272272
self.config,
273+
stacklevel=5,
273274
)
274275

275276
def load_module(self, name):

src/_pytest/cacheprovider.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ def warn(self, fmt, **args):
5656
from _pytest.warning_types import PytestWarning
5757

5858
_issue_config_warning(
59-
PytestWarning(fmt.format(**args) if args else fmt), self._config
59+
PytestWarning(fmt.format(**args) if args else fmt),
60+
self._config,
61+
stacklevel=3,
6062
)
6163

6264
def makedir(self, name):

src/_pytest/config/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def _prepareconfig(args=None, plugins=None):
191191
if warning:
192192
from _pytest.warnings import _issue_config_warning
193193

194-
_issue_config_warning(warning, config=config)
194+
_issue_config_warning(warning, config=config, stacklevel=4)
195195
return pluginmanager.hook.pytest_cmdline_parse(
196196
pluginmanager=pluginmanager, args=args
197197
)

src/_pytest/config/findpaths.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def getcfg(args, config=None):
4242
CFG_PYTEST_SECTION.format(filename=inibasename)
4343
),
4444
config=config,
45+
stacklevel=2,
4546
)
4647
return base, p, iniconfig["pytest"]
4748
if (
@@ -116,7 +117,9 @@ def determine_setup(inifile, args, rootdir_cmd_arg=None, config=None):
116117
# TODO: [pytest] section in *.cfg files is deprecated. Need refactoring once
117118
# the deprecation expires.
118119
_issue_config_warning(
119-
CFG_PYTEST_SECTION.format(filename=str(inifile)), config
120+
CFG_PYTEST_SECTION.format(filename=str(inifile)),
121+
config,
122+
stacklevel=2,
120123
)
121124
break
122125
except KeyError:

src/_pytest/resultlog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def pytest_configure(config):
3636
from _pytest.deprecated import RESULT_LOG
3737
from _pytest.warnings import _issue_config_warning
3838

39-
_issue_config_warning(RESULT_LOG, config)
39+
_issue_config_warning(RESULT_LOG, config, stacklevel=2)
4040

4141

4242
def pytest_unconfigure(config):

src/_pytest/warnings.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,19 @@ def pytest_terminal_summary(terminalreporter):
160160
yield
161161

162162

163-
def _issue_config_warning(warning, config):
163+
def _issue_config_warning(warning, config, stacklevel):
164164
"""
165165
This function should be used instead of calling ``warnings.warn`` directly when we are in the "configure" stage:
166166
at this point the actual options might not have been set, so we manually trigger the pytest_warning_captured
167167
hook so we can display this warnings in the terminal. This is a hack until we can sort out #2891.
168168
169169
:param warning: the warning instance.
170170
:param config:
171+
:param stacklevel: stacklevel forwarded to warnings.warn
171172
"""
172173
with warnings.catch_warnings(record=True) as records:
173174
warnings.simplefilter("always", type(warning))
174-
warnings.warn(warning, stacklevel=2)
175+
warnings.warn(warning, stacklevel=stacklevel)
175176
config.hook.pytest_warning_captured.call_historic(
176177
kwargs=dict(warning_message=records[0], when="config", item=None)
177178
)

testing/test_warnings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def test_warning_captured_hook(testdir):
310310
"""
311311
from _pytest.warnings import _issue_config_warning
312312
def pytest_configure(config):
313-
_issue_config_warning(UserWarning("config warning"), config)
313+
_issue_config_warning(UserWarning("config warning"), config, stacklevel=2)
314314
"""
315315
)
316316
testdir.makepyfile(

0 commit comments

Comments
 (0)