CLI Typo Suggestions are Enabled by Default in Python 3.15

CLI Typo Suggestions are Enabled by Default in Python 3.15

A command-line tool built with argparse rejects invalid values and lists the accepted ones, leaving users to spot typos themselves. Python 3.14 introduced suggestions for likely intended values, but enabling them required explicitly setting suggest_on_error=True when creating the parser.

For example, a tool that accepts a fixed set of output formats can request the suggestions explicitly:

import argparse

parser = argparse.ArgumentParser(suggest_on_error=True)
parser.add_argument('--format', choices=['json', 'yaml', 'xml'])
parser.parse_args()

Running the tool with a misspelled value:

python main.py --format yml

Python 3.14:

usage: main.py [-h] [--format {json,yaml,xml}]
main.py: error: argument --format: invalid choice: 'yml', maybe you meant 'yaml'? (choose from 'json', 'yaml', 'xml')

The suggestion appears only because the parser was created with suggest_on_error=True. Without that argument, the same mistake produces the plain message with no hint.

Since Python 3.15, the parameter defaults to True, so the suggestion is produced without it.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--format', choices=['json', 'yaml', 'xml'])
parser.parse_args()

Python 3.15:

usage: main.py [-h] [--format {json,yaml,xml}]
main.py: error: argument --format: invalid choice: 'yml', maybe you meant 'yaml'? (choose from 'json', 'yaml', 'xml')

The output is the same, but the parser no longer has to ask for it. Existing tools gain the suggestions on upgrade without any change to their source code.

Leave a Comment

Cancel reply

Your email address will not be published.