type hints for container.py & handle no stdout

We added the following check as well:

+        if stdout_callback and p.stdout is not None:

Because, according to the subprocess docs[1]:

>  If the stdout argument was not PIPE, this attribute is None.

In this case, it should not need to confirm that p.stdout is not
None in the mypy static analysis. However it still complained. So
we made mypy the favor and confirmed this was the case.

[1]: https://docs.python.org/3/library/subprocess.html#subprocess.Popen.stdout
This commit is contained in:
deeplow 2022-07-21 12:40:46 +01:00
parent 78daf75638
commit f99131e30c
No known key found for this signature in database
GPG key ID: 577982871529A52A

View file

@ -5,6 +5,8 @@ import platform
import shutil
import subprocess
import tempfile
from collections.abc import Callable
from typing import List, Optional
import appdirs
@ -28,7 +30,7 @@ log = logging.getLogger(__name__)
container_name = "dangerzone.rocks/dangerzone"
def exec(args, stdout_callback=None):
def exec(args: List[str], stdout_callback: Callable[[str], None] = None) -> int:
args_str = " ".join(pipes.quote(s) for s in args)
log.info("> " + args_str)
@ -41,7 +43,7 @@ def exec(args, stdout_callback=None):
universal_newlines=True,
startupinfo=startupinfo,
) as p:
if stdout_callback:
if stdout_callback and p.stdout is not None:
for line in p.stdout:
stdout_callback(line)
@ -49,7 +51,11 @@ def exec(args, stdout_callback=None):
return p.returncode
def exec_container(command, extra_args=[], stdout_callback=None):
def exec_container(
command: List[str],
extra_args: List[str] = [],
stdout_callback: Callable[[str], None] = None,
) -> int:
if container_tech == "podman":
container_runtime = shutil.which("podman")
if container_runtime is None:
@ -84,7 +90,12 @@ def exec_container(command, extra_args=[], stdout_callback=None):
return exec(args, stdout_callback)
def convert(input_filename, output_filename, ocr_lang, stdout_callback):
def convert(
input_filename: str,
output_filename: str,
ocr_lang: Optional[str],
stdout_callback: Callable[[str], None],
) -> bool:
success = False
if ocr_lang: