From 72d7e2327b74b4e3928227648cb97706fbf28be9 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 24 May 2024 21:37:15 +0200 Subject: [PATCH] Update for new pylint/astroid releases - Add a couple new "raise utils.Unreachable" to avoid possibly-used-before-assignment issues. - Simplify an "if" for the same reason - Remove an unneeded "return" - Use "NoReturn" to prepare for pylint knowing about it in the future: https://github.com/pylint-dev/pylint/issues/9674 - Add some ignores for used-before-assignment false-positives - Ignore new undefined-variable messages for Qt wrapers - Ignore a new no-member warning for KeySequence: https://github.com/pylint-dev/astroid/issues/2448#issuecomment-2130124755 --- qutebrowser/browser/commands.py | 2 ++ qutebrowser/browser/webengine/webenginetab.py | 6 +++--- qutebrowser/commands/userscripts.py | 1 - qutebrowser/config/configdata.py | 5 +++-- qutebrowser/keyinput/keyutils.py | 2 ++ qutebrowser/qt/webenginewidgets.py | 1 + qutebrowser/qt/widgets.py | 1 + tests/end2end/test_invocations.py | 1 + tests/unit/browser/test_pdfjs.py | 4 +++- tests/unit/completion/test_models.py | 4 +++- tests/unit/config/test_configcommands.py | 2 ++ tests/unit/config/test_configfiles.py | 1 + tests/unit/config/test_configtypes.py | 2 ++ tests/unit/keyinput/test_modeparsers.py | 4 ++++ tests/unit/utils/test_qtutils.py | 2 ++ 15 files changed, 30 insertions(+), 8 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 83a846b85..06298a8ca 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -948,6 +948,8 @@ class CommandDispatcher: "No window specified and couldn't find active window!") assert isinstance(active_win, mainwindow.MainWindow), active_win win_id = active_win.win_id + else: + raise utils.Unreachable(index_parts) if win_id not in objreg.window_registry: raise cmdutils.CommandError( diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index 02d912a50..19defb6d3 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -1477,9 +1477,9 @@ class WebEngineTab(browsertab.AbstractTab): log.network.debug("Asking for credentials") answer = shared.authentication_required( url, authenticator, abort_on=[self.abort_questions]) - if not netrc_success and answer is None: - log.network.debug("Aborting auth") - sip.assign(authenticator, QAuthenticator()) + if answer is None: + log.network.debug("Aborting auth") + sip.assign(authenticator, QAuthenticator()) @pyqtSlot() def _on_load_started(self): diff --git a/qutebrowser/commands/userscripts.py b/qutebrowser/commands/userscripts.py index f4ddd2bf4..01710a63c 100644 --- a/qutebrowser/commands/userscripts.py +++ b/qutebrowser/commands/userscripts.py @@ -330,7 +330,6 @@ class _WindowsUserscriptRunner(_BaseUserscriptRunner): self._filepath = handle.name except OSError as e: message.error("Error while creating tempfile: {}".format(e)) - return class Error(Exception): diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index 9535dd727..2377841ef 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -10,7 +10,7 @@ DATA: A dict of Option objects after init() has been called. """ from typing import (Any, Dict, Iterable, List, Mapping, MutableMapping, Optional, - Sequence, Tuple, Union, cast) + Sequence, Tuple, Union, NoReturn, cast) import functools import dataclasses @@ -57,7 +57,7 @@ class Migrations: deleted: List[str] = dataclasses.field(default_factory=list) -def _raise_invalid_node(name: str, what: str, node: Any) -> None: +def _raise_invalid_node(name: str, what: str, node: Any) -> NoReturn: """Raise an exception for an invalid configdata YAML node. Args: @@ -94,6 +94,7 @@ def _parse_yaml_type( _raise_invalid_node(name, 'type', node) try: + # pylint: disable=possibly-used-before-assignment typ = getattr(configtypes, type_name) except AttributeError: raise AttributeError("Did not find type {} for {}".format( diff --git a/qutebrowser/keyinput/keyutils.py b/qutebrowser/keyinput/keyutils.py index 54b6e88b1..18730c74b 100644 --- a/qutebrowser/keyinput/keyutils.py +++ b/qutebrowser/keyinput/keyutils.py @@ -359,6 +359,8 @@ class KeyInfo: modifier_classes = (Qt.KeyboardModifier, Qt.KeyboardModifiers) elif machinery.IS_QT6: modifier_classes = Qt.KeyboardModifier + else: + raise utils.Unreachable() assert isinstance(self.key, Qt.Key), self.key assert isinstance(self.modifiers, modifier_classes), self.modifiers diff --git a/qutebrowser/qt/webenginewidgets.py b/qutebrowser/qt/webenginewidgets.py index b8833e9c8..88758cf23 100644 --- a/qutebrowser/qt/webenginewidgets.py +++ b/qutebrowser/qt/webenginewidgets.py @@ -27,6 +27,7 @@ else: if machinery.IS_QT5: + # pylint: disable=undefined-variable # moved to WebEngineCore in Qt 6 del QWebEngineSettings del QWebEngineProfile diff --git a/qutebrowser/qt/widgets.py b/qutebrowser/qt/widgets.py index eac8cafbb..f82ec2e3b 100644 --- a/qutebrowser/qt/widgets.py +++ b/qutebrowser/qt/widgets.py @@ -26,4 +26,5 @@ else: raise machinery.UnknownWrapper() if machinery.IS_QT5: + # pylint: disable=undefined-variable del QFileSystemModel # moved to QtGui in Qt 6 diff --git a/tests/end2end/test_invocations.py b/tests/end2end/test_invocations.py index a55efb129..ccf4534dd 100644 --- a/tests/end2end/test_invocations.py +++ b/tests/end2end/test_invocations.py @@ -905,6 +905,7 @@ def test_sandboxing( f"{bpf_text} supports TSYNC": "Yes" if has_seccomp else "No", f"{yama_text} (Broker)": "Yes" if has_yama else "No", + # pylint: disable-next=used-before-assignment f"{yama_text} (Non-broker)": "Yes" if has_yama_non_broker else "No", } diff --git a/tests/unit/browser/test_pdfjs.py b/tests/unit/browser/test_pdfjs.py index cb5c26229..867e8e1de 100644 --- a/tests/unit/browser/test_pdfjs.py +++ b/tests/unit/browser/test_pdfjs.py @@ -9,7 +9,7 @@ import pytest from qutebrowser.qt.core import QUrl from qutebrowser.browser import pdfjs -from qutebrowser.utils import urlmatch +from qutebrowser.utils import urlmatch, utils pytestmark = [pytest.mark.usefixtures('data_tmpdir')] @@ -154,6 +154,8 @@ def test_read_from_system(names, expected_name, tmpdir): expected = (b'text2', str(file2)) elif expected_name is None: expected = (None, None) + else: + raise utils.Unreachable(expected_name) assert pdfjs._read_from_system(str(tmpdir), names) == expected diff --git a/tests/unit/completion/test_models.py b/tests/unit/completion/test_models.py index e0bce8f04..ad73d9eb5 100644 --- a/tests/unit/completion/test_models.py +++ b/tests/unit/completion/test_models.py @@ -29,7 +29,7 @@ from qutebrowser.completion import completer from qutebrowser.completion.models import ( configmodel, listcategory, miscmodels, urlmodel, filepathcategory) from qutebrowser.config import configdata, configtypes -from qutebrowser.utils import usertypes +from qutebrowser.utils import usertypes, utils from qutebrowser.mainwindow import tabbedbrowser @@ -417,6 +417,8 @@ def test_filesystem_completion(qtmodeltester, config_stub, info, base = '~' expected_1 = str(pathlib.Path('~') / 'file1.txt') expected_2 = str(pathlib.Path('~') / 'file2.txt') + else: + raise utils.Unreachable(method) config_stub.val.completion.open_categories = ['filesystem'] model = urlmodel.url(info=info) diff --git a/tests/unit/config/test_configcommands.py b/tests/unit/config/test_configcommands.py index 10d9e2292..f9dca3e74 100644 --- a/tests/unit/config/test_configcommands.py +++ b/tests/unit/config/test_configcommands.py @@ -840,6 +840,8 @@ class TestBind: func = functools.partial(commands.bind, 0) elif command == 'unbind': func = commands.unbind + else: + raise utils.Unreachable(command) with pytest.raises(cmdutils.CommandError, match=expected): func(*args, **kwargs) diff --git a/tests/unit/config/test_configfiles.py b/tests/unit/config/test_configfiles.py index cde0a180b..7a47b6cb0 100644 --- a/tests/unit/config/test_configfiles.py +++ b/tests/unit/config/test_configfiles.py @@ -399,6 +399,7 @@ class TestYaml: yaml._save() if not insert and old_config is None: + data = {} # unused lines = [] else: data = autoconfig.read() diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index bcd257ed7..09fc4fa75 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -1602,6 +1602,8 @@ class TestDict: valtype=configtypes.String(), required_keys=['one', 'two']) message = 'Required keys .*' + else: + raise utils.Unreachable(kind) if ok: expectation = testutils.nop_contextmanager() diff --git a/tests/unit/keyinput/test_modeparsers.py b/tests/unit/keyinput/test_modeparsers.py index 4ba11ed58..6a83d614b 100644 --- a/tests/unit/keyinput/test_modeparsers.py +++ b/tests/unit/keyinput/test_modeparsers.py @@ -115,10 +115,12 @@ class TestHintKeyParser: seq = keyutils.KeySequence.parse(keychain) assert len(seq) == 2 + # pylint: disable-next=no-member match = keyparser.handle(seq[0].to_event()) assert match == QKeySequence.SequenceMatch.PartialMatch assert hintmanager.keystr == prefix + # pylint: disable-next=no-member match = keyparser.handle(seq[1].to_event()) assert match == QKeySequence.SequenceMatch.ExactMatch assert hintmanager.keystr == hint @@ -132,10 +134,12 @@ class TestHintKeyParser: seq = keyutils.KeySequence.parse('ασ') assert len(seq) == 2 + # pylint: disable-next=no-member match = keyparser.handle(seq[0].to_event()) assert match == QKeySequence.SequenceMatch.PartialMatch assert hintmanager.keystr == 'a' + # pylint: disable-next=no-member match = keyparser.handle(seq[1].to_event()) assert match == QKeySequence.SequenceMatch.ExactMatch assert hintmanager.keystr == 'as' diff --git a/tests/unit/utils/test_qtutils.py b/tests/unit/utils/test_qtutils.py index c7af3162c..f1cf1e73e 100644 --- a/tests/unit/utils/test_qtutils.py +++ b/tests/unit/utils/test_qtutils.py @@ -758,8 +758,10 @@ class TestPyQIODevice: # pylint: enable=no-member,useless-suppression else: pytest.skip("Needs os.SEEK_HOLE or os.SEEK_DATA available.") + pyqiodev.open(QIODevice.OpenModeFlag.ReadOnly) with pytest.raises(io.UnsupportedOperation): + # pylint: disable=possibly-used-before-assignment pyqiodev.seek(0, whence) @pytest.mark.flaky