gh-109653: Defer importing warnings
in several modules
#110286
Merged
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.
warnings
is a pretty cheap import, so the import-time saving from deferring it is usually pretty minimal. However, it's also a textbook example of an import that should be deferred wherever reasonable. Most of the time, it's only imported so that aDeprecationWarning
can be emitted, and we should try to make it so that users only have to pay the (small) performance penalty from it being imported if they actually hit the specific code path that's deprecated.Nonetheless, since the performance impact of importing
warnings
is so small, I've tried to avoid modules that make multiple uses ofwarnings
-- it's not particularly DRY to have import warnings repeated several times in the same module. The one exception I made here isargparse
:argparse
really should be importing the absolute minimum set of modules given that it's a CLI framework (see #74338 for previous discussion).