From 268ad40982466e0985961318e0a6c2b4ae17423f Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 4 Dec 2018 15:52:14 +0100 Subject: [PATCH] Add and enforce types for api.*/components.* --- mypy.ini | 8 +++++ qutebrowser/api/apitypes.py | 3 +- qutebrowser/components/caretcommands.py | 36 +++++++++---------- qutebrowser/components/misccommands.py | 44 +++++++++++++----------- qutebrowser/components/scrollcommands.py | 2 +- qutebrowser/components/zoomcommands.py | 23 +++++++------ 6 files changed, 65 insertions(+), 51 deletions(-) diff --git a/mypy.ini b/mypy.ini index 154a877bf..c61ca1f18 100644 --- a/mypy.ini +++ b/mypy.ini @@ -97,3 +97,11 @@ disallow_incomplete_defs = True [mypy-qutebrowser.config.configutils] disallow_untyped_defs = True disallow_incomplete_defs = True + +[mypy-qutebrowser.api.*] +disallow_untyped_defs = True +disallow_incomplete_defs = True + +[mypy-qutebrowser.components.*] +disallow_untyped_defs = True +disallow_incomplete_defs = True diff --git a/qutebrowser/api/apitypes.py b/qutebrowser/api/apitypes.py index 2ed188724..9fec0a6cb 100644 --- a/qutebrowser/api/apitypes.py +++ b/qutebrowser/api/apitypes.py @@ -21,5 +21,6 @@ # pylint: disable=unused-import from qutebrowser.browser.browsertab import WebTabError, AbstractTab as Tab -from qutebrowser.browser.webelem import Error as WebElemError +from qutebrowser.browser.webelem import (Error as WebElemError, + AbstractWebElement as WebElement) from qutebrowser.utils.usertypes import ClickTarget, JsWorld diff --git a/qutebrowser/components/caretcommands.py b/qutebrowser/components/caretcommands.py index fe04f2483..4bab6b6c6 100644 --- a/qutebrowser/components/caretcommands.py +++ b/qutebrowser/components/caretcommands.py @@ -26,7 +26,7 @@ from qutebrowser.api import cmdutils, apitypes @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('count', value=cmdutils.Value.count) -def move_to_next_line(tab: apitypes.Tab, count: int = 1): +def move_to_next_line(tab: apitypes.Tab, count: int = 1) -> None: """Move the cursor or selection to the next line. Args: @@ -38,7 +38,7 @@ def move_to_next_line(tab: apitypes.Tab, count: int = 1): @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('count', value=cmdutils.Value.count) -def move_to_prev_line(tab: apitypes.Tab, count: int = 1): +def move_to_prev_line(tab: apitypes.Tab, count: int = 1) -> None: """Move the cursor or selection to the prev line. Args: @@ -50,7 +50,7 @@ def move_to_prev_line(tab: apitypes.Tab, count: int = 1): @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('count', value=cmdutils.Value.count) -def move_to_next_char(tab: apitypes.Tab, count: int = 1): +def move_to_next_char(tab: apitypes.Tab, count: int = 1) -> None: """Move the cursor or selection to the next char. Args: @@ -62,7 +62,7 @@ def move_to_next_char(tab: apitypes.Tab, count: int = 1): @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('count', value=cmdutils.Value.count) -def move_to_prev_char(tab: apitypes.Tab, count: int = 1): +def move_to_prev_char(tab: apitypes.Tab, count: int = 1) -> None: """Move the cursor or selection to the previous char. Args: @@ -74,7 +74,7 @@ def move_to_prev_char(tab: apitypes.Tab, count: int = 1): @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('count', value=cmdutils.Value.count) -def move_to_end_of_word(tab: apitypes.Tab, count: int = 1): +def move_to_end_of_word(tab: apitypes.Tab, count: int = 1) -> None: """Move the cursor or selection to the end of the word. Args: @@ -86,7 +86,7 @@ def move_to_end_of_word(tab: apitypes.Tab, count: int = 1): @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('count', value=cmdutils.Value.count) -def move_to_next_word(tab: apitypes.Tab, count: int = 1): +def move_to_next_word(tab: apitypes.Tab, count: int = 1) -> None: """Move the cursor or selection to the next word. Args: @@ -98,7 +98,7 @@ def move_to_next_word(tab: apitypes.Tab, count: int = 1): @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('count', value=cmdutils.Value.count) -def move_to_prev_word(tab: apitypes.Tab, count: int = 1): +def move_to_prev_word(tab: apitypes.Tab, count: int = 1) -> None: """Move the cursor or selection to the previous word. Args: @@ -109,14 +109,14 @@ def move_to_prev_word(tab: apitypes.Tab, count: int = 1): @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) -def move_to_start_of_line(tab: apitypes.Tab): +def move_to_start_of_line(tab: apitypes.Tab) -> None: """Move the cursor or selection to the start of the line.""" tab.caret.move_to_start_of_line() @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) -def move_to_end_of_line(tab: apitypes.Tab): +def move_to_end_of_line(tab: apitypes.Tab) -> None: """Move the cursor or selection to the end of line.""" tab.caret.move_to_end_of_line() @@ -124,7 +124,7 @@ def move_to_end_of_line(tab: apitypes.Tab): @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('count', value=cmdutils.Value.count) -def move_to_start_of_next_block(tab: apitypes.Tab, count: int = 1): +def move_to_start_of_next_block(tab: apitypes.Tab, count: int = 1) -> None: """Move the cursor or selection to the start of next block. Args: @@ -136,7 +136,7 @@ def move_to_start_of_next_block(tab: apitypes.Tab, count: int = 1): @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('count', value=cmdutils.Value.count) -def move_to_start_of_prev_block(tab: apitypes.Tab, count: int = 1): +def move_to_start_of_prev_block(tab: apitypes.Tab, count: int = 1) -> None: """Move the cursor or selection to the start of previous block. Args: @@ -148,7 +148,7 @@ def move_to_start_of_prev_block(tab: apitypes.Tab, count: int = 1): @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('count', value=cmdutils.Value.count) -def move_to_end_of_next_block(tab: apitypes.Tab, count: int = 1): +def move_to_end_of_next_block(tab: apitypes.Tab, count: int = 1) -> None: """Move the cursor or selection to the end of next block. Args: @@ -160,7 +160,7 @@ def move_to_end_of_next_block(tab: apitypes.Tab, count: int = 1): @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('count', value=cmdutils.Value.count) -def move_to_end_of_prev_block(tab: apitypes.Tab, count: int = 1): +def move_to_end_of_prev_block(tab: apitypes.Tab, count: int = 1) -> None: """Move the cursor or selection to the end of previous block. Args: @@ -171,35 +171,35 @@ def move_to_end_of_prev_block(tab: apitypes.Tab, count: int = 1): @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) -def move_to_start_of_document(tab: apitypes.Tab): +def move_to_start_of_document(tab: apitypes.Tab) -> None: """Move the cursor or selection to the start of the document.""" tab.caret.move_to_start_of_document() @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) -def move_to_end_of_document(tab: apitypes.Tab): +def move_to_end_of_document(tab: apitypes.Tab) -> None: """Move the cursor or selection to the end of the document.""" tab.caret.move_to_end_of_document() @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) -def toggle_selection(tab: apitypes.Tab): +def toggle_selection(tab: apitypes.Tab) -> None: """Toggle caret selection mode.""" tab.caret.toggle_selection() @cmdutils.register(modes=[cmdutils.KeyMode.caret]) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) -def drop_selection(tab: apitypes.Tab): +def drop_selection(tab: apitypes.Tab) -> None: """Drop selection and keep selection mode enabled.""" tab.caret.drop_selection() @cmdutils.register() @cmdutils.argument('tab_obj', value=cmdutils.Value.cur_tab) -def follow_selected(tab_obj: apitypes.Tab, *, tab=False): +def follow_selected(tab_obj: apitypes.Tab, *, tab: bool = False) -> None: """Follow the selected text. Args: diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index 2f9e2f5e5..60715c65b 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -37,7 +37,7 @@ from qutebrowser.api import cmdutils, apitypes, message, config @cmdutils.register(name='reload') @cmdutils.argument('tab', value=cmdutils.Value.count_tab) -def reloadpage(tab, force=False): +def reloadpage(tab: apitypes.Tab, force: bool = False) -> None: """Reload the current/[count]th tab. Args: @@ -50,7 +50,7 @@ def reloadpage(tab, force=False): @cmdutils.register() @cmdutils.argument('tab', value=cmdutils.Value.count_tab) -def stop(tab): +def stop(tab: apitypes.Tab) -> None: """Stop loading in the current/[count]th tab. Args: @@ -60,9 +60,9 @@ def stop(tab): tab.stop() -def _print_preview(tab): +def _print_preview(tab: apitypes.Tab) -> None: """Show a print preview.""" - def print_callback(ok): + def print_callback(ok: bool) -> None: if not ok: message.error("Printing failed!") @@ -76,7 +76,7 @@ def _print_preview(tab): diag.exec_() -def _print_pdf(tab, filename): +def _print_pdf(tab: apitypes.Tab, filename: str) -> None: """Print to the given PDF file.""" tab.printing.check_pdf_support() filename = os.path.expanduser(filename) @@ -90,7 +90,9 @@ def _print_pdf(tab, filename): @cmdutils.register(name='print') @cmdutils.argument('tab', value=cmdutils.Value.count_tab) @cmdutils.argument('pdf', flag='f', metavar='file') -def printpage(tab, preview=False, *, pdf=None): +def printpage(tab: apitypes.Tab, + preview: bool = False, *, + pdf: str = None) -> None: """Print the current/[count]th tab. Args: @@ -114,7 +116,7 @@ def printpage(tab, preview=False, *, pdf=None): @cmdutils.register() @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) -def home(tab): +def home(tab: apitypes.Tab) -> None: """Open main startpage in current tab.""" if tab.data.pinned: message.info("Tab is pinned!") @@ -124,7 +126,7 @@ def home(tab): @cmdutils.register(debug=True) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) -def debug_dump_page(tab, dest, plain=False): +def debug_dump_page(tab: apitypes.Tab, dest: str, plain: bool = False) -> None: """Dump the current page's content to a file. Args: @@ -133,7 +135,7 @@ def debug_dump_page(tab, dest, plain=False): """ dest = os.path.expanduser(dest) - def callback(data): + def callback(data: str) -> None: """Write the data to disk.""" try: with open(dest, 'w', encoding='utf-8') as f: @@ -148,13 +150,13 @@ def debug_dump_page(tab, dest, plain=False): @cmdutils.register(maxsplit=0) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) -def insert_text(tab, text): +def insert_text(tab: apitypes.Tab, text: str) -> None: """Insert text at cursor position. Args: text: The text to insert. """ - def _insert_text_cb(elem): + def _insert_text_cb(elem: apitypes.WebElement) -> None: if elem is None: message.error("No element focused!") return @@ -170,7 +172,7 @@ def insert_text(tab, text): @cmdutils.register() @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('filter_', choices=['id']) -def click_element(tab, filter_: str, value: str, *, +def click_element(tab: apitypes.Tab, filter_: str, value: str, *, target: apitypes.ClickTarget = apitypes.ClickTarget.normal, force_event: bool = False) -> None: @@ -186,7 +188,7 @@ def click_element(tab, filter_: str, value: str, *, target: How to open the clicked element (normal/tab/tab-bg/window). force_event: Force generating a fake click event. """ - def single_cb(elem): + def single_cb(elem: apitypes.WebElement) -> None: """Click a single element.""" if elem is None: message.error("No element found with id {}!".format(value)) @@ -207,7 +209,7 @@ def click_element(tab, filter_: str, value: str, *, @cmdutils.register(debug=True) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('count', value=cmdutils.Value.count) -def debug_webaction(tab, action, count=1): +def debug_webaction(tab: apitypes.Tab, action: str, count: int = 1) -> None: """Execute a webaction. Available actions: @@ -227,7 +229,7 @@ def debug_webaction(tab, action, count=1): @cmdutils.register() @cmdutils.argument('tab', value=cmdutils.Value.count_tab) -def tab_mute(tab): +def tab_mute(tab: apitypes.Tab) -> None: """Mute/Unmute the current/[count]th tab. Args: @@ -242,12 +244,12 @@ def tab_mute(tab): @cmdutils.register() -def nop(): +def nop() -> None: """Do nothing.""" @cmdutils.register() -def message_error(text): +def message_error(text: str) -> None: """Show an error message in the statusbar. Args: @@ -258,7 +260,7 @@ def message_error(text): @cmdutils.register() @cmdutils.argument('count', value=cmdutils.Value.count) -def message_info(text, count=1): +def message_info(text: str, count: int = 1) -> None: """Show an info message in the statusbar. Args: @@ -270,7 +272,7 @@ def message_info(text, count=1): @cmdutils.register() -def message_warning(text): +def message_warning(text: str) -> None: """Show a warning message in the statusbar. Args: @@ -281,7 +283,7 @@ def message_warning(text): @cmdutils.register(debug=True) @cmdutils.argument('typ', choices=['exception', 'segfault']) -def debug_crash(typ='exception'): +def debug_crash(typ: str = 'exception') -> None: """Crash for debugging purposes. Args: @@ -295,7 +297,7 @@ def debug_crash(typ='exception'): @cmdutils.register(debug=True, maxsplit=0, no_cmd_split=True) -def debug_trace(expr=""): +def debug_trace(expr: str = "") -> None: """Trace executed code via hunter. Args: diff --git a/qutebrowser/components/scrollcommands.py b/qutebrowser/components/scrollcommands.py index 4a6d9cb99..0b8943f2d 100644 --- a/qutebrowser/components/scrollcommands.py +++ b/qutebrowser/components/scrollcommands.py @@ -112,7 +112,7 @@ def scroll_to_perc(tab: apitypes.Tab, count: int = None, @cmdutils.register() @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) -def scroll_to_anchor(tab: apitypes.Tab, name): +def scroll_to_anchor(tab: apitypes.Tab, name: str) -> None: """Scroll to the given anchor in the document. Args: diff --git a/qutebrowser/components/zoomcommands.py b/qutebrowser/components/zoomcommands.py index 05d2d8481..69f1df1c4 100644 --- a/qutebrowser/components/zoomcommands.py +++ b/qutebrowser/components/zoomcommands.py @@ -25,7 +25,7 @@ from qutebrowser.api import cmdutils, apitypes, message, config @cmdutils.register() @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('count', value=cmdutils.Value.count) -def zoom_in(tab: apitypes.Tab, count=1, quiet=False): +def zoom_in(tab: apitypes.Tab, count: int = 1, quiet: bool = False) -> None: """Increase the zoom level for the current tab. Args: @@ -43,7 +43,7 @@ def zoom_in(tab: apitypes.Tab, count=1, quiet=False): @cmdutils.register() @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('count', value=cmdutils.Value.count) -def zoom_out(tab: apitypes.Tab, count=1, quiet=False): +def zoom_out(tab: apitypes.Tab, count: int = 1, quiet: bool = False) -> None: """Decrease the zoom level for the current tab. Args: @@ -61,7 +61,10 @@ def zoom_out(tab: apitypes.Tab, count=1, quiet=False): @cmdutils.register() @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('count', value=cmdutils.Value.count) -def zoom(tab: apitypes.Tab, level=None, count=None, quiet=False): +def zoom(tab: apitypes.Tab, + level: str = None, + count: int = None, + quiet: bool = False) -> None: """Set the zoom level for the current tab. The zoom can be given as argument or as [count]. If neither is @@ -75,19 +78,19 @@ def zoom(tab: apitypes.Tab, level=None, count=None, quiet=False): """ if level is not None: try: - level = int(level.rstrip('%')) + int_level = int(level.rstrip('%')) except ValueError: raise cmdutils.CommandError("zoom: Invalid int value {}" .format(level)) if count is not None: - level = count - elif level is None: - level = config.val.zoom.default + int_level = count + elif int_level is None: + int_level = config.val.zoom.default try: - tab.zoom.set_factor(float(level) / 100) + tab.zoom.set_factor(int_level / 100) except ValueError: - raise cmdutils.CommandError("Can't zoom {}%!".format(level)) + raise cmdutils.CommandError("Can't zoom {}%!".format(int_level)) if not quiet: - message.info("Zoom level: {}%".format(int(level)), replace=True) + message.info("Zoom level: {}%".format(int_level), replace=True)