Skip to content

argparse: type=FileType("wb"), default=sys.stdout returns _io.TextIOWrapper instead of _io.BufferedWriter #134410

Open
@Chaz6

Description

@Chaz6

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

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions