From f4a7255378041d5ff0c2bf677c9e5de0f490dbee Mon Sep 17 00:00:00 2001 From: kihuni Date: Sun, 16 Nov 2025 10:42:41 +0300 Subject: [PATCH] Fixed #36321 -- Updated test for suggest_on_error on Python 3.14. Updated test_invalid_choice_db_option to handle the new error message format with suggestions on Python 3.14. --- django/core/management/base.py | 4 ++-- tests/admin_scripts/tests.py | 17 ++++++++++++----- tests/user_commands/tests.py | 8 +++++--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/django/core/management/base.py b/django/core/management/base.py index db7a01cc4c..23a367f424 100644 --- a/django/core/management/base.py +++ b/django/core/management/base.py @@ -15,7 +15,7 @@ from django.core import checks from django.core.exceptions import ImproperlyConfigured from django.core.management.color import color_style, no_style from django.db import DEFAULT_DB_ALIAS, connections -from django.utils.version import PY314, PY315 +from django.utils.version import PY314 ALL_CHECKS = "__all__" @@ -58,7 +58,7 @@ class CommandParser(ArgumentParser): ): self.missing_args_message = missing_args_message self.called_from_command_line = called_from_command_line - if PY314 and not PY315: + if PY314: kwargs.setdefault("suggest_on_error", True) super().__init__(**kwargs) diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index 9442822bd6..6bd19c8dba 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -40,7 +40,7 @@ from django.test import LiveServerTestCase, SimpleTestCase, TestCase, override_s from django.test.utils import captured_stderr, captured_stdout from django.urls import path from django.utils.functional import cached_property -from django.utils.version import PY313, get_docs_version +from django.utils.version import PY313, PY314, get_docs_version from django.views.static import serve from . import urls @@ -2464,10 +2464,17 @@ class Discovery(SimpleTestCase): class CommandDBOptionChoiceTests(SimpleTestCase): def test_invalid_choice_db_option(self): - expected_error = ( - r"Error: argument --database: invalid choice: 'deflaut' " - r"\(choose from '?default'?, '?other'?\)" - ) + # Update expected error based on Python version + if PY314: + expected_error = ( + r"Error: argument --database: invalid choice: 'deflaut', " + r"maybe you meant 'default'\? \(choose from default, other\)" + ) + else: + expected_error = ( + r"Error: argument --database: invalid choice: 'deflaut' " + r"\(choose from '?default'?, '?other'?\)" + ) args = [ "changepassword", "createsuperuser", diff --git a/tests/user_commands/tests.py b/tests/user_commands/tests.py index 76521b7c0c..090cf38a16 100644 --- a/tests/user_commands/tests.py +++ b/tests/user_commands/tests.py @@ -25,7 +25,7 @@ from django.db import connection from django.test import SimpleTestCase, override_settings from django.test.utils import captured_stderr, extend_sys_path from django.utils import translation -from django.utils.version import PY314, PY315 +from django.utils.version import PY314 from .management.commands import dance from .utils import AssertFormatterFailureCaughtContext @@ -456,21 +456,23 @@ class CommandTests(SimpleTestCase): self.assertIn("Working...", out.getvalue()) self.assertIs(mocked_flush.called, True) - @unittest.skipUnless(PY314 and not PY315, "Requires Python 3.14") + @unittest.skipUnless(PY314, "Requires Python 3.14") def test_suggest_on_error_defaults_true(self): """ CommandParser sets suggest_on_error=True on Python 3.14. """ command = BaseCommand() + command._called_from_command_line = True parser = command.create_parser("prog_name", "subcommand") self.assertTrue(parser.suggest_on_error) - @unittest.skipUnless(PY314 and not PY315, "Requires Python 3.14") + @unittest.skipUnless(PY314, "Requires Python 3.14") def test_suggest_on_error_custom(self): """ Explicit suggest_on_error=False is respected. """ command = BaseCommand() + command._called_from_command_line = True parser = command.create_parser( "prog_name", "subcommand", suggest_on_error=False )