Not planned
Description
Bug report
Bug description:
When setting the default of an argument to sys.stdout and the type is argparse.FileType("wb")
then the class should be _io.BufferedWriter
not _io.TextIOWrapper
when no arguments are provided. When the argument is set to -
then the class is correctly set to _io.BufferedWriter
as expected.
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument("outfile", nargs="?", type=argparse.FileType("wb"), default=sys.stdout)
args = parser.parse_args("-")
print(type(args.outfile))
# <class '_io.BufferedWriter'>
# Correct
args = parser.parse_args()
print(type(args.outfile))
# <class '_io.TextIOWrapper'>
# Incorrect
CPython versions tested on:
3.14
Operating systems tested on:
Linux
Metadata
Metadata
Assignees
Projects
Status
Doc issues
Activity
F18-Ray commentedon May 22, 2025
But the type of
sys.stdout
is actually_io.TextIOWrapper
if you don't give it any args, so it's nothing to do with python.serhiy-storchaka commentedon May 31, 2025
Just use
default=sys.stdout.buffer
in your code.argparse
does nothing with the default value. It just takes it as given. If you specifydefault=sys.stdout
, it sets the default tosys.stdout
. If you specifydefault=42
, it sets the default to42
. If this is not correct, the error is on your side.