diff options
Diffstat (limited to 'qutebrowser/utils')
-rw-r--r-- | qutebrowser/utils/log.py | 2 | ||||
-rw-r--r-- | qutebrowser/utils/qtutils.py | 31 | ||||
-rw-r--r-- | qutebrowser/utils/standarddir.py | 2 | ||||
-rw-r--r-- | qutebrowser/utils/urlutils.py | 6 |
4 files changed, 25 insertions, 16 deletions
diff --git a/qutebrowser/utils/log.py b/qutebrowser/utils/log.py index 5ac150702..174a599ed 100644 --- a/qutebrowser/utils/log.py +++ b/qutebrowser/utils/log.py @@ -255,7 +255,7 @@ def disable_qt_msghandler() -> Iterator[None]: @contextlib.contextmanager -def py_warning_filter( +def py_warning_filter( action: Literal['default', 'error', 'ignore', 'always', 'module', 'once'] = 'ignore', **kwargs: Any, 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 diff --git a/qutebrowser/utils/standarddir.py b/qutebrowser/utils/standarddir.py index 762125c11..a1fa414f7 100644 --- a/qutebrowser/utils/standarddir.py +++ b/qutebrowser/utils/standarddir.py @@ -65,7 +65,7 @@ def _unset_organization() -> Iterator[None]: qapp = QApplication.instance() if qapp is not None: orgname = qapp.organizationName() - qapp.setOrganizationName(qtutils.allow_none(None)) + qapp.setOrganizationName(qtutils.QT_NONE) try: yield finally: diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index b3f03b256..1bb035939 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -179,9 +179,9 @@ def _get_search_url(txt: str) -> QUrl: url = QUrl.fromUserInput(evaluated) else: url = QUrl.fromUserInput(config.val.url.searchengines[engine]) - url.setPath(qtutils.allow_none(None)) - url.setFragment(qtutils.allow_none(None)) - url.setQuery(qtutils.allow_none(None)) + url.setPath(qtutils.QT_NONE) + url.setFragment(qtutils.QT_NONE) + url.setQuery(qtutils.QT_NONE) qtutils.ensure_valid(url) return url |