I have a function like this:
def run_process(path: str, *args: str) -> dict:
cmd = subprocess.run(
["command", *args, path],
capture_output=True,
)
ret = {
"stdout": cmd.stdout.decode("utf-8"),
"stderr": cmd.stderr.decode("utf-8"),
"returncode": cmd.returncode,
}
return ret
I’m trying to add a complete type hint for the ret
dict. I tried Dict[str, Union[str, int]]
But mypy has some issues with this:
error: Incompatible return value type (got "Dict[str, object]", expected "Dict[str, Union[str, int]]") [return-value]
If I try to use it as run_process["stderr"].splitlines()
, mypy says:
error: Item "int" of "Union[str, int]" has no attribute "splitlines"
How would I solve this?