Skip to content

Commit c397675

Browse files
toofardaerich
authored andcommitted
Adapt chooseFiles() for PyQt6 type hints
The type of the two list arguments of chooseFiles() have changed from `Iterable[str]` to `Iterable[Optional[str]]`. I'm not sure it makes much sense to have individual list elements as None. That seems like a pretty unlikely case. So we could just put an ignore comment somewhere. I've added another copy of the lists, with longer names, to strip any hypothetical Nones out. This allows us to be backwards compatible with the old type hints (and the current Qt5 ones), since that doesn't have the Optional part in the signature. ref: qutebrowser#7990
1 parent 4818162 commit c397675

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

qutebrowser/browser/webengine/webview.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""The main browser widget for QtWebEngine."""
66

77
import mimetypes
8-
from typing import List, Iterable
8+
from typing import List, Iterable, Optional
99

1010
from qutebrowser.qt import machinery
1111
from qutebrowser.qt.core import pyqtSignal, pyqtSlot, QUrl
@@ -316,29 +316,37 @@ def acceptNavigationRequest(self,
316316
def chooseFiles(
317317
self,
318318
mode: QWebEnginePage.FileSelectionMode,
319-
old_files: Iterable[str],
320-
accepted_mimetypes: Iterable[str],
319+
old_files: Iterable[Optional[str]],
320+
accepted_mimetypes: Iterable[Optional[str]],
321321
) -> List[str]:
322322
"""Override chooseFiles to (optionally) invoke custom file uploader."""
323-
extra_suffixes = extra_suffixes_workaround(accepted_mimetypes)
323+
accepted_mimetypes_filtered = [m for m in accepted_mimetypes if m is not None]
324+
old_files_filtered = [f for f in old_files if f is not None]
325+
extra_suffixes = extra_suffixes_workaround(accepted_mimetypes_filtered)
324326
if extra_suffixes:
325327
log.webview.debug(
326328
"adding extra suffixes to filepicker: "
327-
f"before={accepted_mimetypes} "
329+
f"before={accepted_mimetypes_filtered} "
328330
f"added={extra_suffixes}",
329331
)
330-
accepted_mimetypes = list(accepted_mimetypes) + list(extra_suffixes)
332+
accepted_mimetypes_filtered = list(
333+
accepted_mimetypes_filtered
334+
) + list(extra_suffixes)
331335

332336
handler = config.val.fileselect.handler
333337
if handler == "default":
334-
return super().chooseFiles(mode, old_files, accepted_mimetypes)
338+
return super().chooseFiles(
339+
mode, old_files_filtered, accepted_mimetypes_filtered,
340+
)
335341
assert handler == "external", handler
336342
try:
337343
qb_mode = _QB_FILESELECTION_MODES[mode]
338344
except KeyError:
339345
log.webview.warning(
340346
f"Got file selection mode {mode}, but we don't support that!"
341347
)
342-
return super().chooseFiles(mode, old_files, accepted_mimetypes)
348+
return super().chooseFiles(
349+
mode, old_files_filtered, accepted_mimetypes_filtered,
350+
)
343351

344352
return shared.choose_file(qb_mode=qb_mode)

0 commit comments

Comments
 (0)