diff options
author | toofar <toofar@spalge.com> | 2022-09-11 15:05:37 +1200 |
---|---|---|
committer | toofar <toofar@spalge.com> | 2022-09-11 17:22:51 +1200 |
commit | 377bdf736f96605b998ca6f2ea2b062e190653b0 (patch) | |
tree | 5516a75ca8ff29c11507f8a2b571f5b99446c5d4 | |
parent | 3d93e1537836e91580eae32714f440626a2a1e15 (diff) | |
download | qutebrowser-377bdf736f96605b998ca6f2ea2b062e190653b0.tar.gz qutebrowser-377bdf736f96605b998ca6f2ea2b062e190653b0.zip |
mypy: fix qenum debug type hints?
1. stop pretending to propagate None to the qt/python debug methods
2. handle simplewrapper in extract_enum_val
I think this stuff will need a little more cleaning up when we get to
sorting out type checking on PyQt6.
The whole key debug module seems to be a bit fuzzy about when it's going
to be passing around a simplewrapper, int and enum.
I believe we are using sip.simplerwapper as a common parent type of all
the Qt Flag/Enum values. I think it gets better on PyQt6, don't remember
though. It might just change to be sip.wrapper instead.
-rw-r--r-- | qutebrowser/utils/debug.py | 6 | ||||
-rw-r--r-- | qutebrowser/utils/qtutils.py | 6 |
2 files changed, 8 insertions, 4 deletions
diff --git a/qutebrowser/utils/debug.py b/qutebrowser/utils/debug.py index ff6c3b9c2..47cbebb35 100644 --- a/qutebrowser/utils/debug.py +++ b/qutebrowser/utils/debug.py @@ -104,7 +104,7 @@ _EnumValueType = Union[sip.simplewrapper, int] def _qenum_key_python( value: _EnumValueType, - klass: Type[_EnumValueType] = None, + klass: Type[_EnumValueType], ) -> Optional[str]: """New-style PyQt6: Try getting value from Python enum.""" if isinstance(value, enum.Enum) and value.name: @@ -113,6 +113,7 @@ def _qenum_key_python( # We got an int with klass passed: Try asking Python enum for member if issubclass(klass, enum.Enum): try: + assert isinstance(value, int) name = klass(value).name if name is not None and name != str(value): return name @@ -125,7 +126,7 @@ def _qenum_key_python( def _qenum_key_qt( base: Type[_EnumValueType], value: _EnumValueType, - klass: Type[_EnumValueType] = None, + klass: Type[_EnumValueType], ) -> Optional[str]: # On PyQt5, or PyQt6 with int passed: Try to ask Qt's introspection. # However, not every Qt enum value has a staticMetaObject @@ -168,6 +169,7 @@ def qenum_key( klass = value.__class__ if klass == int: raise TypeError("Can't guess enum class of an int!") + assert klass is not None name = _qenum_key_python(value=value, klass=klass) if name is not None: diff --git a/qutebrowser/utils/qtutils.py b/qutebrowser/utils/qtutils.py index 430f15c75..699dc86d9 100644 --- a/qutebrowser/utils/qtutils.py +++ b/qutebrowser/utils/qtutils.py @@ -36,7 +36,7 @@ import contextlib from typing import (Any, AnyStr, TYPE_CHECKING, BinaryIO, IO, Iterator, Optional, Union, Tuple, cast) -from qutebrowser.qt import machinery +from qutebrowser.qt import machinery, sip from qutebrowser.qt.core import (qVersion, QEventLoop, QDataStream, QByteArray, QIODevice, QFileDevice, QSaveFile, QT_VERSION_STR, PYQT_VERSION_STR, QObject, QUrl, QLibraryInfo) @@ -600,7 +600,7 @@ def library_path(which: LibraryPath) -> pathlib.Path: return pathlib.Path(ret) -def extract_enum_val(val: Union[int, enum.Enum]) -> int: +def extract_enum_val(val: Union[sip.simplewrapper, int, enum.Enum]) -> int: """Extract an int value from a Qt enum value. For Qt 5, enum values are basically Python integers. @@ -609,4 +609,6 @@ def extract_enum_val(val: Union[int, enum.Enum]) -> int: """ if isinstance(val, enum.Enum): return val.value + elif isinstance(val, sip.simplewrapper): + return int(val) # type: ignore[call-overload] return int(val) |