Just as a brainstorm/out of curiosity, I did a little experiment to see what is necessary to forward the -W
options to the subprocess:
# > docker run --rm -it python:3.10-bookworm /bin/bash
mkdir /tmp/test && cd /tmp/test
cat <<EOF > wrapper.py
import sys
import subprocess
from itertools import chain
from pprint import pprint
print("\n\n--- wrapper ---")
pprint(sys.warnoptions)
args = [
sys.executable,
*chain.from_iterable(("-W", opt) for opt in sys.warnoptions),
*("-W", "default:not given by user"),
"_subprocess.py"
]
print(subprocess.check_output(args, text=True))
EOF
cat <<EOF > _subprocess.py
import sys
from pprint import pprint
print("\n\n--- _subprocess ---")
pprint(sys.warnoptions)
EOF
PYTHONWARNINGS='once:via env var' python -W 'error:hello' -W 'always:world' wrapper.py
This is the output:
--- wrapper ---
['once:via env var', 'error:hello', 'always:world']
--- _subprocess ---
['once:via env var', 'error:hello', 'always:world', 'default:not given by user']
So it seems that doing the following may be enough:
subprocess.run([
sys.executable,
*chain.from_iterable(("-W", opt) for opt in sys.warnoptions),
...
])