gh-140448: Default `suggest_on_error` to `True` in `argparse.Argument… · python/cpython@d2f3cfd · GitHub
Skip to content

Commit d2f3cfd

Browse files
authored
gh-140448: Default suggest_on_error to True in argparse.ArgumentParser (#140450)
1 parent d51be28 commit d2f3cfd

6 files changed

Lines changed: 36 additions & 26 deletions

File tree

Doc/library/argparse.rst

Lines changed: 15 additions & 16 deletions

Doc/whatsnew/3.15.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,13 @@ New modules
317317
Improved modules
318318
================
319319

320+
argparse
321+
--------
322+
323+
* Changed the *suggest_on_error* parameter of :class:`argparse.ArgumentParser` to
324+
default to ``True``. This enables suggestions for mistyped arguments by default.
325+
(Contributed by Jakob Schluse in :gh:`140450`.)
326+
320327
calendar
321328
--------
322329

Lib/argparse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,7 +1857,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
18571857
- exit_on_error -- Determines whether or not ArgumentParser exits with
18581858
error info when an error occurs
18591859
- suggest_on_error - Enables suggestions for mistyped argument choices
1860-
and subparser names (default: ``False``)
1860+
and subparser names (default: ``True``)
18611861
- color - Allow color output in help messages (default: ``False``)
18621862
"""
18631863

@@ -1876,7 +1876,7 @@ def __init__(self,
18761876
allow_abbrev=True,
18771877
exit_on_error=True,
18781878
*,
1879-
suggest_on_error=False,
1879+
suggest_on_error=True,
18801880
color=True,
18811881
):
18821882
superinit = super(ArgumentParser, self).__init__

Lib/test/test_argparse.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,7 +2287,7 @@ class TestArgumentAndSubparserSuggestions(TestCase):
22872287
"""Test error handling and suggestion when a user makes a typo"""
22882288

22892289
def test_wrong_argument_error_with_suggestions(self):
2290-
parser = ErrorRaisingArgumentParser(suggest_on_error=True)
2290+
parser = ErrorRaisingArgumentParser()
22912291
parser.add_argument('foo', choices=['bar', 'baz'])
22922292
with self.assertRaises(ArgumentParserError) as excinfo:
22932293
parser.parse_args(('bazz',))
@@ -2307,7 +2307,7 @@ def test_wrong_argument_error_no_suggestions(self):
23072307
)
23082308

23092309
def test_wrong_argument_subparsers_with_suggestions(self):
2310-
parser = ErrorRaisingArgumentParser(suggest_on_error=True)
2310+
parser = ErrorRaisingArgumentParser()
23112311
subparsers = parser.add_subparsers(required=True)
23122312
subparsers.add_parser('foo')
23132313
subparsers.add_parser('bar')
@@ -2331,18 +2331,19 @@ def test_wrong_argument_subparsers_no_suggestions(self):
23312331
excinfo.exception.stderr,
23322332
)
23332333

2334-
def test_wrong_argument_no_suggestion_implicit(self):
2335-
parser = ErrorRaisingArgumentParser()
2334+
def test_wrong_argument_with_suggestion_explicit(self):
2335+
parser = ErrorRaisingArgumentParser(suggest_on_error=True)
23362336
parser.add_argument('foo', choices=['bar', 'baz'])
23372337
with self.assertRaises(ArgumentParserError) as excinfo:
23382338
parser.parse_args(('bazz',))
23392339
self.assertIn(
2340-
"error: argument foo: invalid choice: 'bazz' (choose from bar, baz)",
2340+
"error: argument foo: invalid choice: 'bazz', maybe you meant"
2341+
" 'baz'? (choose from bar, baz)",
23412342
excinfo.exception.stderr,
23422343
)
23432344

23442345
def test_suggestions_choices_empty(self):
2345-
parser = ErrorRaisingArgumentParser(suggest_on_error=True)
2346+
parser = ErrorRaisingArgumentParser()
23462347
parser.add_argument('foo', choices=[])
23472348
with self.assertRaises(ArgumentParserError) as excinfo:
23482349
parser.parse_args(('bazz',))
@@ -2352,7 +2353,7 @@ def test_suggestions_choices_empty(self):
23522353
)
23532354

23542355
def test_suggestions_choices_int(self):
2355-
parser = ErrorRaisingArgumentParser(suggest_on_error=True)
2356+
parser = ErrorRaisingArgumentParser()
23562357
parser.add_argument('foo', choices=[1, 2])
23572358
with self.assertRaises(ArgumentParserError) as excinfo:
23582359
parser.parse_args(('3',))
@@ -2362,7 +2363,7 @@ def test_suggestions_choices_int(self):
23622363
)
23632364

23642365
def test_suggestions_choices_mixed_types(self):
2365-
parser = ErrorRaisingArgumentParser(suggest_on_error=True)
2366+
parser = ErrorRaisingArgumentParser()
23662367
parser.add_argument('foo', choices=[1, '2'])
23672368
with self.assertRaises(ArgumentParserError) as excinfo:
23682369
parser.parse_args(('3',))

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,7 @@ David Scherer
16811681
Wolfgang Scherer
16821682
Felix Scherz
16831683
Hynek Schlawack
1684+
Jakob Schluse
16841685
Bob Schmertz
16851686
Gregor Schmid
16861687
Ralf Schmitt
Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)