diff options
author | toofar <toofar@spalge.com> | 2022-09-11 14:04:37 +1200 |
---|---|---|
committer | toofar <toofar@spalge.com> | 2022-09-11 17:22:50 +1200 |
commit | 2eacf0456a7b5e7c7cb519e59c191dc257752214 (patch) | |
tree | f7bb136a25f19bd4974222a68449d526a92e6792 | |
parent | ed13c3c734d265fc92b9b4ea118505aed0aa4414 (diff) | |
download | qutebrowser-2eacf0456a7b5e7c7cb519e59c191dc257752214.tar.gz qutebrowser-2eacf0456a7b5e7c7cb519e59c191dc257752214.zip |
mypy: defer to machinery for conditional: QKeyCombination
That wasn't so hard, just put everything behind compile time constants,
even the type definition.
-rw-r--r-- | qutebrowser/keyinput/keyutils.py | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/qutebrowser/keyinput/keyutils.py b/qutebrowser/keyinput/keyutils.py index 3af103607..92ef717af 100644 --- a/qutebrowser/keyinput/keyutils.py +++ b/qutebrowser/keyinput/keyutils.py @@ -35,12 +35,13 @@ import itertools import dataclasses from typing import Iterator, List, Mapping, Optional, Union, overload, cast +from qutebrowser.qt import machinery from qutebrowser.qt.core import Qt, QEvent from qutebrowser.qt.gui import QKeySequence, QKeyEvent -try: +if machinery.IS_QT6: from qutebrowser.qt.core import QKeyCombination -except ImportError: - QKeyCombination = None # Qt 6 only +else: + QKeyCombination = None from qutebrowser.utils import utils, qtutils, debug @@ -72,6 +73,10 @@ except ValueError: _NIL_KEY = 0 # type: ignore[assignment] _ModifierType = Qt.KeyboardModifier +if machinery.IS_QT6: + _KeyInfoType = QKeyCombination +else: + _KeyInfoType = int _SPECIAL_NAMES = { @@ -380,9 +385,10 @@ class KeyInfo: return cls(key, modifiers) @classmethod - def from_qt(cls, combination: Union[int, QKeyCombination]) -> 'KeyInfo': + def from_qt(cls, combination: _KeyInfoType) -> 'KeyInfo': """Construct a KeyInfo from a Qt5-style int or Qt6-style QKeyCombination.""" - if isinstance(combination, int): + if machinery.IS_QT5: + assert isinstance(combination, int) key = Qt.Key( int(combination) & ~Qt.KeyboardModifier.KeyboardModifierMask) modifiers = Qt.KeyboardModifier( @@ -461,22 +467,21 @@ class KeyInfo: """Get a QKeyEvent from this KeyInfo.""" return QKeyEvent(typ, self.key, self.modifiers, self.text()) - def to_qt(self) -> Union[int, QKeyCombination]: + def to_qt(self) -> _KeyInfoType: """Get something suitable for a QKeySequence.""" - if QKeyCombination is None: - # Qt 5 + if machinery.IS_QT5: return int(self.key) | int(self.modifiers) + else: + try: + # FIXME:qt6 We might want to consider only supporting KeyInfo to be + # instanciated with a real Qt.Key, not with ints. See __post_init__. + key = Qt.Key(self.key) + except ValueError as e: + # WORKAROUND for + # https://www.riverbankcomputing.com/pipermail/pyqt/2022-April/044607.html + raise InvalidKeyError(e) - try: - # FIXME:qt6 We might want to consider only supporting KeyInfo to be - # instanciated with a real Qt.Key, not with ints. See __post_init__. - key = Qt.Key(self.key) - except ValueError as e: - # WORKAROUND for - # https://www.riverbankcomputing.com/pipermail/pyqt/2022-April/044607.html - raise InvalidKeyError(e) - - return QKeyCombination(self.modifiers, key) + return QKeyCombination(self.modifiers, key) def with_stripped_modifiers(self, modifiers: Qt.KeyboardModifier) -> "KeyInfo": mods = self.modifiers & ~modifiers |