Skip to content

Commit

Permalink
mypy: adapt new type hints to pyqt5
Browse files Browse the repository at this point in the history
Ah! I'm having flashbacks to last year.

1. pyqt5 has plural enum names = define conditional type variable
2. pyqt5 doesn't wrap all the nullable things in Optional = sneakily
   make the existing overload function signature conditional.
   There might be some other was to solve this, not sure. I know we have
   qtutils.add_optional() but in this case it's complaining that the
   signature doesn't match the parent. Narrowing or widening the type of
   the returned object doesn't affect the function signature. Possibly
   we could define our own type variable MaybeOptional...
  • Loading branch information
toofar committed Aug 12, 2024
1 parent aceef82 commit 0050fc9
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions qutebrowser/completion/models/completionmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@

"""A model that proxies access to one or more completion categories."""

from typing import MutableSequence, overload, Optional, Any
from typing import MutableSequence, overload, Optional, Any, cast

from qutebrowser.qt import machinery
from qutebrowser.qt.core import Qt, QModelIndex, QAbstractItemModel, QObject

from qutebrowser.utils import log, qtutils, utils
from qutebrowser.api import cmdutils
from qutebrowser.completion.models import BaseCategory


if machinery.IS_QT5:
_FlagType = Qt.ItemFlags
else:
_FlagType = Qt.ItemFlag


class CompletionModel(QAbstractItemModel):

"""A model that proxies access to one or more completion categories.
Expand Down Expand Up @@ -75,22 +82,22 @@ def data(self, index: QModelIndex, role: int = Qt.ItemDataRole.DisplayRole) -> A
idx = cat.index(index.row(), index.column())
return cat.data(idx)

def flags(self, index: QModelIndex) -> Qt.ItemFlag:
def flags(self, index: QModelIndex) -> _FlagType:
"""Return the item flags for index.
Override QAbstractItemModel::flags.
Return: The item flags, or Qt.ItemFlag.NoItemFlags on error.
"""
if not index.isValid():
return Qt.ItemFlag.NoItemFlags
return cast(_FlagType, Qt.ItemFlag.NoItemFlags)
if index.parent().isValid():
# item
return (Qt.ItemFlag.ItemIsEnabled | Qt.ItemFlag.ItemIsSelectable |
Qt.ItemFlag.ItemNeverHasChildren)
else:
# category
return Qt.ItemFlag.NoItemFlags
return cast(_FlagType, Qt.ItemFlag.NoItemFlags)

def index(self, row: int, col: int, parent: QModelIndex = QModelIndex()) -> QModelIndex:
"""Get an index into the model.
Expand All @@ -113,9 +120,15 @@ def index(self, row: int, col: int, parent: QModelIndex = QModelIndex()) -> QMod
def parent(self, index: QModelIndex) -> QModelIndex:
...

@overload
def parent(self) -> Optional[QObject]:
...
if machinery.IS_QT5:
@overload
def parent(self) -> QObject:
...

else:
@overload
def parent(self) -> Optional[QObject]:
...

def parent(self, index=None):
"""Get an index to the parent of the given index.
Expand Down

0 comments on commit 0050fc9

Please sign in to comment.