Skip to content

Commit 4405ce1

Browse files
committed
🐛 fix: set PYTHON_COLORS=0 while formatting usage
On Python 3.14 argparse checks PYTHON_COLORS before NO_COLOR, so a doc build with PYTHON_COLORS=1 kept coloring the usage text and the `usage: ` prefix slice removed the wrong characters. Setting both variables during format_usage yields plain output in every shell.
1 parent e1b56d0 commit 4405ce1

6 files changed

Lines changed: 56 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
88
- Fix Sphinx warnings about parallel reads.
99
- Add `force_args_lower` to enable `:ref:` links with mixed-case program names and arguments.
1010
- Fix Sphinx smart quotes rewriting `--` to an en dash in `--option` names within descriptions, epilogs, and help text.
11+
- Keep ANSI color codes out of usage blocks when `PYTHON_COLORS=1` is set on Python 3.14 or newer.
1112

1213
## 1.13.1
1314

roots/test-python-colors/conf.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from __future__ import annotations
2+
3+
import os
4+
import sys
5+
from pathlib import Path
6+
7+
sys.path.insert(0, str(Path(__file__).parent))
8+
os.environ["PYTHON_COLORS"] = "1"
9+
extensions = ["sphinx_argparse_cli"]
10+
nitpicky = True

roots/test-python-colors/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.. sphinx_argparse_cli::
2+
:module: parser
3+
:func: make

roots/test-python-colors/parser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from __future__ import annotations
2+
3+
from argparse import ArgumentParser
4+
5+
6+
def make() -> ArgumentParser:
7+
parser = ArgumentParser(prog="prog", add_help=False)
8+
parser.add_argument("--flag", help="a flag")
9+
parser.add_subparsers().add_parser("run", add_help=False).add_argument("--magic", help="magic")
10+
return parser

src/sphinx_argparse_cli/_logic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ def _mk_usage(self, parser: ArgumentParser) -> literal_block:
396396

397397
@contextmanager
398398
def _no_color(self) -> Iterator[None]:
399-
with patch.dict(os.environ, {"NO_COLOR": "1"}, clear=False):
399+
# PYTHON_COLORS=1 outranks NO_COLOR in argparse, so both must be set to get plain text
400+
with patch.dict(os.environ, {"NO_COLOR": "1", "PYTHON_COLORS": "0"}, clear=False):
400401
yield
401402

402403

tests/test_logic.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,36 @@ def test_ref_prefix_doc(build_outcome: str) -> None:
215215
assert ref in build_outcome
216216

217217

218+
@pytest.mark.sphinx(buildername="text", testroot="python-colors")
219+
def test_usage_ignores_python_colors(build_outcome: str) -> None:
220+
assert (
221+
build_outcome
222+
== """prog - CLI interface
223+
********************
224+
225+
prog [--flag FLAG] {run} ...
226+
227+
228+
prog options
229+
============
230+
231+
* **"--flag"** "FLAG" - a flag
232+
233+
234+
prog run
235+
========
236+
237+
prog run [--magic MAGIC]
238+
239+
240+
prog run options
241+
----------------
242+
243+
* **"--magic"** "MAGIC" - magic
244+
"""
245+
)
246+
247+
218248
@pytest.mark.sphinx(buildername="text", testroot="ref-duplicate-label")
219249
def test_ref_duplicate_label(build_outcome: tuple[str, str], warning: StringIO) -> None:
220250
assert build_outcome

0 commit comments

Comments
 (0)