Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest the correct choice when invalid (Python 3.14+) #234

Merged
merged 2 commits into from
Nov 21, 2024
Merged

Conversation

hugovk
Copy link
Owner

@hugovk hugovk commented Nov 20, 2024

Before

python3.14 -m norwegianblue python --color ye
usage: python3.14 -m norwegianblue [-h] [-f {html,json,md,markdown,pretty,rst,csv,tsv,yaml}] [-c {yes,no,auto}] [--clear-cache] [--show-title {yes,no,auto}] [-v]
                                   [-V] [-w] [--pretty | --md | --rst | --json | --csv | --tsv | --html | --yaml]
                                   [product ...]
python3.14 -m norwegianblue: error: argument -c/--color: invalid choice: 'ye' (choose from yes, no, auto)

After

python3.14 -m norwegianblue python --color ye
usage: python3.14 -m norwegianblue [-h] [-f {html,json,md,markdown,pretty,rst,csv,tsv,yaml}] [-c {yes,no,auto}] [--clear-cache] [--show-title {yes,no,auto}] [-v]
                                   [-V] [-w] [--pretty | --md | --rst | --json | --csv | --tsv | --html | --yaml]
                                   [product ...]
python3.14 -m norwegianblue: error: argument -c/--color: invalid choice: 'ye', maybe you meant 'yes'? (choose from yes, no, auto)

Backwards compatibility

This was added in Python 3.14:

https://docs.python.org/3.14/library/argparse.html#suggest-on-error

So we can't do this:

 def main() -> None:
     parser = argparse.ArgumentParser(
-        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
+        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, suggest_on_error=True
     )

Because we'll get an error on 3.13 and earlier:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/hugo/github/norwegianblue/src/norwegianblue/__main__.py", line 6, in <module>
    cli.main()
    ~~~~~~~~^^
  File "/Users/hugo/github/norwegianblue/src/norwegianblue/cli.py", line 31, in main
    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, suggest_on_error=True
    )
TypeError: ArgumentParser.__init__() got an unexpected keyword argument 'suggest_on_error'

Instead, parser.suggest_on_error = True enables it on 3.14+ and prevents the error for 3.13 and older.

@hugovk hugovk added the changelog: Added For new features label Nov 20, 2024
@hugovk hugovk changed the title Suggest the correct choice when invalid Suggest the correct choice when invalid (Python 3.14+) Nov 21, 2024
@hugovk hugovk merged commit bc6df5b into main Nov 21, 2024
53 checks passed
@hugovk hugovk deleted the suggest_on_error branch November 21, 2024 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: Added For new features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant