diff options
author | Florian Bruhin <me@the-compiler.org> | 2023-07-19 17:27:55 +0200 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2023-07-19 17:27:55 +0200 |
commit | 5cfab728b29610dd1ab43591b4ba66919621b8a2 (patch) | |
tree | 82329b4d1ca473ae989eb01d1d6eb432671e02d0 /qutebrowser/utils/qtutils.py | |
parent | 92dea988c01e74596cc6ed698e88ac56df392c14 (diff) | |
download | qutebrowser-5cfab728b29610dd1ab43591b4ba66919621b8a2.tar.gz qutebrowser-5cfab728b29610dd1ab43591b4ba66919621b8a2.zip |
Improve handling of Optional with new PyQt stubs
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 |