Skip to content

Conversation

@tonghuaroot
Copy link

@tonghuaroot tonghuaroot commented Jan 18, 2026

Description

Lib/test/test_decimal.py currently uses the deprecated optparse module for parsing command-line arguments. This task involves migrating it to use argparse.

Proposed Changes

  • Replace optparse imports with argparse.
  • Update argument parsing logic to use argparse.ArgumentParser.
  • Ensure all existing command-line options (--debug, --skip) are supported.
  • Fix any formatting issues (e.g. % in help strings).

Verification

  • Run python Lib/test/test_decimal.py --help
  • Run python Lib/test/test_decimal.py --skip

@bedevere-app
Copy link

bedevere-app bot commented Jan 18, 2026

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 skip news label instead.

@bedevere-app bedevere-app bot added tests Tests in the Lib/test dir awaiting review labels Jan 18, 2026
@bedevere-app
Copy link

bedevere-app bot commented Jan 18, 2026

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 skip news label instead.

…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.
@tonghuaroot tonghuaroot changed the title Modernize Lib/test/test_decimal.py: use argparse instead of optparse gh-143984: Replace optparse with argparse in Lib/test/test_decimal.py Jan 18, 2026
@bedevere-app
Copy link

bedevere-app bot commented Jan 18, 2026

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 skip news label instead.

@@ -0,0 +1 @@
Replace the usage of the deprecated optparse module with argparse in Lib/test/test_decimal.py.
Copy link
Member

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.

Suggested change
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``.

Comment on lines +6031 to +6035
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')
Copy link
Member

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:

Suggested change
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",
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants