gh-137855: Improve import time of textwrap by lazy importing re modules#150156
Closed
XiaoWan-zi wants to merge 1 commit into
Closed
gh-137855: Improve import time of textwrap by lazy importing re modules#150156XiaoWan-zi wants to merge 1 commit into
XiaoWan-zi wants to merge 1 commit into
Conversation
picnixz
requested changes
May 20, 2026
Member
picnixz
left a comment
There was a problem hiding this comment.
This is a nontrivial change that may break subclasses so closing, sorry.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
textwrapimport time by lazy-importingre(PEP 810) instead of loading it at module import.TextWrapperword-separator regexes from class body to_compile_wordseps(), called on firstTextWrapper()instantiation. Compiled patterns remain class attributes, shared across instances (same semantics as before).LazyImportTest.test_lazy_importusingensure_lazy_imports()to guard against regressions.Part of the ongoing stdlib import-time work tracked in Improve import time of various stdlib modules #137855.
Motivation
textwrapis imported eagerly by many modules (e.g. tests,traceback,optparsepaths) often only fordedent()/indent(), which do not needre. Previously,import reat module level andre.compile()in theTextWrapperclass body causedreto load on everyimport textwrap.Before
After