Skip to content

Navigation Menu

Sign in
Appearance settings

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

3.14+ argparse colorization makes sub-commands prog always colored if colored at creation time #139809

Copy link
Copy link
@gaborbernat

Description

@gaborbernat
Issue body actions

Bug report

Bug description:

Here's an example script that replicates the problem:

from __future__ import annotations

import os
from argparse import ArgumentParser
from contextlib import suppress
from unittest.mock import patch

os.environ["FORCE_COLOR"] = "1"

parser = ArgumentParser(description="argparse tester", prog="complex")
parser.add_argument("--root", action="store_true", help="root flag")
sub = parser.add_subparsers(dest="command")
sub.add_parser("demo")

with suppress(SystemExit):
    parser.parse_args(["demo", "--help"])

with patch.dict(os.environ, {"NO_COLOR": "1"}, clear=False):
    parser.parse_args(["demo", "--help"])

You can see the first invocation prints everything in color, while the second only the sub-command:

Image

This is because https://github.com/python/cpython/blame/main/Lib/argparse.py#L1966 passes through the prog formatted and not as raw. This example is contrived a bit to have a minimal demonstration, the actual issue I'm facing is in https://github.com/tox-dev/sphinx-argparse-cli where I'm reading this value directly to generate Sphinx documentation from this.

CPython versions tested on:

3.14, 3.15

Operating systems tested on:

Linux, macOS

Linked PRs

Reactions are currently unavailable

Metadata

Metadata

Labels

3.14bugs and security fixesbugs and security fixes3.15pre-release feature fixes, bugs and security fixespre-release feature fixes, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
No fields configured for issues without a type.

Projects

Status
Doc issues
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.