From ec053f800709dd5f11b9adb781db0022b8b5efe9 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 7 Jul 2016 09:53:14 +0200 Subject: [PATCH] Simplify TabData implementation This uses direct attributes instead of self._data which means we can only override __setattr__, and pylint will better understand what's happening. --- qutebrowser/browser/tab.py | 28 ++++++++++++---------------- scripts/dev/run_vulture.py | 1 + 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/qutebrowser/browser/tab.py b/qutebrowser/browser/tab.py index 30c40a7cd..855af334c 100644 --- a/qutebrowser/browser/tab.py +++ b/qutebrowser/browser/tab.py @@ -74,31 +74,27 @@ class TabData: """A simple namespace with a fixed set of attributes. Attributes: + _initializing: Set when we're currently in __init__. keep_icon: Whether the (e.g. cloned) icon should not be cleared on page load. inspector: The QWebInspector used for this webview. + viewing_source: Set if we're currently showing a source view. """ def __init__(self): - self._data = { - 'keep_icon': False, - 'viewing_source': False, - 'inspector': None, - } - - def __getattr__(self, attr): - try: - return self._data[attr] - except KeyError: - raise AttributeError(attr) + self._initializing = True + self.keep_icon = False + self.viewing_source = False + self.inspector = None + self._initializing = False def __setattr__(self, attr, value): - if attr.startswith('_'): + if (attr == '_initializing' or + getattr(self, '_initializing', False) or + hasattr(self, attr)): return super().__setattr__(attr, value) - if attr not in self._data: - msg = "Can't set unknown attribute {!r}".format(attr) - raise AttributeError(msg) - self._data[attr] = value + msg = "Can't set unknown attribute {!r}".format(attr) + raise AttributeError(msg) class AbstractSearch(QObject): diff --git a/scripts/dev/run_vulture.py b/scripts/dev/run_vulture.py index b894377cf..9eaf08998 100755 --- a/scripts/dev/run_vulture.py +++ b/scripts/dev/run_vulture.py @@ -85,6 +85,7 @@ def whitelist_generator(): yield 'qutebrowser.utils.log.QtWarningFilter.filter' yield 'logging.LogRecord.log_color' yield 'qutebrowser.browser.pdfjs.is_available' + yield 'qutebrowser.browser.tab.TabData._initializing' # vulture doesn't notice the hasattr() and thus thinks netrc_used is unused # in NetworkManager.on_authentication_required yield 'PyQt5.QtNetwork.QNetworkReply.netrc_used'