mypy: Add types for config.websettings

This commit is contained in:
Florian Bruhin 2018-12-05 17:48:56 +01:00
parent 7bddb7867b
commit b9cb98a5ef
2 changed files with 18 additions and 51 deletions

View File

@ -62,43 +62,7 @@ disallow_incomplete_defs = True
disallow_untyped_defs = True disallow_untyped_defs = True
disallow_incomplete_defs = True disallow_incomplete_defs = True
[mypy-qutebrowser.config.config] [mypy-qutebrowser.config.*]
disallow_untyped_defs = True
disallow_incomplete_defs = True
[mypy-qutebrowser.config.configcache]
disallow_untyped_defs = True
disallow_incomplete_defs = True
[mypy-qutebrowser.config.configcommands]
disallow_untyped_defs = True
disallow_incomplete_defs = True
[mypy-qutebrowser.config.configdata]
disallow_untyped_defs = True
disallow_incomplete_defs = True
[mypy-qutebrowser.config.configdiff]
disallow_untyped_defs = True
disallow_incomplete_defs = True
[mypy-qutebrowser.config.configexc]
disallow_untyped_defs = True
disallow_incomplete_defs = True
[mypy-qutebrowser.config.configfiles]
disallow_untyped_defs = True
disallow_incomplete_defs = True
[mypy-qutebrowser.config.configinit]
disallow_untyped_defs = True
disallow_incomplete_defs = True
[mypy-qutebrowser.config.configutils]
disallow_untyped_defs = True
disallow_incomplete_defs = True
[mypy-qutebrowser.config.configtypes]
disallow_untyped_defs = True disallow_untyped_defs = True
disallow_incomplete_defs = True disallow_incomplete_defs = True

View File

@ -19,8 +19,10 @@
"""Bridge from QWeb(Engine)Settings to our own settings.""" """Bridge from QWeb(Engine)Settings to our own settings."""
import typing # pylint: disable=unused-import import typing
import argparse
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QFont from PyQt5.QtGui import QFont
from qutebrowser.config import config, configutils from qutebrowser.config import config, configutils
@ -34,7 +36,8 @@ class AttributeInfo:
"""Info about a settings attribute.""" """Info about a settings attribute."""
def __init__(self, *attributes, converter=None): def __init__(self, *attributes: typing.Any,
converter: typing.Callable = None) -> None:
self.attributes = attributes self.attributes = attributes
if converter is None: if converter is None:
self.converter = lambda val: val self.converter = lambda val: val
@ -51,10 +54,10 @@ class AbstractSettings:
_FONT_FAMILIES = {} # type: typing.Dict[str, typing.Any] _FONT_FAMILIES = {} # type: typing.Dict[str, typing.Any]
_FONT_TO_QFONT = {} # type: typing.Dict[typing.Any, QFont.StyleHint] _FONT_TO_QFONT = {} # type: typing.Dict[typing.Any, QFont.StyleHint]
def __init__(self, settings): def __init__(self, settings: typing.Any) -> None:
self._settings = settings self._settings = settings
def set_attribute(self, name, value): def set_attribute(self, name: str, value: typing.Any) -> bool:
"""Set the given QWebSettings/QWebEngineSettings attribute. """Set the given QWebSettings/QWebEngineSettings attribute.
If the value is configutils.UNSET, the value is reset instead. If the value is configutils.UNSET, the value is reset instead.
@ -75,7 +78,7 @@ class AbstractSettings:
return old_value != new_value return old_value != new_value
def test_attribute(self, name): def test_attribute(self, name: str) -> bool:
"""Get the value for the given attribute. """Get the value for the given attribute.
If the setting resolves to a list of attributes, only the first If the setting resolves to a list of attributes, only the first
@ -84,7 +87,7 @@ class AbstractSettings:
info = self._ATTRIBUTES[name] info = self._ATTRIBUTES[name]
return self._settings.testAttribute(info.attributes[0]) return self._settings.testAttribute(info.attributes[0])
def set_font_size(self, name, value): def set_font_size(self, name: str, value: int) -> bool:
"""Set the given QWebSettings/QWebEngineSettings font size. """Set the given QWebSettings/QWebEngineSettings font size.
Return: Return:
@ -96,7 +99,7 @@ class AbstractSettings:
self._settings.setFontSize(family, value) self._settings.setFontSize(family, value)
return old_value != value return old_value != value
def set_font_family(self, name, value): def set_font_family(self, name: str, value: typing.Optional[str]) -> bool:
"""Set the given QWebSettings/QWebEngineSettings font family. """Set the given QWebSettings/QWebEngineSettings font family.
With None (the default), QFont is used to get the default font for the With None (the default), QFont is used to get the default font for the
@ -117,7 +120,7 @@ class AbstractSettings:
return value != old_value return value != old_value
def set_default_text_encoding(self, encoding): def set_default_text_encoding(self, encoding: str) -> bool:
"""Set the default text encoding to use. """Set the default text encoding to use.
Return: Return:
@ -128,7 +131,7 @@ class AbstractSettings:
self._settings.setDefaultTextEncoding(encoding) self._settings.setDefaultTextEncoding(encoding)
return old_value != encoding return old_value != encoding
def _update_setting(self, setting, value): def _update_setting(self, setting: str, value: typing.Any) -> bool:
"""Update the given setting/value. """Update the given setting/value.
Unknown settings are ignored. Unknown settings are ignored.
@ -146,12 +149,12 @@ class AbstractSettings:
return self.set_default_text_encoding(value) return self.set_default_text_encoding(value)
return False return False
def update_setting(self, setting): def update_setting(self, setting: str) -> None:
"""Update the given setting.""" """Update the given setting."""
value = config.instance.get(setting) value = config.instance.get(setting)
self._update_setting(setting, value) self._update_setting(setting, value)
def update_for_url(self, url): def update_for_url(self, url: QUrl) -> typing.Set[str]:
"""Update settings customized for the given tab. """Update settings customized for the given tab.
Return: Return:
@ -173,14 +176,14 @@ class AbstractSettings:
return changed_settings return changed_settings
def init_settings(self): def init_settings(self) -> None:
"""Set all supported settings correctly.""" """Set all supported settings correctly."""
for setting in (list(self._ATTRIBUTES) + list(self._FONT_SIZES) + for setting in (list(self._ATTRIBUTES) + list(self._FONT_SIZES) +
list(self._FONT_FAMILIES)): list(self._FONT_FAMILIES)):
self.update_setting(setting) self.update_setting(setting)
def init(args): def init(args: argparse.Namespace) -> None:
"""Initialize all QWeb(Engine)Settings.""" """Initialize all QWeb(Engine)Settings."""
if objects.backend == usertypes.Backend.QtWebEngine: if objects.backend == usertypes.Backend.QtWebEngine:
from qutebrowser.browser.webengine import webenginesettings from qutebrowser.browser.webengine import webenginesettings
@ -195,7 +198,7 @@ def init(args):
pattern=urlmatch.UrlPattern(pattern)) pattern=urlmatch.UrlPattern(pattern))
def shutdown(): def shutdown() -> None:
"""Shut down QWeb(Engine)Settings.""" """Shut down QWeb(Engine)Settings."""
if objects.backend == usertypes.Backend.QtWebEngine: if objects.backend == usertypes.Backend.QtWebEngine:
from qutebrowser.browser.webengine import webenginesettings from qutebrowser.browser.webengine import webenginesettings