diff options
author | Tim Brown <stimut@gmail.com> | 2020-10-28 21:35:24 +1000 |
---|---|---|
committer | Tim Brown <stimut@gmail.com> | 2020-10-28 21:35:24 +1000 |
commit | 8ae8883c4bb6c1a3e9925a38e530cbe980898d5c (patch) | |
tree | c4779f809181301f28ee066cba0622a87171cd98 /qutebrowser/keyinput | |
parent | 37d7a195a9e47c8d6e3b603d90ad2130a7ce683e (diff) | |
download | qutebrowser-8ae8883c4bb6c1a3e9925a38e530cbe980898d5c.tar.gz qutebrowser-8ae8883c4bb6c1a3e9925a38e530cbe980898d5c.zip |
mypy: use from-import style for typing
Update files in `keyinput`, `mainwindow`, and `misc`.
See #5396
Diffstat (limited to 'qutebrowser/keyinput')
-rw-r--r-- | qutebrowser/keyinput/basekeyparser.py | 18 | ||||
-rw-r--r-- | qutebrowser/keyinput/eventfilter.py | 4 | ||||
-rw-r--r-- | qutebrowser/keyinput/keyutils.py | 38 | ||||
-rw-r--r-- | qutebrowser/keyinput/macros.py | 14 | ||||
-rw-r--r-- | qutebrowser/keyinput/modeman.py | 3 | ||||
-rw-r--r-- | qutebrowser/keyinput/modeparsers.py | 6 |
6 files changed, 38 insertions, 45 deletions
diff --git a/qutebrowser/keyinput/basekeyparser.py b/qutebrowser/keyinput/basekeyparser.py index dea85aede..23b77cba1 100644 --- a/qutebrowser/keyinput/basekeyparser.py +++ b/qutebrowser/keyinput/basekeyparser.py @@ -21,7 +21,7 @@ import string import types -import typing +from typing import Mapping, MutableMapping, Optional, Sequence import attr from PyQt5.QtCore import pyqtSignal, QObject, Qt @@ -37,9 +37,9 @@ class MatchResult: """The result of matching a keybinding.""" - match_type = attr.ib() # type: QKeySequence.SequenceMatch - command = attr.ib() # type: typing.Optional[str] - sequence = attr.ib() # type: keyutils.KeySequence + match_type: QKeySequence.SequenceMatch = attr.ib() + command: Optional[str] = attr.ib() + sequence: keyutils.KeySequence = attr.ib() def __attrs_post_init__(self) -> None: if self.match_type == QKeySequence.ExactMatch: @@ -75,9 +75,8 @@ class BindingTrie: __slots__ = 'children', 'command' def __init__(self) -> None: - self.children = { - } # type: typing.MutableMapping[keyutils.KeyInfo, BindingTrie] - self.command = None # type: typing.Optional[str] + self.children: MutableMapping[keyutils.KeyInfo, BindingTrie] = {} + self.command: Optional[str] = None def __setitem__(self, sequence: keyutils.KeySequence, command: str) -> None: @@ -99,8 +98,7 @@ class BindingTrie: def __str__(self) -> str: return '\n'.join(self.string_lines(blank=True)) - def string_lines(self, indent: int = 0, - blank: bool = False) -> typing.Sequence[str]: + def string_lines(self, indent: int = 0, blank: bool = False) -> Sequence[str]: """Get a list of strings for a pretty-printed version of this trie.""" lines = [] if self.command is not None: @@ -114,7 +112,7 @@ class BindingTrie: return lines - def update(self, mapping: typing.Mapping) -> None: + def update(self, mapping: Mapping) -> None: """Add data from the given mapping to the trie.""" for key in mapping: self[key] = mapping[key] diff --git a/qutebrowser/keyinput/eventfilter.py b/qutebrowser/keyinput/eventfilter.py index 6ef0dd201..d77c8702d 100644 --- a/qutebrowser/keyinput/eventfilter.py +++ b/qutebrowser/keyinput/eventfilter.py @@ -19,7 +19,7 @@ """Global Qt event filter which dispatches key events.""" -import typing +from typing import cast from PyQt5.QtCore import pyqtSlot, QObject, QEvent from PyQt5.QtGui import QKeyEvent, QWindow @@ -102,7 +102,7 @@ class EventFilter(QObject): handler = self._handlers[typ] try: - return handler(typing.cast(QKeyEvent, event)) + return handler(cast(QKeyEvent, event)) except: # If there is an exception in here and we leave the eventfilter # activated, we'll get an infinite loop and a stack overflow. diff --git a/qutebrowser/keyinput/keyutils.py b/qutebrowser/keyinput/keyutils.py index b95f4a55d..aa5457c6d 100644 --- a/qutebrowser/keyinput/keyutils.py +++ b/qutebrowser/keyinput/keyutils.py @@ -32,7 +32,7 @@ handle what we actually think we do. """ import itertools -import typing +from typing import cast, overload, Iterable, Iterator, List, Mapping, Optional, Union import attr from PyQt5.QtCore import Qt, QEvent @@ -53,10 +53,10 @@ _MODIFIER_MAP = { _NIL_KEY = Qt.Key(0) -_ModifierType = typing.Union[Qt.KeyboardModifier, Qt.KeyboardModifiers] +_ModifierType = Union[Qt.KeyboardModifier, Qt.KeyboardModifiers] -def _build_special_names() -> typing.Mapping[Qt.Key, str]: +def _build_special_names() -> Mapping[Qt.Key, str]: """Build _SPECIAL_NAMES dict from the special_names_str mapping below. The reason we don't do this directly is that certain Qt versions don't have @@ -231,8 +231,7 @@ def _remap_unicode(key: Qt.Key, text: str) -> Qt.Key: return key -def _check_valid_utf8(s: str, - data: typing.Union[Qt.Key, _ModifierType]) -> None: +def _check_valid_utf8(s: str, data: Union[Qt.Key, _ModifierType]) -> None: """Make sure the given string is valid UTF-8. Makes sure there are no chars where Qt did fall back to weird UTF-16 @@ -288,7 +287,7 @@ class KeyParseError(Exception): """Raised by _parse_single_key/parse_keystring on parse errors.""" - def __init__(self, keystr: typing.Optional[str], error: str) -> None: + def __init__(self, keystr: Optional[str], error: str) -> None: if keystr is None: msg = "Could not parse keystring: {}".format(error) else: @@ -296,7 +295,7 @@ class KeyParseError(Exception): super().__init__(msg) -def _parse_keystring(keystr: str) -> typing.Iterator[str]: +def _parse_keystring(keystr: str) -> Iterator[str]: key = '' special = False for c in keystr: @@ -363,8 +362,8 @@ class KeyInfo: modifiers: A Qt::KeyboardModifiers enum value. """ - key = attr.ib() # type: Qt.Key - modifiers = attr.ib() # type: _ModifierType + key: Qt.Key = attr.ib() + modifiers: _ModifierType = attr.ib() @classmethod def from_event(cls, e: QKeyEvent) -> 'KeyInfo': @@ -377,7 +376,7 @@ class KeyInfo: modifiers = e.modifiers() _assert_plain_key(key) _assert_plain_modifier(modifiers) - return cls(key, typing.cast(Qt.KeyboardModifier, modifiers)) + return cls(key, cast(Qt.KeyboardModifier, modifiers)) def __hash__(self) -> int: """Convert KeyInfo to int before hashing. @@ -473,7 +472,7 @@ class KeySequence: _MAX_LEN = 4 def __init__(self, *keys: int) -> None: - self._sequences = [] # type: typing.List[QKeySequence] + self._sequences: List[QKeySequence] = [] for sub in utils.chunk(keys, self._MAX_LEN): args = [self._convert_key(key) for key in sub] sequence = QKeySequence(*args) @@ -493,7 +492,7 @@ class KeySequence: parts.append(str(info)) return ''.join(parts) - def __iter__(self) -> typing.Iterator[KeyInfo]: + def __iter__(self) -> Iterator[KeyInfo]: """Iterate over KeyInfo objects.""" for key_and_modifiers in self._iter_keys(): key = Qt.Key(int(key_and_modifiers) & ~Qt.KeyboardModifierMask) @@ -535,17 +534,15 @@ class KeySequence: def __bool__(self) -> bool: return bool(self._sequences) - @typing.overload + @overload def __getitem__(self, item: int) -> KeyInfo: ... - @typing.overload + @overload def __getitem__(self, item: slice) -> 'KeySequence': ... - def __getitem__( - self, item: typing.Union[int, slice] - ) -> typing.Union[KeyInfo, 'KeySequence']: + def __getitem__(self, item: Union[int, slice]) -> Union[KeyInfo, 'KeySequence']: if isinstance(item, slice): keys = list(self._iter_keys()) return self.__class__(*keys[item]) @@ -553,9 +550,8 @@ class KeySequence: infos = list(self) return infos[item] - def _iter_keys(self) -> typing.Iterator[int]: - sequences = typing.cast(typing.Iterable[typing.Iterable[int]], - self._sequences) + def _iter_keys(self) -> Iterator[int]: + sequences = cast(Iterable[Iterable[int]], self._sequences) return itertools.chain.from_iterable(sequences) def _validate(self, keystr: str = None) -> None: @@ -664,7 +660,7 @@ class KeySequence: def with_mappings( self, - mappings: typing.Mapping['KeySequence', 'KeySequence'] + mappings: Mapping['KeySequence', 'KeySequence'] ) -> 'KeySequence': """Get a new KeySequence with the given mappings applied.""" keys = [] diff --git a/qutebrowser/keyinput/macros.py b/qutebrowser/keyinput/macros.py index 6e48e5a3f..ee8883070 100644 --- a/qutebrowser/keyinput/macros.py +++ b/qutebrowser/keyinput/macros.py @@ -20,7 +20,7 @@ """Keyboard macro system.""" -import typing +from typing import cast, Dict, List, Optional, Tuple from qutebrowser.commands import runners from qutebrowser.api import cmdutils @@ -28,9 +28,9 @@ from qutebrowser.keyinput import modeman from qutebrowser.utils import message, objreg, usertypes -_CommandType = typing.Tuple[str, int] # command, type +_CommandType = Tuple[str, int] # command, type -macro_recorder = typing.cast('MacroRecorder', None) +macro_recorder = cast('MacroRecorder', None) class MacroRecorder: @@ -47,10 +47,10 @@ class MacroRecorder: """ def __init__(self) -> None: - self._macros = {} # type: typing.Dict[str, typing.List[_CommandType]] - self._recording_macro = None # type: typing.Optional[str] - self._macro_count = {} # type: typing.Dict[int, int] - self._last_register = None # type: typing.Optional[str] + self._macros: Dict[str, List[_CommandType]] = {} + self._recording_macro: Optional[str] = None + self._macro_count: Dict[int, int] = {} + self._last_register: Optional[str] = None @cmdutils.register(instance='macro-recorder', name='record-macro') @cmdutils.argument('win_id', value=cmdutils.Value.win_id) diff --git a/qutebrowser/keyinput/modeman.py b/qutebrowser/keyinput/modeman.py index e49aa891d..27e4be34e 100644 --- a/qutebrowser/keyinput/modeman.py +++ b/qutebrowser/keyinput/modeman.py @@ -19,7 +19,6 @@ """Mode manager (per window) which handles the current keyboard mode.""" -import typing import functools from typing import Mapping, Callable, MutableMapping, Union, Set, cast @@ -265,7 +264,7 @@ class ModeManager(QObject): self.mode = usertypes.KeyMode.normal self._releaseevents_to_pass = set() # type: Set[KeyEvent] # Set after __init__ - self.hintmanager = typing.cast(hints.HintManager, None) + self.hintmanager = cast(hints.HintManager, None) def __repr__(self) -> str: return utils.get_repr(self, mode=self.mode) diff --git a/qutebrowser/keyinput/modeparsers.py b/qutebrowser/keyinput/modeparsers.py index dd8ee37f6..48f3594a5 100644 --- a/qutebrowser/keyinput/modeparsers.py +++ b/qutebrowser/keyinput/modeparsers.py @@ -23,9 +23,9 @@ Module attributes: STARTCHARS: Possible chars for starting a commandline input. """ -import typing import traceback import enum +from typing import TYPE_CHECKING, Sequence from PyQt5.QtCore import pyqtSlot, Qt, QObject from PyQt5.QtGui import QKeySequence, QKeyEvent @@ -35,7 +35,7 @@ from qutebrowser.commands import cmdexc from qutebrowser.config import config from qutebrowser.keyinput import basekeyparser, keyutils, macros from qutebrowser.utils import usertypes, log, message, objreg, utils -if typing.TYPE_CHECKING: +if TYPE_CHECKING: from qutebrowser.commands import runners @@ -232,7 +232,7 @@ class HintKeyParser(basekeyparser.BaseKeyParser): return match - def update_bindings(self, strings: typing.Sequence[str], + def update_bindings(self, strings: Sequence[str], preserve_filter: bool = False) -> None: """Update bindings when the hint strings changed. |