From 8b846820d2a5761738c0530d989e6c6418d745ca Mon Sep 17 00:00:00 2001 From: Alex Pyrgiotis Date: Mon, 27 Mar 2023 15:07:40 +0300 Subject: [PATCH] Update typing hints for Mypy 1.1.1 Due to a bump in our Python dependencies, we now install Mypy 1.1.1 instead of 0.982. This change triggered the following errors: * Incompatible default for argument (default has type None, argument has type ): Mypy further explains here that PEP 484 prohibits implicit Optional, so we need to make these types explicit Optional. * Unused "type: ignore" comment, use narrower [method-assign] instead of [assignment]: Mypy has specialized some of its lints, meaning that we should switch to the newer variants. Also, it detected several other small inconsistencies. We fix all of these errors in this commit. --- container/dangerzone.py | 8 +++++--- dangerzone/args.py | 2 +- dangerzone/document.py | 4 ++-- dangerzone/gui/__init__.py | 2 +- dangerzone/gui/logic.py | 4 ++-- dangerzone/gui/main_window.py | 2 +- dangerzone/isolation_provider/dummy.py | 2 +- tests/test_cli.py | 8 +++++--- 8 files changed, 18 insertions(+), 14 deletions(-) diff --git a/container/dangerzone.py b/container/dangerzone.py index f69529b..4996c23 100644 --- a/container/dangerzone.py +++ b/container/dangerzone.py @@ -30,7 +30,9 @@ TIMEOUT_PER_MB: float = 30 # (seconds) TIMEOUT_MIN: float = 60 # (seconds) -async def read_stream(sr: asyncio.StreamReader, callback: Callable = None) -> bytes: +async def read_stream( + sr: asyncio.StreamReader, callback: Optional[Callable] = None +) -> bytes: """Consume a byte stream line-by-line. Read all lines in a stream until EOF. If a user has passed a callback, call it for @@ -59,8 +61,8 @@ async def run_command( error_message: str, timeout_message: str, timeout: Optional[float], - stdout_callback: Callable = None, - stderr_callback: Callable = None, + stdout_callback: Optional[Callable] = None, + stderr_callback: Optional[Callable] = None, ) -> Tuple[bytes, bytes]: """Run a command and get its output. diff --git a/dangerzone/args.py b/dangerzone/args.py index 8deb01d..f273e65 100644 --- a/dangerzone/args.py +++ b/dangerzone/args.py @@ -105,4 +105,4 @@ def override_parser_and_check_suspicious_options(click_main: click.Command) -> N check_suspicious_options(args) return orig_parse_fn(ctx, args) - click_main.parse_args = custom_parse_fn # type: ignore [assignment] + click_main.parse_args = custom_parse_fn # type: ignore [method-assign] diff --git a/dangerzone/document.py b/dangerzone/document.py index 36145a9..9013713 100644 --- a/dangerzone/document.py +++ b/dangerzone/document.py @@ -33,8 +33,8 @@ class Document: def __init__( self, - input_filename: str = None, - output_filename: str = None, + input_filename: Optional[str] = None, + output_filename: Optional[str] = None, suffix: str = SAFE_EXTENSION, archive: bool = False, ) -> None: diff --git a/dangerzone/gui/__init__.py b/dangerzone/gui/__init__.py index ca7dee5..a40a7e4 100644 --- a/dangerzone/gui/__init__.py +++ b/dangerzone/gui/__init__.py @@ -55,7 +55,7 @@ class Application(QtWidgets.QApplication): return self.original_event(event) - self.event = monkeypatch_event # type: ignore [assignment] + self.event = monkeypatch_event # type: ignore [method-assign] @click.command() diff --git a/dangerzone/gui/logic.py b/dangerzone/gui/logic.py index cf0c062..654a2ac 100644 --- a/dangerzone/gui/logic.py +++ b/dangerzone/gui/logic.py @@ -6,7 +6,7 @@ import shlex import subprocess import typing from pathlib import Path -from typing import Dict +from typing import Dict, Optional from colorama import Fore @@ -129,7 +129,7 @@ class Alert(QtWidgets.QDialog): message: str, ok_text: str = "Ok", has_cancel: bool = True, - extra_button_text: str = None, + extra_button_text: Optional[str] = None, ) -> None: super(Alert, self).__init__() self.dangerzone = dangerzone diff --git a/dangerzone/gui/main_window.py b/dangerzone/gui/main_window.py index 413779d..1f9abd8 100644 --- a/dangerzone/gui/main_window.py +++ b/dangerzone/gui/main_window.py @@ -665,7 +665,7 @@ class ConvertTask(QtCore.QObject): self, dangerzone: DangerzoneGui, document: Document, - ocr_lang: str = None, + ocr_lang: Optional[str] = None, ) -> None: super(ConvertTask, self).__init__() self.document = document diff --git a/dangerzone/isolation_provider/dummy.py b/dangerzone/isolation_provider/dummy.py index b0ec822..16b8967 100644 --- a/dangerzone/isolation_provider/dummy.py +++ b/dangerzone/isolation_provider/dummy.py @@ -28,7 +28,7 @@ class Dummy(IsolationProvider): ) def install(self) -> bool: - pass + return True def _convert( self, diff --git a/tests/test_cli.py b/tests/test_cli.py index 24db954..76e8aa0 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -9,7 +9,7 @@ import sys import tempfile import traceback from pathlib import Path -from typing import Sequence +from typing import Optional, Sequence from unittest import mock import pytest @@ -56,7 +56,9 @@ class CLIResult(Result): self.print_info() raise - def assert_failure(self, exit_code: int = None, message: str = None) -> None: + def assert_failure( + self, exit_code: Optional[int] = None, message: Optional[str] = None + ) -> None: """Assert that the command failed. By default, check that the command has returned with an exit code @@ -111,7 +113,7 @@ class CLIResult(Result): class TestCli(TestBase): def run_cli( - self, args: Sequence[str] | str = (), tmp_path: Path = None + self, args: Sequence[str] | str = (), tmp_path: Optional[Path] = None ) -> CLIResult: """Run the CLI with the provided arguments.