Skip to content

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

Not planned
@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

Activity

added
type-bugAn unexpected behavior, bug, or error
on May 21, 2025
F18-Ray

F18-Ray commented on May 22, 2025

@F18-Ray

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

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

serhiy-storchaka commented on May 31, 2025

@serhiy-storchaka
Member

Just use default=sys.stdout.buffer in your code.

argparse does nothing with the default value. It just takes it as given. If you specify default=sys.stdout, it sets the default to sys.stdout. If you specify default=42, it sets the default to 42. If this is not correct, the error is on your side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Projects

    Status

    Doc issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

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

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant