chore: use subprocess.run instead of old check_*#210
Merged
Conversation
pradyunsg
reviewed
Apr 13, 2025
pradyunsg
reviewed
Apr 13, 2025
takluyver
reviewed
Feb 18, 2026
takluyver
reviewed
Feb 18, 2026
Contributor
Author
|
This removes the ability to check and see why a command failed! My version: >>> try:
... subprocess.run(["ruff", "invalid"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
... except subprocess.CalledProcessError as err:
... print(err.stdout, err.stderr)
...
b'' b"error: unrecognized subcommand 'invalid'\n\nUsage: ruff [OPTIONS] <COMMAND>\n\nFor more information, try '--help'.\n"Current version: >>> try:
... subprocess.run(["ruff", "invalid"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
... except subprocess.CalledProcessError as err:
... print(err.stdout, err.stderr)
...
None NoneIf a command fails, users will want to know why it failed! This was available before (though with stdout and stderr merged, I didn't do that in my example here): >>> try:
... subprocess.check_output(["ruff", "invalid"], stderr=subprocess.STDOUT)
... except subprocess.CalledProcessError as err:
... print(err.stdout, err.stderr)
...
b"error: unrecognized subcommand 'invalid'\n\nUsage: ruff [OPTIONS] <COMMAND>\n\nFor more information, try '--help'.\n" NoneWe catch and print this all the time in build: https://github.com/search?q=repo%3Apypa%2Fbuild%20CalledProcessError&type=code |
Member
|
You're right, my mistake. I figured that if the return value wasn't used,
there was no way to get the output, but I forgot about the exception having
it.
Please knock my commit off the branch. If you want to add a comment & a
test, it might stop me making the same mistake in a couple of years time. 😉
…On Wed, 18 Feb 2026, 18:33 Henry Schreiner, ***@***.***> wrote:
*henryiii* left a comment (pypa/pyproject-hooks#210)
<#210 (comment)>
This removes the ability to check and see why a command failed! My version:
>>> try:
... subprocess.run(["ruff", "invalid"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
... except subprocess.CalledProcessError as err:
... print(err.stdout, err.stderr)
...
b'' b"error: unrecognized subcommand 'invalid'\n\nUsage: ruff [OPTIONS] <COMMAND>\n\nFor more information, try '--help'.\n"
Current version:
>>> try:
... subprocess.run(["ruff", "invalid"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
... except subprocess.CalledProcessError as err:
... print(err.stdout, err.stderr)
...
None None
If a command fails, users will want to know why it failed!
This was available before (though with stdout and stderr merged):
>>> try:
... subprocess.check_output(["ruff", "invalid"], stderr=subprocess.STDOUT)
... except subprocess.CalledProcessError as err:
... print(err.stdout, err.stderr)
...
b"error: unrecognized subcommand 'invalid'\n\nUsage: ruff [OPTIONS] <COMMAND>\n\nFor more information, try '--help'.\n" None
We catch and print this all the time in build:
https://github.com/search?q=repo%3Apypa%2Fbuild%20CalledProcessError&type=code
—
Reply to this email directly, view it on GitHub
<#210 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACQB5KQCK2T6HGPPOMTG434MSO65AVCNFSM6AAAAACVR5GY6KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTSMRSGE3DCOBYGE>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Pradyun Gedam <pradyunsg@gmail.com>
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
1c5c97d to
f11cee2
Compare
Member
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I noticed this was using the old
subprocess.check_*methods. In modern Python,subprocess.runis recommended.