From 47be6f3aeb886cf10f85158af18b13906fc68704 Mon Sep 17 00:00:00 2001 From: toofar Date: Sun, 26 Feb 2023 13:39:35 +1300 Subject: lint: add stacklevel kwarg to warnings flake8-bugbear B028 No explicit stacklevel keyword argument found. The warn method from the warnings module uses a stacklevel of 1 by default. This will only show a stack trace for the line on which the warn method is called. It is therefore recommended to use a stacklevel of 2 or greater to provide more information to the user. Semgrep helped: semgrep --lang=py -e 'warnings.warn($ARG)' --replacement 'warnings.warn($ARG, stacklevel=2)' $FILES -a semgrep --lang=py -e 'warnings.warn($ARG1, $ARG2)' --replacement 'warnings.warn($ARG1, $ARG2, stacklevel=2)' $FILES -a Although it did lose the f-string on one of them. --- tests/end2end/fixtures/testprocess.py | 2 +- tests/test_conftest.py | 2 +- tests/unit/config/test_configtypes.py | 2 +- tests/unit/utils/test_log.py | 14 +++++++------- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/end2end/fixtures/testprocess.py b/tests/end2end/fixtures/testprocess.py index 96e700390..9276a4820 100644 --- a/tests/end2end/fixtures/testprocess.py +++ b/tests/end2end/fixtures/testprocess.py @@ -319,7 +319,7 @@ class Process(QObject): if not ok: cmdline = ' '.join([self.proc.program()] + self.proc.arguments()) warnings.warn(f"Test process {cmdline} with PID {self.proc.processId()} " - "failed to terminate!") + "failed to terminate!", stacklevel=2) self.proc.kill() self.proc.waitForFinished() diff --git a/tests/test_conftest.py b/tests/test_conftest.py index fedc6b43f..2019bfd0b 100644 --- a/tests/test_conftest.py +++ b/tests/test_conftest.py @@ -41,7 +41,7 @@ def test_no_qapp(request): def test_fail_on_warnings(): with pytest.raises(PendingDeprecationWarning): - warnings.warn('test', PendingDeprecationWarning) + warnings.warn('test', PendingDeprecationWarning, stacklevel=2) @pytest.mark.xfail(reason="https://github.com/qutebrowser/qutebrowser/issues/1070", diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index 99b8a5de0..22ac3e9df 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -1521,7 +1521,7 @@ class TestRegex: """ regex = klass() m = mocker.patch('qutebrowser.config.configtypes.re') - m.compile.side_effect = lambda *args: warnings.warn(warning) + m.compile.side_effect = lambda *args: warnings.warn(warning, stacklevel=2) m.error = re.error with pytest.raises(type(warning)): regex.to_py('foo') diff --git a/tests/unit/utils/test_log.py b/tests/unit/utils/test_log.py index bbc6b02db..0a03ea7ee 100644 --- a/tests/unit/utils/test_log.py +++ b/tests/unit/utils/test_log.py @@ -275,7 +275,7 @@ class TestInitLog: log.init_log(args) with caplog.at_level(logging.WARNING): - warnings.warn("test warning", PendingDeprecationWarning) + warnings.warn("test warning", PendingDeprecationWarning, stacklevel=2) expected = "PendingDeprecationWarning: test warning" assert expected in caplog.records[0].message @@ -285,7 +285,7 @@ class TestInitLog: log.init_log(args) with pytest.raises(PendingDeprecationWarning): - warnings.warn("test warning", PendingDeprecationWarning) + warnings.warn("test warning", PendingDeprecationWarning, stacklevel=2) @pytest.mark.parametrize('cli, conf, expected', [ (None, 'info', logging.INFO), @@ -386,9 +386,9 @@ def test_stub(caplog, suffix, expected): def test_py_warning_filter(caplog): logging.captureWarnings(True) with log.py_warning_filter(category=UserWarning): - warnings.warn("hidden", UserWarning) + warnings.warn("hidden", UserWarning, stacklevel=2) with caplog.at_level(logging.WARNING): - warnings.warn("not hidden", UserWarning) + warnings.warn("not hidden", UserWarning, stacklevel=2) assert len(caplog.records) == 1 msg = caplog.messages[0].splitlines()[0] assert msg.endswith("UserWarning: not hidden") @@ -396,17 +396,17 @@ def test_py_warning_filter(caplog): def test_py_warning_filter_error(caplog): warnings.simplefilter('ignore') - warnings.warn("hidden", UserWarning) + warnings.warn("hidden", UserWarning, stacklevel=2) with log.py_warning_filter('error'): with pytest.raises(UserWarning): - warnings.warn("error", UserWarning) + warnings.warn("error", UserWarning, stacklevel=2) def test_warning_still_errors(): # Mainly a sanity check after the tests messing with warnings above. with pytest.raises(UserWarning): - warnings.warn("error", UserWarning) + warnings.warn("error", UserWarning, stacklevel=2) class TestQtMessageHandler: -- cgit v1.2.3-54-g00ecf