add assert statements following .communicate() to help type checkers

After calling .communicate() on an asyncio.subprocess.Process object,
the attribute returncode gets set to a non-None value. Type checkers are
not able to figure this out.

Fixed by adding an assert to help type checkers out.

Signed-off-by: Olivier Gayot <olivier.gayot@canonical.com>
This commit is contained in:
Olivier Gayot 2022-04-05 10:13:50 +02:00
parent f0ea1d16c2
commit dd788f9eee
2 changed files with 4 additions and 0 deletions

View File

@ -78,6 +78,8 @@ class LoggedCommandRunner:
async def wait(self, proc: asyncio.subprocess.Process) \
-> subprocess.CompletedProcess:
stdout, stderr = await proc.communicate()
# .communicate() forces returncode to be set to a value
assert(proc.returncode is not None)
if proc.returncode != 0:
raise subprocess.CalledProcessError(proc.returncode, proc.args)
else:

View File

@ -84,6 +84,8 @@ async def arun_command(cmd: List[str], *,
if stderr is not None:
stderr = stderr.decode(encoding)
log.debug("arun_command %s exited with code %s", cmd, proc.returncode)
# .communicate() forces returncode to be set to a value
assert(proc.returncode is not None)
if check and proc.returncode != 0:
raise subprocess.CalledProcessError(proc.returncode, cmd)
else: