Use lazy imports in argparse · Issue #154638 · python/cpython · GitHub
Skip to content

Use lazy imports in argparse #154638

Description

@savannahostrowski

argparse currently defers importing copy, difflib, shutil, textwrap, and warnings using inline imports, while importing re and gettext eagerly at module scope. I think these can safely be replaced with module-level explicit lazy imports, which will make the module’s dependencies more readable and reduce the cost of a bare import argparse.

On my machine, preliminary release-build measurements using -S -X importtime show the median cumulative import time for argparse decreasing by approximately 67%, from 4,792 µs to 1,595 µs across 15 alternating runs.

Linked PRs

Activity

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

Metadata

Metadata

Labels

3.16new features, bugs and security fixesperformancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directory

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions