diff options
Diffstat (limited to 'qutebrowser/utils/qtutils.py')
-rw-r--r-- | qutebrowser/utils/qtutils.py | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/qutebrowser/utils/qtutils.py b/qutebrowser/utils/qtutils.py index 781e43f08..975503f85 100644 --- a/qutebrowser/utils/qtutils.py +++ b/qutebrowser/utils/qtutils.py @@ -651,20 +651,29 @@ def extract_enum_val(val: Union[sip.simplewrapper, int, enum.Enum]) -> int: return int(val) # type: ignore[call-overload] return val -T = TypeVar("T") +_T = TypeVar("_T") -def is_not_none(obj: Optional[T]) -> "TypeGuard[T]": - """Check if a Qt object is None. - PyQt6 marks things as Optional[...], but PyQt5 doesn't. - By using this function, we can use the same type hints for both. - """ - return obj is not None +if machinery.IS_QT5: + # On Qt 5, add/remove Optional where type annotations don't have it. + # Also we have a special QT_NONE, which (being Any) we can pass to functions + # where PyQt type hints claim that it's not allowed. + def remove_optional(obj: Optional[_T]) -> _T: + return cast(_T, obj) -if machinery.IS_QT5: - def allow_none(obj: Optional[T]) -> T: - return cast(T, obj) + def add_optional(obj: _T) -> Optional[_T]: + return cast(Optional[_T], obj) + + QT_NONE: Any = None else: - def allow_none(obj: Optional[T]) -> Optional[T]: + # On Qt 6, all those things are handled correctly by type annotations, so we + # have a no-op below. + + def remove_optional(obj: Optional[_T]) -> Optional[_T]: + return obj + + def add_optional(obj: Optional[_T]) -> Optional[_T]: return obj + + QT_NONE = None |