Skip to content

Commit 4294bdc

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 3cfee42 commit 4294bdc

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
@@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
99
- Add `force_refs_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.
1111
- Register flags and positional arguments as Sphinx program options so the `:option:` role links to them.
12+
- Keep ANSI color codes out of usage blocks when `PYTHON_COLORS=1` is set on Python 3.14 or newer.
1213

1314
## 1.13.1
1415

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
@@ -398,7 +398,8 @@ def _mk_usage(self, parser: ArgumentParser) -> literal_block:
398398

399399
@contextmanager
400400
def _no_color(self) -> Iterator[None]:
401-
with patch.dict(os.environ, {"NO_COLOR": "1"}, clear=False):
401+
# PYTHON_COLORS=1 outranks NO_COLOR in argparse, so both must be set to get plain text
402+
with patch.dict(os.environ, {"NO_COLOR": "1", "PYTHON_COLORS": "0"}, clear=False):
402403
yield
403404

404405

tests/test_logic.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,36 @@ def test_option_role_as_html(build_outcome: str, warning: StringIO) -> None:
230230
assert not warning.getvalue()
231231

232232

233+
@pytest.mark.sphinx(buildername="text", testroot="python-colors")
234+
def test_usage_ignores_python_colors(build_outcome: str) -> None:
235+
assert (
236+
build_outcome
237+
== """prog - CLI interface
238+
********************
239+
240+
prog [--flag FLAG] {run} ...
241+
242+
243+
prog options
244+
============
245+
246+
* **"--flag"** "FLAG" - a flag
247+
248+
249+
prog run
250+
========
251+
252+
prog run [--magic MAGIC]
253+
254+
255+
prog run options
256+
----------------
257+
258+
* **"--magic"** "MAGIC" - magic
259+
"""
260+
)
261+
262+
233263
@pytest.mark.sphinx(buildername="text", testroot="ref-duplicate-label")
234264
def test_ref_duplicate_label(build_outcome: tuple[str, str], warning: StringIO) -> None:
235265
assert build_outcome

0 commit comments

Comments
 (0)