-
-
Notifications
You must be signed in to change notification settings - Fork 31k
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
gh-118761: improve optparse import time by delaying textwrap import #128899
Conversation
Lib/optparse.py
Outdated
text_width = max(self.width - self.current_indent, 11) | ||
indent = " "*self.current_indent | ||
import textwrap |
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.
Style: move local imports to the top of the block
text_width = max(self.width - self.current_indent, 11) | |
indent = " "*self.current_indent | |
import textwrap | |
import textwrap | |
text_width = max(self.width - self.current_indent, 11) | |
indent = " "*self.current_indent |
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.
Sure, makes sense!
For what it's worth the reason I did it this way was a direct consequence of having just been reading argparse.py, which breaks this style suggestion, while documenting the motivation of the change. :) I suppose it is not worth bothering with the churn there as it's just a style nit on code that isn't being modified already...
Lib/optparse.py
Outdated
help_text = self.expand_default(option) | ||
import textwrap |
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.
Style: move local imports to the top of the block
help_text = self.expand_default(option) | |
import textwrap | |
import textwrap | |
help_text = self.expand_default(option) |
…port The same change was made, and for the same reason, by argparse back in 2017. The textwrap module is only used when printing help text, so most invocations will never need it imported. See: python#1269 See: 8110837
4484efc
to
10cd152
Compare
Out of curiosity, how are the |
As mentioned in the other PR -- I'm not used to benchmarking and I may be doing something totally wrong because I can get results saying this PR is now slower to import, or results saying it's faster, and hyperfine warns that I have lots of "outliers". But here's a favorable result. Produced by copying the old and new versions of the file so that I can import them both without switching branches:
|
The same change was made, and for the same reason, by ``argparse`` back in 2017. The ``textwrap`` module is only used when printing help text, so most invocations will never need it imported. Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
The same change was made, and for the same reason, by argparse back in
2017. The textwrap module is only used when printing help text, so most invocations will never need it imported.
See: #1269
See: 8110837