You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
optparse: parsing many positional arguments takes quadratic time #161282
AI-needs-explanationAI-assisted work that needs further explanation and follow up from the contributorstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
OptionParser.parse_args() is quadratic in the number of positional arguments: _process_args() removes each one from the front of rargs with del rargs[0].
importoptparse, timeparser=optparse.OptionParser()
parser.add_option('-v', action='store_true')
args= ['-v'] + [f'file{i}'foriinrange(200_000)]
start=time.perf_counter()
parser.parse_args(args)
print(f'{time.perf_counter() -start:.3f} s') # 2.445 s on main, 0.009 s with the fix
Thank you for taking the time to open this issue. Python is a community-driven project, and we want it to be a welcoming place to learn and collaborate.
This issue appears to have been written with AI assistance. Please explain your issue in your own words and describe how you checked the information you provided. This helps us understand your issue and leaves more time to address other issues.
optparse removes positional arguments one at a time using del rargs[0], causing quadratic performance due to repeated list shifting. With 200k arguments, parsing takes ~2.5s instead of ~8ms.
I changed it to remove consecutive positional arguments in a single operation using del rargs[:end], keeping option handling and callbacks unchanged.
FYI I used caude code to investigate and prepare the fix, then verified it myself with benchmarks, the full test suite, and a fuzz test covering ~200k random argument lists. The output matched the old implementation.
AI-needs-explanationAI-assisted work that needs further explanation and follow up from the contributorstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
Activity
bedevere-app commentedon Nov 4, 2026
Thank you for taking the time to open this issue. Python is a community-driven project, and we want it to be a welcoming place to learn and collaborate.
This issue appears to have been written with AI assistance. Please explain your issue in your own words and describe how you checked the information you provided. This helps us understand your issue and leaves more time to address other issues.
Please read our guidelines for using AI tools.
thejesh23 commentedon Nov 4, 2026
optparse removes positional arguments one at a time using
del rargs[0], causing quadratic performance due to repeated list shifting. With 200k arguments, parsing takes ~2.5s instead of ~8ms.I changed it to remove consecutive positional arguments in a single operation using
del rargs[:end], keeping option handling and callbacks unchanged.FYI I used caude code to investigate and prepare the fix, then verified it myself with benchmarks, the full test suite, and a fuzz test covering ~200k random argument lists. The output matched the old implementation.
thejesh23 commentedon Dec 4, 2026
command-line tool that still uses optparse being handed a long list of file paths