PEP 787: Safer subprocess usage using t-strings

I don’t know enough about all the different combinations and possibilities here (especially Windows) but how about this as a suggestion:

  • Deprecate (in the soft sense) any use of run except for with a list of strings and with shell=False and also deprecate the shell parameter.
  • Add a new function shrun whose argument must be a t-string.

The intended usage is then:

def run(cmd: list[str], ...):
    """Launch subprocess"""
    ...

def shrun(cmd: Template, ...):
    """Run command in shell with interpolation"""
    ...

Then the suggestion is that anyone doing

run(f'echo {name}', shell=True)

should do

shrun(t'echo {name}')
2 Likes