From 7836ecbf27d5af19601d4f0620fd6fd23337e25a Mon Sep 17 00:00:00 2001 From: toofar Date: Mon, 2 Jun 2025 18:42:23 +1200 Subject: [PATCH] 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. --- qutebrowser/completion/models/completionmodel.py | 6 +++--- qutebrowser/misc/nativeeventfilter.py | 8 ++++---- qutebrowser/utils/qtutils.py | 11 +++++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/qutebrowser/completion/models/completionmodel.py b/qutebrowser/completion/models/completionmodel.py index a4eed93d1..c0f6a86ff 100644 --- a/qutebrowser/completion/models/completionmodel.py +++ b/qutebrowser/completion/models/completionmodel.py @@ -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. diff --git a/qutebrowser/misc/nativeeventfilter.py b/qutebrowser/misc/nativeeventfilter.py index 1933dc63f..e93b3c6d1 100644 --- a/qutebrowser/misc/nativeeventfilter.py +++ b/qutebrowser/misc/nativeeventfilter.py @@ -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__() diff --git a/qutebrowser/utils/qtutils.py b/qutebrowser/utils/qtutils.py index a027db74a..d55f9bc2f 100644 --- a/qutebrowser/utils/qtutils.py +++ b/qutebrowser/utils/qtutils.py @@ -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