-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-143984: Replace optparse with argparse in Lib/test/test_decimal.py #143983
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
base: main
Are you sure you want to change the base?
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
…mal.py The optparse module is deprecated since Python 3.2. This change migrates Lib/test/test_decimal.py to use the argparse module for command line argument parsing. This involves: - Importing argparse instead of optparse. - Using ArgumentParser instead of OptionParser. - Updating argument definitions (add_argument). - Escaping '%' characters in help strings. - Updating execution logic to use the argparse Namespace object.
24d59b9 to
5fee791
Compare
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
| @@ -0,0 +1 @@ | |||
| Replace the usage of the deprecated optparse module with argparse in Lib/test/test_decimal.py. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in the issue, optparse is no longer deprecated.
| Replace the usage of the deprecated optparse module with argparse in Lib/test/test_decimal.py. | |
| Replace :mod:`optparse` with :mod:`argparse` in ``Lib/test/test_decimal.py``. |
| import argparse | ||
| parser = argparse.ArgumentParser(usage="test_decimal.py [--debug] [{--skip | test1 [test2 [...]]}]") | ||
| parser.add_argument('--debug', '-d', action='store_true', help='shows the test number and context before each test') | ||
| parser.add_argument('--skip', '-s', action='store_true', help='skip over 90%% of the arithmetic tests') | ||
| parser.add_argument('tests', nargs='*', help='specific tests to run') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(This replaces my previous review.)
Let argparse generate the usage, and it'll use colour for the different parts.
And put the positional arg first, to match help output.
The short option usually goes before the long, and this matches -h, --help. Also wrap long lines.
Finally, the previous usage made it clear you couldn't skip AND pass a list of tests: [{--skip | test1 [test2 [...]]}]
Although optparse didn't enforce it.
With argparse, we can enforce it with a mutually exclusive group:
| import argparse | |
| parser = argparse.ArgumentParser(usage="test_decimal.py [--debug] [{--skip | test1 [test2 [...]]}]") | |
| parser.add_argument('--debug', '-d', action='store_true', help='shows the test number and context before each test') | |
| parser.add_argument('--skip', '-s', action='store_true', help='skip over 90%% of the arithmetic tests') | |
| parser.add_argument('tests', nargs='*', help='specific tests to run') | |
| import argparse | |
| parser = argparse.ArgumentParser() | |
| group = parser.add_mutually_exclusive_group() | |
| group.add_argument("tests", nargs="*", default=[], help="specific tests to run") | |
| group.add_argument( | |
| "-s", | |
| "--skip", | |
| action="store_true", | |
| help="skip over 90%% of the arithmetic tests", | |
| ) | |
| parser.add_argument( | |
| "-d", | |
| "--debug", | |
| action="store_true", | |
| help="shows the test number and context before each test", | |
| ) |
Description
Lib/test/test_decimal.pycurrently uses the deprecatedoptparsemodule for parsing command-line arguments. This task involves migrating it to useargparse.Proposed Changes
optparseimports withargparse.argparse.ArgumentParser.--debug,--skip) are supported.%in help strings).Verification
python Lib/test/test_decimal.py --helppython Lib/test/test_decimal.py --skip