Skip to content

Commit c4f17c5

Browse files
AlexWaygoodGlyphack
authored andcommitted
pythongh-109653: Defer importing warnings in several modules (python#110286)
1 parent fc7925e commit c4f17c5

File tree

6 files changed

+10
-6
lines changed

6 files changed

+10
-6
lines changed

Lib/argparse.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@
8989
import re as _re
9090
import sys as _sys
9191

92-
import warnings
93-
9492
from gettext import gettext as _, ngettext
9593

9694
SUPPRESS = '==SUPPRESS=='
@@ -910,6 +908,7 @@ def __init__(self,
910908
# parser.add_argument('-f', action=BooleanOptionalAction, type=int)
911909
for field_name in ('type', 'choices', 'metavar'):
912910
if locals()[field_name] is not _deprecated_default:
911+
import warnings
913912
warnings._deprecated(
914913
field_name,
915914
"{name!r} is deprecated as of Python 3.12 and will be "
@@ -1700,6 +1699,7 @@ def _remove_action(self, action):
17001699
self._group_actions.remove(action)
17011700

17021701
def add_argument_group(self, *args, **kwargs):
1702+
import warnings
17031703
warnings.warn(
17041704
"Nesting argument groups is deprecated.",
17051705
category=DeprecationWarning,
@@ -1728,6 +1728,7 @@ def _remove_action(self, action):
17281728
self._group_actions.remove(action)
17291729

17301730
def add_mutually_exclusive_group(self, *args, **kwargs):
1731+
import warnings
17311732
warnings.warn(
17321733
"Nesting mutually exclusive groups is deprecated.",
17331734
category=DeprecationWarning,

Lib/calendar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from enum import IntEnum, global_enum
1111
import locale as _locale
1212
from itertools import repeat
13-
import warnings
1413

1514
__all__ = ["IllegalMonthError", "IllegalWeekdayError", "setfirstweekday",
1615
"firstweekday", "isleap", "leapdays", "weekday", "monthrange",
@@ -44,6 +43,7 @@ def __str__(self):
4443

4544
def __getattr__(name):
4645
if name in ('January', 'February'):
46+
import warnings
4747
warnings.warn(f"The '{name}' attribute is deprecated, use '{name.upper()}' instead",
4848
DeprecationWarning, stacklevel=2)
4949
if name == 'January':

Lib/getpass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import io
1919
import os
2020
import sys
21-
import warnings
2221

2322
__all__ = ["getpass","getuser","GetPassWarning"]
2423

@@ -118,6 +117,7 @@ def win_getpass(prompt='Password: ', stream=None):
118117

119118

120119
def fallback_getpass(prompt='Password: ', stream=None):
120+
import warnings
121121
warnings.warn("Can not control echo on the terminal.", GetPassWarning,
122122
stacklevel=2)
123123
if not stream:

Lib/shutil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import fnmatch
1111
import collections
1212
import errno
13-
import warnings
1413

1514
try:
1615
import zlib
@@ -723,6 +722,7 @@ def rmtree(path, ignore_errors=False, onerror=None, *, onexc=None, dir_fd=None):
723722
"""
724723

725724
if onerror is not None:
725+
import warnings
726726
warnings.warn("onerror argument is deprecated, use onexc instead",
727727
DeprecationWarning, stacklevel=2)
728728

Lib/tarfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import struct
4747
import copy
4848
import re
49-
import warnings
5049

5150
try:
5251
import pwd
@@ -2219,6 +2218,7 @@ def _get_filter_function(self, filter):
22192218
if filter is None:
22202219
filter = self.extraction_filter
22212220
if filter is None:
2221+
import warnings
22222222
warnings.warn(
22232223
'Python 3.14 will, by default, filter extracted tar '
22242224
+ 'archives and reject files or modify their metadata. '
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Slightly improve the import time of several standard-library modules by
2+
deferring imports of :mod:`warnings` within those modules. Patch by Alex
3+
Waygood.

0 commit comments

Comments
 (0)