summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSkyiesac <jainsachi1202@gmail.com>2025-12-18 16:35:38 +0530
committerJacob Walls <jacobtylerwalls@gmail.com>2025-12-22 14:02:21 -0500
commitd0d85cd165e54582cce98cf685252e771460a9d4 (patch)
tree60e9ed2fc2ea5926d9f5498bdee07ccc97d4630a
parent6025eab3c509b4de922117e16866bbfe0ee99aa6 (diff)
Fixed #36376 -- Fixed --no-color for command help in Python 3.14+.
https://github.com/python/cpython/pull/136809 made `color` default to True in ArgumentParser.
-rw-r--r--django/core/management/base.py7
-rw-r--r--docs/releases/5.2.10.txt4
-rw-r--r--docs/releases/6.0.1.txt4
-rw-r--r--tests/user_commands/tests.py28
4 files changed, 40 insertions, 3 deletions
diff --git a/django/core/management/base.py b/django/core/management/base.py
index db7a01cc4c..8f24479050 100644
--- a/django/core/management/base.py
+++ b/django/core/management/base.py
@@ -58,8 +58,11 @@ class CommandParser(ArgumentParser):
):
self.missing_args_message = missing_args_message
self.called_from_command_line = called_from_command_line
- if PY314 and not PY315:
- kwargs.setdefault("suggest_on_error", True)
+ if PY314:
+ if not PY315:
+ kwargs.setdefault("suggest_on_error", True)
+ if os.environ.get("DJANGO_COLORS") == "nocolor" or "--no-color" in sys.argv:
+ kwargs.setdefault("color", False)
super().__init__(**kwargs)
def parse_args(self, args=None, namespace=None):
diff --git a/docs/releases/5.2.10.txt b/docs/releases/5.2.10.txt
index 35626cfedc..0b7ab06d54 100644
--- a/docs/releases/5.2.10.txt
+++ b/docs/releases/5.2.10.txt
@@ -9,4 +9,6 @@ Django 5.2.10 fixes several bugs in 5.2.9.
Bugfixes
========
-* ...
+* Fixed a bug where management command colorized help (introduced in
+ Python 3.14) ignored the :option:`--no-color` option and the
+ :envvar:`DJANGO_COLORS` setting (:ticket:`36376`).
diff --git a/docs/releases/6.0.1.txt b/docs/releases/6.0.1.txt
index a84d49b07a..36e533a888 100644
--- a/docs/releases/6.0.1.txt
+++ b/docs/releases/6.0.1.txt
@@ -16,3 +16,7 @@ Bugfixes
* Fixed a regression in Django 6.0 that prevented changing the name of a
:class:`~django.db.models.ManyToManyField` from taking effect when applying
migrations (:ticket:`36800`).
+
+* Fixed a bug where management command colorized help (introduced in
+ Python 3.14) ignored the :option:`--no-color` option and the
+ :envvar:`DJANGO_COLORS` setting (:ticket:`36376`).
diff --git a/tests/user_commands/tests.py b/tests/user_commands/tests.py
index afd376307a..0997c10017 100644
--- a/tests/user_commands/tests.py
+++ b/tests/user_commands/tests.py
@@ -470,6 +470,34 @@ class CommandTests(SimpleTestCase):
)
self.assertFalse(parser.suggest_on_error)
+ @unittest.skipUnless(PY314, "Only relevant for Python 3.14+")
+ def test_color_enabled_by_default(self):
+ with mock.patch.dict(os.environ, {}, clear=True):
+ command = BaseCommand()
+ parser = command.create_parser("prog_name", "subcommand")
+ self.assertTrue(parser.color)
+
+ @unittest.skipUnless(PY314, "Only relevant for Python 3.14+")
+ def test_color_disabled_with_django_colors_nocolor(self):
+ with mock.patch.dict(os.environ, {"DJANGO_COLORS": "nocolor"}):
+ command = BaseCommand()
+ parser = command.create_parser("prog_name", "subcommand")
+ self.assertFalse(parser.color)
+
+ @unittest.skipUnless(PY314, "Only relevant for Python 3.14+")
+ def test_force_color_does_not_affect_argparse_color(self):
+ with mock.patch.dict(os.environ, {}, clear=True):
+ command = BaseCommand(force_color=True)
+ parser = command.create_parser("prog_name", "subcommand")
+ self.assertTrue(parser.color)
+
+ @unittest.skipUnless(PY314, "Only relevant for Python 3.14+")
+ def test_no_color_flag_disables_color(self):
+ with mock.patch.object(sys, "argv", ["manage.py", "mycommand", "--no-color"]):
+ command = BaseCommand()
+ parser = command.create_parser("manage.py", "mycommand")
+ self.assertFalse(parser.color)
+
class CommandRunTests(AdminScriptTestCase):
"""