Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
Open
@Chaz6

Description

@Chaz6
Issue body actions

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 dirPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    Status

    No status
    Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.