From 2ac0566babd1d710507ed6d5b3a16ad17dae98d8 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sat, 2 Jan 2021 14:14:43 +0100 Subject: pylint: Make config check more intelligent --- qutebrowser/browser/downloads.py | 2 -- qutebrowser/browser/navigate.py | 2 -- qutebrowser/completion/models/urlmodel.py | 2 -- scripts/dev/pylint_checkers/qute_pylint/config.py | 25 ++++++++++++++++------- tests/unit/config/test_configfiles.py | 4 +--- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/qutebrowser/browser/downloads.py b/qutebrowser/browser/downloads.py index b179013fc..67df2e70d 100644 --- a/qutebrowser/browser/downloads.py +++ b/qutebrowser/browser/downloads.py @@ -548,12 +548,10 @@ class AbstractDownloadItem(QObject): position: The color type requested, can be 'fg' or 'bg'. """ assert position in ["fg", "bg"] - # pylint: disable=bad-config-option start = getattr(config.val.colors.downloads.start, position) stop = getattr(config.val.colors.downloads.stop, position) system = getattr(config.val.colors.downloads.system, position) error = getattr(config.val.colors.downloads.error, position) - # pylint: enable=bad-config-option if self.error_msg is not None: assert not self.successful return error diff --git a/qutebrowser/browser/navigate.py b/qutebrowser/browser/navigate.py index bace6fa6a..f41c968c0 100644 --- a/qutebrowser/browser/navigate.py +++ b/qutebrowser/browser/navigate.py @@ -174,9 +174,7 @@ def _find_prevnext(prev, elems): if not elems: return None - # pylint: disable=bad-config-option for regex in getattr(config.val.hints, option): - # pylint: enable=bad-config-option log.hints.vdebug( # type: ignore[attr-defined] "== Checking regex '{}'.".format(regex.pattern)) for e in elems: diff --git a/qutebrowser/completion/models/urlmodel.py b/qutebrowser/completion/models/urlmodel.py index ba0857d4c..1de336015 100644 --- a/qutebrowser/completion/models/urlmodel.py +++ b/qutebrowser/completion/models/urlmodel.py @@ -67,14 +67,12 @@ def url(*, info): """ model = completionmodel.CompletionModel(column_widths=(40, 50, 10)) - # pylint: disable=bad-config-option quickmarks = [(url, name) for (name, url) in objreg.get('quickmark-manager').marks.items()] bookmarks = objreg.get('bookmark-manager').marks.items() searchengines = [(k, v) for k, v in sorted(config.val.url.searchengines.items()) if k != 'DEFAULT'] - # pylint: enable=bad-config-option categories = config.val.completion.open_categories models: Dict[str, QAbstractItemModel] = {} diff --git a/scripts/dev/pylint_checkers/qute_pylint/config.py b/scripts/dev/pylint_checkers/qute_pylint/config.py index 097b8ada3..7eedeb215 100644 --- a/scripts/dev/pylint_checkers/qute_pylint/config.py +++ b/scripts/dev/pylint_checkers/qute_pylint/config.py @@ -49,13 +49,24 @@ class ConfigChecker(checkers.BaseChecker): @utils.check_messages('bad-config-option') def visit_attribute(self, node): """Visit a getattr node.""" - # At the end of a config.val.foo.bar chain - if not isinstance(node.parent, astroid.Attribute): - # FIXME:conf do some proper check for this... - node_str = node.as_string() - prefix = 'config.val.' - if node_str.startswith(prefix): - self._check_config(node, node_str[len(prefix):]) + # We're only interested in the end of a config.val.foo.bar chain + if isinstance(node.parent, astroid.Attribute): + return + + if isinstance(node.parent, astroid.Call): + # Skip dynamic getattr() + func = node.parent.func + if isinstance(func, astroid.Name) and func.name == 'getattr': + return + # Handle .items() / .values() + if node.attrname in ['items', 'values']: + node = node.expr + + # FIXME:conf do some proper check for this... + node_str = node.as_string() + prefix = 'config.val.' + if node_str.startswith(prefix): + self._check_config(node, node_str[len(prefix):]) def _check_config(self, node, name): """Check that we're accessing proper config options.""" diff --git a/tests/unit/config/test_configfiles.py b/tests/unit/config/test_configfiles.py index 11808e2c2..a21fa4e71 100644 --- a/tests/unit/config/test_configfiles.py +++ b/tests/unit/config/test_configfiles.py @@ -808,9 +808,7 @@ class TestConfigPy: ]) def test_get(self, confpy, set_first, get_line): """Test whether getting options works correctly.""" - # pylint: disable=bad-config-option - config.val.colors.hints.fg = 'green' - # pylint: enable=bad-config-option + config.val.colors.hints.fg = 'green' # pylint: disable=bad-config-option if set_first: confpy.write('c.colors.hints.fg = "red"', 'assert {} == "red"'.format(get_line)) -- cgit v1.2.3-54-g00ecf