diff options
author | Tim Brown <stimut@gmail.com> | 2020-10-26 23:08:35 +1000 |
---|---|---|
committer | Tim Brown <stimut@gmail.com> | 2020-10-27 13:49:24 +1000 |
commit | 4279a4da6f16e851497dbc75e342ebb0bf0e3b59 (patch) | |
tree | dd633aaaad3bca80eceaffdd8d598e99bd6bfa7d /qutebrowser/browser/hints.py | |
parent | 37d7a195a9e47c8d6e3b603d90ad2130a7ce683e (diff) | |
download | qutebrowser-4279a4da6f16e851497dbc75e342ebb0bf0e3b59.tar.gz qutebrowser-4279a4da6f16e851497dbc75e342ebb0bf0e3b59.zip |
mypy: use from-import for typing in remaining files in `browser`
The type comments have also been changed to annotations, since Python
3.5 support will be dropped with the next release.
See #5396
Diffstat (limited to 'qutebrowser/browser/hints.py')
-rw-r--r-- | qutebrowser/browser/hints.py | 87 |
1 files changed, 43 insertions, 44 deletions
diff --git a/qutebrowser/browser/hints.py b/qutebrowser/browser/hints.py index b9dc6cbe6..f914f3085 100644 --- a/qutebrowser/browser/hints.py +++ b/qutebrowser/browser/hints.py @@ -20,13 +20,14 @@ """A HintManager to draw hints over links.""" import collections -import typing import functools import os import re import html import enum from string import ascii_lowercase +from typing import (TYPE_CHECKING, Callable, Dict, Iterable, Iterator, List, Mapping, + MutableSequence, Optional, Sequence, Set) import attr from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, Qt, QUrl @@ -38,7 +39,7 @@ from qutebrowser.browser import webelem, history from qutebrowser.commands import userscripts, runners from qutebrowser.api import cmdutils from qutebrowser.utils import usertypes, log, qtutils, message, objreg, utils -if typing.TYPE_CHECKING: +if TYPE_CHECKING: from qutebrowser.browser import browsertab @@ -180,22 +181,22 @@ class HintContext: group: The group of web elements to hint. """ - all_labels = attr.ib(attr.Factory(list)) # type: typing.List[HintLabel] - labels = attr.ib(attr.Factory(dict)) # type: typing.Dict[str, HintLabel] - target = attr.ib(None) # type: Target - baseurl = attr.ib(None) # type: QUrl - to_follow = attr.ib(None) # type: str - rapid = attr.ib(False) # type: bool - first_run = attr.ib(True) # type: bool - add_history = attr.ib(False) # type: bool - filterstr = attr.ib(None) # type: str - args = attr.ib(attr.Factory(list)) # type: typing.List[str] - tab = attr.ib(None) # type: browsertab.AbstractTab - group = attr.ib(None) # type: str - hint_mode = attr.ib(None) # type: str - first = attr.ib(False) # type: bool - - def get_args(self, urlstr: str) -> typing.Sequence[str]: + all_labels: List[HintLabel] = attr.ib(attr.Factory(list)) + labels: Dict[str, HintLabel] = attr.ib(attr.Factory(dict)) + target: Target = attr.ib(None) + baseurl: QUrl = attr.ib(None) + to_follow: str = attr.ib(None) + rapid: bool = attr.ib(False) + first_run: bool = attr.ib(True) + add_history: bool = attr.ib(False) + filterstr: str = attr.ib(None) + args: List[str] = attr.ib(attr.Factory(list)) + tab: 'browsertab.AbstractTab' = attr.ib(None) + group: str = attr.ib(None) + hint_mode: str = attr.ib(None) + first: bool = attr.ib(False) + + def get_args(self, urlstr: str) -> Sequence[str]: """Get the arguments, with {hint-url} replaced by the given URL.""" args = [] for arg in self.args: @@ -352,8 +353,8 @@ class HintActions: commandrunner.run_safely('spawn ' + ' '.join(args)) -_ElemsType = typing.Sequence[webelem.AbstractWebElement] -_HintStringsType = typing.MutableSequence[str] +_ElemsType = Sequence[webelem.AbstractWebElement] +_HintStringsType = MutableSequence[str] class HintManager(QObject): @@ -397,7 +398,7 @@ class HintManager(QObject): """Constructor.""" super().__init__(parent) self._win_id = win_id - self._context = None # type: typing.Optional[HintContext] + self._context: Optional[HintContext] = None self._word_hinter = WordHinter() self._actions = HintActions(win_id) @@ -527,12 +528,10 @@ class HintManager(QObject): Return: A list of shuffled hint strings. """ - buckets = [ - [] for i in range(length) - ] # type: typing.Sequence[_HintStringsType] + buckets: Sequence[_HintStringsType] = [[] for i in range(length)] for i, hint in enumerate(hints): buckets[i % len(buckets)].append(hint) - result = [] # type: _HintStringsType + result: _HintStringsType = [] for bucket in buckets: result += bucket return result @@ -557,7 +556,7 @@ class HintManager(QObject): A hint string. """ base = len(chars) - hintstr = [] # type: typing.MutableSequence[str] + hintstr: MutableSequence[str] = [] remainder = 0 while True: remainder = number % base @@ -785,7 +784,7 @@ class HintManager(QObject): error_cb=lambda err: message.error(str(err)), only_visible=True) - def _get_hint_mode(self, mode: typing.Optional[str]) -> str: + def _get_hint_mode(self, mode: Optional[str]) -> str: """Get the hinting mode to use based on a mode argument.""" if mode is None: return config.val.hints.mode @@ -797,7 +796,7 @@ class HintManager(QObject): raise cmdutils.CommandError("Invalid mode: {}".format(e)) return mode - def current_mode(self) -> typing.Optional[str]: + def current_mode(self) -> Optional[str]: """Return the currently active hinting mode (or None otherwise).""" if self._context is None: return None @@ -808,7 +807,7 @@ class HintManager(QObject): self, keystr: str = "", filterstr: str = "", - visible: typing.Mapping[str, HintLabel] = None + visible: Mapping[str, HintLabel] = None ) -> None: """Handle the auto_follow option.""" assert self._context is not None @@ -870,7 +869,7 @@ class HintManager(QObject): pass self._handle_auto_follow(keystr=keystr) - def filter_hints(self, filterstr: typing.Optional[str]) -> None: + def filter_hints(self, filterstr: Optional[str]) -> None: """Filter displayed hints according to a text. Args: @@ -1041,7 +1040,7 @@ class WordHinter: def __init__(self) -> None: # will be initialized on first use. - self.words = set() # type: typing.Set[str] + self.words: Set[str] = set() self.dictionary = None def ensure_initialized(self) -> None: @@ -1073,10 +1072,10 @@ class WordHinter: def extract_tag_words( self, elem: webelem.AbstractWebElement - ) -> typing.Iterator[str]: + ) -> Iterator[str]: """Extract tag words form the given element.""" - _extractor_type = typing.Callable[[webelem.AbstractWebElement], str] - attr_extractors = { + _extractor_type = Callable[[webelem.AbstractWebElement], str] + attr_extractors: Mapping[str, _extractor_type] = { "alt": lambda elem: elem["alt"], "name": lambda elem: elem["name"], "title": lambda elem: elem["title"], @@ -1084,7 +1083,7 @@ class WordHinter: "src": lambda elem: elem["src"].split('/')[-1], "href": lambda elem: elem["href"].split('/')[-1], "text": str, - } # type: typing.Mapping[str, _extractor_type] + } extractable_attrs = collections.defaultdict(list, { "img": ["alt", "title", "src"], @@ -1100,8 +1099,8 @@ class WordHinter: def tag_words_to_hints( self, - words: typing.Iterable[str] - ) -> typing.Iterator[str]: + words: Iterable[str] + ) -> Iterator[str]: """Take words and transform them to proper hints if possible.""" for candidate in words: if not candidate: @@ -1112,20 +1111,20 @@ class WordHinter: if 4 < match.end() - match.start() < 8: yield candidate[match.start():match.end()].lower() - def any_prefix(self, hint: str, existing: typing.Iterable[str]) -> bool: + def any_prefix(self, hint: str, existing: Iterable[str]) -> bool: return any(hint.startswith(e) or e.startswith(hint) for e in existing) def filter_prefixes( self, - hints: typing.Iterable[str], - existing: typing.Iterable[str] - ) -> typing.Iterator[str]: + hints: Iterable[str], + existing: Iterable[str] + ) -> Iterator[str]: """Filter hints which don't start with the given prefix.""" return (h for h in hints if not self.any_prefix(h, existing)) def new_hint_for(self, elem: webelem.AbstractWebElement, - existing: typing.Iterable[str], - fallback: typing.Iterable[str]) -> typing.Optional[str]: + existing: Iterable[str], + fallback: Iterable[str]) -> Optional[str]: """Return a hint for elem, not conflicting with the existing.""" new = self.tag_words_to_hints(self.extract_tag_words(elem)) new_no_prefixes = self.filter_prefixes(new, existing) @@ -1149,7 +1148,7 @@ class WordHinter: """ self.ensure_initialized() hints = [] - used_hints = set() # type: typing.Set[str] + used_hints: Set[str] = set() words = iter(self.words) for elem in elems: hint = self.new_hint_for(elem, used_hints, words) |