This commit is contained in:
Stephen kihuni 2025-11-17 18:26:53 +00:00 committed by GitHub
commit eb18eb4447
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 44 additions and 6 deletions

View file

@ -15,6 +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
ALL_CHECKS = "__all__"
@ -57,6 +58,8 @@ 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)
super().__init__(**kwargs)
def parse_args(self, args=None, namespace=None):

View file

@ -20,6 +20,7 @@ PY311 = sys.version_info >= (3, 11)
PY312 = sys.version_info >= (3, 12)
PY313 = sys.version_info >= (3, 13)
PY314 = sys.version_info >= (3, 14)
PY315 = sys.version_info >= (3, 15)
def get_version(version=None):

View file

@ -228,7 +228,9 @@ Logging
Management Commands
~~~~~~~~~~~~~~~~~~~
* ...
* Management commands now set :class:`~argparse.ArgumentParser`\'s
``suggest_on_error`` argument to ``True`` by default on Python 3.14, enabling
suggestions for mistyped subcommand names and argument choices.
Migrations
~~~~~~~~~~

View file

@ -38,7 +38,8 @@ from django.db.migrations.recorder import MigrationRecorder
from django.test import LiveServerTestCase, SimpleTestCase, TestCase, override_settings
from django.test.utils import captured_stderr, captured_stdout
from django.urls import path
from django.utils.version import PY313, get_docs_version
from django.utils.functional import cached_property
from django.utils.version import PY313, PY314, get_docs_version
from django.views.static import serve
from . import urls
@ -2446,6 +2447,13 @@ class Discovery(SimpleTestCase):
class CommandDBOptionChoiceTests(SimpleTestCase):
def test_invalid_choice_db_option(self):
# 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'?\)"

View file

@ -1,5 +1,6 @@
import os
import sys
import unittest
from argparse import ArgumentDefaultsHelpFormatter
from io import BytesIO, StringIO, TextIOWrapper
from pathlib import Path
@ -24,6 +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
from .management.commands import dance
from .utils import AssertFormatterFailureCaughtContext
@ -454,6 +456,28 @@ class CommandTests(SimpleTestCase):
self.assertIn("Working...", out.getvalue())
self.assertIs(mocked_flush.called, True)
@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, "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
)
self.assertFalse(parser.suggest_on_error)
class CommandRunTests(AdminScriptTestCase):
"""