W1510 subprocess-run-check

Message

'Using subprocess.run without explicitly set `check` is not recommended.'

Description

The check parameter should always be used with explicitly set check keyword to make clear what the error-handling behavior is.https://docs.python.org/3/library/subprocess.html#subprocess.runs

Example

The run call in the example will succeed whether the command is successful or not. This is a problem because we silently ignore errors.

import subprocess
def example():
    proc = subprocess.run("ls")
    return proc.stdout
3:11: W1510: Using subprocess.run without explicitly set `check` is not recommended.

When we pass check=True, the behavior changes towards raising an exception when the return code of the command is non-zero.

>>> subprocess.run(["ls", "/does-not-exist"], capture_output=True)
CompletedProcess(args=['ls', '/does-not-exist'], returncode=2, stdout=b'', stderr=b"ls: cannot access '/does-not-exist': No such file or directory\n")

>>> subprocess.run(["ls", "/does-not-exist"], capture_output=True, check=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/subprocess.py", line 512, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['ls', '/does-not-exist']' returned non-zero exit status 2.
import subprocess
def example():
    proc = subprocess.run("ls", check=True)
    return proc.stdout
No issues found.

Running Pylint locally? In your CI?

There is a better way! Run PyCodeQual & get Pylint findings with history, diffs and statistics.

Click here to find out more.