harbor run -t wsauret/instance_qutebrowser__qutebrowser-3e21c8214a998cb1058defd15aabb24617a76402-v5fc38aaf22415ab0b70567368332beee7955b367harbor download wsauret/instance_qutebrowser__qutebrowser-3e21c8214a998cb1058defd15aabb24617a76402-v5fc38aaf22415ab0b70567368332beee7955b367A code repository is available in the /app directory. Your task is to implement the changes necessary to satisfy the following Pull Request (PR) description:
<pr_description>
KeySequence Type Safety and Qt6 Compatibility Issues
Description
The current KeySequence implementation in qutebrowser uses raw integer values to represent key combinations, which creates several problems:
Type Safety Issues: Using integers lacks type safety and makes the code harder to understand and maintain
Qt6 Compatibility Problems: Qt6 introduces QKeyCombination as a more robust way to handle key combinations, but the current integer-based approach doesn't leverage this properly
Code Maintainability: Key handling logic is scattered and not well-encapsulated, making it difficult to extend or modify
Current problems include:
KeySequence constructor accepts raw integers instead of structured key information
Import handling for QKeyCombination uses a bare pass statement, which can cause issues when the type is referenced elsewhere
No clear separation between Qt5 and Qt6 key handling approaches
Key manipulation operations work directly with integers instead of structured data
How to reproduce
Use qutebrowser with Qt6 where QKeyCombination is available
Create complex key sequences with multiple modifiers
Observe type safety issues and potential runtime errors when QKeyCombination type is referenced but not properly imported
Notice maintainability issues when trying to modify key handling logic due to integer-based approach
The internal representation of a key sequence must be refactored from raw integers to a structured, type-safe value that encapsulates both a key and its associated keyboard modifiers (e.g., Ctrl, Alt, Shift, Meta, Keypad/Num).
The KeyInfo value must be constructible as KeyInfo(key, modifiers) where key is a Qt.Key and modifiers is a Qt.KeyboardModifier. The modifiers argument must be optional and default to Qt.KeyboardModifier.NoModifier, so that KeyInfo(Qt.Key.Key_A) is equivalent to KeyInfo(Qt.Key.Key_A, Qt.KeyboardModifier.NoModifier). Two KeyInfo values must compare equal when, and only when, both their key and their modifiers are equal.
The KeySequence constructor must accept zero or more KeyInfo objects (KeySequence(*keys: KeyInfo)) rather than raw integers. Constructing KeySequence with five KeyInfo objects must split them into the expected internal sequence structure (e.g. for five keys, two sub-sequences of lengths 4 and 1). Constructing a KeySequence from a KeyInfo whose key is unknown/invalid (Qt.Key.Key_unknown, -1, or 0) must raise keyutils.KeyParseError.
Iterating over a KeySequence must yield KeyInfo objects, one per key, each carrying that key's modifiers (or Qt.KeyboardModifier.NoModifier when none). The list of yielded KeyInfo objects must equal the list of KeyInfo objects the sequence was built from.
repr() of a KeySequence must render as <qutebrowser.keyinput.keyutils.KeySequence keys='...'>, where the keys='...' portion is the sequence's string form; e.g. a sequence of KeyInfo(Qt.Key.Key_A, Qt.KeyboardModifier.ControlModifier) and KeyInfo(Qt.Key.Key_B, Qt.KeyboardModifier.ShiftModifier) must repr as <qutebrowser.keyinput.keyutils.KeySequence keys='<Ctrl+a>B'>.
All key-sequence operations must continue to function with this KeyInfo-based representation and must be comparable via KeySequence/KeyInfo equality:
Parsing from strings: KeySequence.parse(...) must produce a sequence equal to the equivalent KeySequence built from KeyInfo objects. For example parse('<Ctrl-Alt-y>') must equal KeySequence(KeyInfo(Qt.Key.Key_Y, Qt.KeyboardModifier.ControlModifier | Qt.KeyboardModifier.AltModifier)); parse('X') must equal KeySequence(KeyInfo(Qt.Key.Key_X, Qt.KeyboardModifier.ShiftModifier)); parse('<Num-x>') must equal KeySequence(KeyInfo(Qt.Key.Key_X, Qt.KeyboardModifier.KeypadModifier)); multi-key strings such as parse('xyz') must equal KeySequence(KeyInfo(Qt.Key.Key_X), KeyInfo(Qt.Key.Key_Y), KeyInfo(Qt.Key.Key_Z)). Malformed strings such as '<alt+<>' and '<alt+>>' must raise keyutils.KeyParseError.
Stripping modifiers: KeySequence.strip_modifiers() must return a KeySequence in which non-significant modifiers are removed while significant ones are preserved. For example, a sequence of KeyInfo(Qt.Key.Key_0), KeyInfo(Qt.Key.Key_1, Qt.KeyboardModifier.KeypadModifier), and KeyInfo(Qt.Key.Key_A, Qt.KeyboardModifier.ControlModifier) must, after strip_modifiers(), equal a sequence of KeyInfo(Qt.Key.Key_0), KeyInfo(Qt.Key.Key_1), and KeyInfo(Qt.Key.Key_A, Qt.KeyboardModifier.ControlModifier).
Applying mappings and appending key events must likewise continue to work and produce KeyInfo-based sequences that compare correctly.
Key sequences must behave identically across Qt versions, correctly abstracting the difference between Qt5's integer keys and Qt6's QKeyCombination.
The system must provide an unambiguous conversion between raw Qt key events, string formats (like <Ctrl+A>), and the structured KeyInfo representation.
KeyInfo must not type-check or convert its key argument: any plain int (including values outside every Qt.Key member, such as surrogate/emoji code points 0x1f468, 0x200d, ord('Œ'), and -1) must be accepted and stored unchanged. Key/modifier combination and string conversion must use plain integer arithmetic so such values pass through KeySequence(...) and str(...) intact.
KeySequence must store its internal chunks in an attribute named _sequences, a List[QKeySequence]; five keys produce two entries holding 4 and 1 keys respectively.
When QKeyCombination cannot be imported (Qt5), the module must still import successfully: the name QKeyCombination must be defined in that case so that annotations such as Union[int, QKeyCombination] can be evaluated at import time.
Path: qutebrowser/keyinput/keyutils.py
Name: KeyInfo
Type: class / value
Input: key: Qt.Key, modifiers: Qt.KeyboardModifier = Qt.KeyboardModifier.NoModifier
Output: a KeyInfo value encapsulating a key and its modifiers; equality holds iff both key and modifiers match.
Description: Structured, type-safe representation of a single key plus its modifiers, used to construct and iterate KeySequence.
Path: qutebrowser/keyinput/keyutils.py
Name: KeySequence.__init__
Type: method
Input: self, *keys: KeyInfo
Output: None
Description: Builds a key sequence from zero or more KeyInfo objects. Raises keyutils.KeyParseError if any provided KeyInfo has an unknown/invalid key.
Path: qutebrowser/keyinput/keyutils.py
Name: KeyInfo.to_qt
Type: method
Input: self
Output: Union[int, QKeyCombination]
Description: Returns a value suitable for QKeySequence, either an integer for Qt5 or QKeyCombination for Qt6.
Path: qutebrowser/keyinput/keyutils.py
Name: KeyInfo.with_stripped_modifiers
Type: method
Input: self, modifiers: Qt.KeyboardModifier
Output: KeyInfo
Description: Returns a new KeyInfo instance with the specified modifiers removed. </pr_description>
Constraints: