mypy: cast variable only on Qt5
We only have to cast these variables on Qt5. If we cast them on Qt6 mypy
warns of a redundant cast. If we ignore that warning mypy complains of a
redundant ignore on Qt5.
Add a conditional on `machinery.IS_QT5` and only cast it required.
But also move that conditional out to a utility function to avoid
duplicating code in the function doing the actual logic.
I'm re-using the `_T` generic TypeVar defined above. Not sure if it's
proper to re-use them or not. Doesn't python have a better way of
defining generics now?
I added the `valid-type` ignore because mypy was complaining:
Variable "to_type" is not valid as a type
I'm not sure if there is some way to do it better, but `reveal_type()`
says that the call site is getting the right type back in the end.
This commit is contained in:
parent
291dda69e5
commit
060f4a59d6
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
"""A model that proxies access to one or more completion categories."""
|
||||
|
||||
from typing import overload, Optional, Any, cast
|
||||
from typing import overload, Optional, Any
|
||||
from collections.abc import MutableSequence
|
||||
|
||||
from qutebrowser.qt import machinery
|
||||
|
|
@ -91,14 +91,14 @@ class CompletionModel(QAbstractItemModel):
|
|||
Return: The item flags, or Qt.ItemFlag.NoItemFlags on error.
|
||||
"""
|
||||
if not index.isValid():
|
||||
return cast(_FlagType, Qt.ItemFlag.NoItemFlags)
|
||||
return qtutils.maybe_cast(_FlagType, machinery.IS_QT5, Qt.ItemFlag.NoItemFlags)
|
||||
if index.parent().isValid():
|
||||
# item
|
||||
return (Qt.ItemFlag.ItemIsEnabled | Qt.ItemFlag.ItemIsSelectable |
|
||||
Qt.ItemFlag.ItemNeverHasChildren)
|
||||
else:
|
||||
# category
|
||||
return cast(_FlagType, Qt.ItemFlag.NoItemFlags)
|
||||
return qtutils.maybe_cast(_FlagType, machinery.IS_QT5, Qt.ItemFlag.NoItemFlags)
|
||||
|
||||
def index(self, row: int, col: int, parent: QModelIndex = QModelIndex()) -> QModelIndex:
|
||||
"""Get an index into the model.
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
This entire file is a giant WORKAROUND for https://bugreports.qt.io/browse/QTBUG-114334.
|
||||
"""
|
||||
|
||||
from typing import Union, cast, Optional
|
||||
from typing import Union, Optional
|
||||
import enum
|
||||
import ctypes
|
||||
import ctypes.util
|
||||
|
|
@ -16,7 +16,7 @@ from qutebrowser.qt import sip, machinery
|
|||
from qutebrowser.qt.core import QAbstractNativeEventFilter, QByteArray, qVersion
|
||||
|
||||
from qutebrowser.misc import objects
|
||||
from qutebrowser.utils import log
|
||||
from qutebrowser.utils import log, qtutils
|
||||
|
||||
|
||||
# Needs to be saved to avoid garbage collection
|
||||
|
|
@ -104,8 +104,8 @@ class NativeEventFilter(QAbstractNativeEventFilter):
|
|||
#
|
||||
# Tuple because PyQt uses the second value as the *result out-pointer, which
|
||||
# according to the Qt documentation is only used on Windows.
|
||||
_PASS_EVENT_RET = (False, cast(_PointerRetType, 0))
|
||||
_FILTER_EVENT_RET = (True, cast(_PointerRetType, 0))
|
||||
_PASS_EVENT_RET = (False, qtutils.maybe_cast(_PointerRetType, machinery.IS_QT6, 0))
|
||||
_FILTER_EVENT_RET = (True, qtutils.maybe_cast(_PointerRetType, machinery.IS_QT6, 0))
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
|
|
|||
|
|
@ -749,3 +749,14 @@ else:
|
|||
return obj
|
||||
|
||||
QT_NONE: None = None
|
||||
|
||||
|
||||
def maybe_cast(to_type: type[_T], do_cast: bool, value: Any) -> _T:
|
||||
"""Cast `value` to `to_type` only if `do_cast` is true."""
|
||||
if do_cast:
|
||||
return cast(
|
||||
to_type, # type: ignore[valid-type]
|
||||
value,
|
||||
)
|
||||
|
||||
return value
|
||||
|
|
|
|||
Loading…
Reference in New Issue