Skip to content

Commit

Permalink
pythongh-120417: Add #noqa to used imports in the stdlib
Browse files Browse the repository at this point in the history
Tools such as ruff can ignore "imported but unused" warnings if a
line ends with "# noqa". It avoids the temptation to remove an import
which is used effectively.
  • Loading branch information
vstinner committed Jun 12, 2024
1 parent 97b69db commit 8347c30
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
_setmode = None

import io
from io import (__all__, SEEK_SET, SEEK_CUR, SEEK_END)
from io import (__all__, SEEK_SET, SEEK_CUR, SEEK_END) # noqa

valid_seek_flags = {0, 1, 2} # Hardwired values
if hasattr(os, 'SEEK_HOLE') :
Expand Down
2 changes: 1 addition & 1 deletion Lib/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def interact(banner=None, readfunc=None, local=None, exitmsg=None, local_exit=Fa
console.raw_input = readfunc
else:
try:
import readline
import readline # noqa
except ImportError:
pass
console.interact(banner, exitmsg)
Expand Down
2 changes: 1 addition & 1 deletion Lib/codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,4 +1129,4 @@ def make_encoding_map(decoding_map):
# package
_false = 0
if _false:
import encodings
import encodings # noqa
4 changes: 2 additions & 2 deletions Lib/datetime.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
try:
from _datetime import *
from _datetime import __doc__
from _datetime import __doc__ # noqa
except ImportError:
from _pydatetime import *
from _pydatetime import __doc__
from _pydatetime import __doc__ # noqa

__all__ = ("date", "datetime", "time", "timedelta", "timezone", "tzinfo",
"MINYEAR", "MAXYEAR", "UTC")
8 changes: 4 additions & 4 deletions Lib/decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@

try:
from _decimal import *
from _decimal import __version__
from _decimal import __libmpdec_version__
from _decimal import __version__ # noqa
from _decimal import __libmpdec_version__ # noqa
except ImportError:
from _pydecimal import *
from _pydecimal import __version__
from _pydecimal import __libmpdec_version__
from _pydecimal import __version__ # noqa
from _pydecimal import __libmpdec_version__ # noqa
2 changes: 1 addition & 1 deletion Lib/hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def __hash_new(name, data=b'', **kwargs):

try:
# OpenSSL's scrypt requires OpenSSL 1.1+
from _hashlib import scrypt
from _hashlib import scrypt # noqa
except ImportError:
pass

Expand Down
2 changes: 1 addition & 1 deletion Lib/lzma.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import io
import os
from _lzma import *
from _lzma import _encode_filter_properties, _decode_filter_properties
from _lzma import _encode_filter_properties, _decode_filter_properties # noqa
import _compression


Expand Down
4 changes: 2 additions & 2 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import _opcode
from _opcode import stack_effect

from _opcode_metadata import (_specializations, _specialized_opmap, opmap,
HAVE_ARGUMENT, MIN_INSTRUMENTED_OPCODE)
from _opcode_metadata import (_specializations, _specialized_opmap, opmap, # noqa
HAVE_ARGUMENT, MIN_INSTRUMENTED_OPCODE) # noqa
EXTENDED_ARG = opmap['EXTENDED_ARG']

opname = ['<%r>' % (op,) for op in range(max(opmap.values()) + 1)]
Expand Down
2 changes: 1 addition & 1 deletion Lib/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def ixor(a, b):
except ImportError:
pass
else:
from _operator import __doc__
from _operator import __doc__ # noqa

# All of these "__func__ = func" assignments have to happen after importing
# from _operator to make sure they're set to the right function
Expand Down
2 changes: 1 addition & 1 deletion Lib/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def java_ver(release='', vendor='', vminfo=('', '', ''), osinfo=('', '', '')):
warnings._deprecated('java_ver', remove=(3, 15))
# Import the needed APIs
try:
import java.lang
import java.lang # noqa
except ImportError:
return release, vendor, vminfo, osinfo

Expand Down
2 changes: 1 addition & 1 deletion Lib/pstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ def f8(x):
if __name__ == '__main__':
import cmd
try:
import readline
import readline # noqa
except ImportError:
pass

Expand Down
5 changes: 3 additions & 2 deletions Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ class or function within a module or module in a package. If the
from reprlib import Repr
from traceback import format_exception_only

from _pyrepl.pager import (get_pager, plain, escape_less, pipe_pager,
plain_pager, tempfile_pager, tty_pager)
from _pyrepl.pager import (get_pager, escape_less, pipe_pager,
plain_pager, tempfile_pager, tty_pager,
plain) # noqa


# --------------------------------------------------------- old names
Expand Down
12 changes: 6 additions & 6 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,10 +485,10 @@ def register_readline():
"""
import atexit
try:
import readline
import rlcompleter
import _pyrepl.readline
import _pyrepl.unix_console
import readline # noqa
import rlcompleter # noqa
import _pyrepl.readline # noqa
import _pyrepl.unix_console # noqa
except ImportError:
return

Expand Down Expand Up @@ -603,7 +603,7 @@ def execsitecustomize():
"""Run custom site specific code, if available."""
try:
try:
import sitecustomize
import sitecustomize # noqa
except ImportError as exc:
if exc.name == 'sitecustomize':
pass
Expand All @@ -623,7 +623,7 @@ def execusercustomize():
"""Run custom user specific code, if available."""
try:
try:
import usercustomize
import usercustomize # noqa
except ImportError as exc:
if exc.name == 'usercustomize':
pass
Expand Down
20 changes: 10 additions & 10 deletions Lib/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,26 @@

import _ssl # if we can't import it, let the error propagate

from _ssl import OPENSSL_VERSION_NUMBER, OPENSSL_VERSION_INFO, OPENSSL_VERSION
from _ssl import _SSLContext, MemoryBIO, SSLSession
from _ssl import OPENSSL_VERSION_NUMBER, OPENSSL_VERSION_INFO, OPENSSL_VERSION # noqa
from _ssl import _SSLContext, MemoryBIO, SSLSession # noqa
from _ssl import (
SSLError, SSLZeroReturnError, SSLWantReadError, SSLWantWriteError,
SSLSyscallError, SSLEOFError, SSLCertVerificationError
SSLError, SSLZeroReturnError, SSLWantReadError, SSLWantWriteError, # noqa
SSLSyscallError, SSLEOFError, SSLCertVerificationError # noqa
)
from _ssl import txt2obj as _txt2obj, nid2obj as _nid2obj
from _ssl import RAND_status, RAND_add, RAND_bytes
from _ssl import RAND_status, RAND_add, RAND_bytes # noqa
try:
from _ssl import RAND_egd
from _ssl import RAND_egd # noqa
except ImportError:
# RAND_egd is not supported on some platforms
pass


from _ssl import (
HAS_SNI, HAS_ECDH, HAS_NPN, HAS_ALPN, HAS_SSLv2, HAS_SSLv3, HAS_TLSv1,
HAS_TLSv1_1, HAS_TLSv1_2, HAS_TLSv1_3, HAS_PSK
HAS_SNI, HAS_ECDH, HAS_NPN, HAS_ALPN, HAS_SSLv2, HAS_SSLv3, HAS_TLSv1, # noqa
HAS_TLSv1_1, HAS_TLSv1_2, HAS_TLSv1_3, HAS_PSK # noqa
)
from _ssl import _DEFAULT_CIPHERS, _OPENSSL_API_VERSION
from _ssl import _DEFAULT_CIPHERS, _OPENSSL_API_VERSION # noqa

_IntEnum._convert_(
'_SSLMethod', __name__,
Expand Down Expand Up @@ -255,7 +255,7 @@ class _TLSMessageType:


if sys.platform == "win32":
from _ssl import enum_certificates, enum_crls
from _ssl import enum_certificates, enum_crls # noqa

from socket import socket, SOCK_STREAM, create_connection
from socket import SOL_SOCKET, SO_TYPE, _GLOBAL_DEFAULT_TIMEOUT
Expand Down
4 changes: 2 additions & 2 deletions Lib/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
]

from _struct import *
from _struct import _clearcache
from _struct import __doc__
from _struct import _clearcache # noqa
from _struct import __doc__ # noqa
20 changes: 10 additions & 10 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@

if _mswindows:
import _winapi
from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP,
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
STD_ERROR_HANDLE, SW_HIDE,
STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW,
STARTF_FORCEONFEEDBACK, STARTF_FORCEOFFFEEDBACK,
ABOVE_NORMAL_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS,
HIGH_PRIORITY_CLASS, IDLE_PRIORITY_CLASS,
NORMAL_PRIORITY_CLASS, REALTIME_PRIORITY_CLASS,
CREATE_NO_WINDOW, DETACHED_PROCESS,
CREATE_DEFAULT_ERROR_MODE, CREATE_BREAKAWAY_FROM_JOB)
from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP, # noqa
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, # noqa
STD_ERROR_HANDLE, SW_HIDE, # noqa
STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW, # noqa
STARTF_FORCEONFEEDBACK, STARTF_FORCEOFFFEEDBACK, # noqa
ABOVE_NORMAL_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS, # noqa
HIGH_PRIORITY_CLASS, IDLE_PRIORITY_CLASS, # noqa
NORMAL_PRIORITY_CLASS, REALTIME_PRIORITY_CLASS, # noqa
CREATE_NO_WINDOW, DETACHED_PROCESS, # noqa
CREATE_DEFAULT_ERROR_MODE, CREATE_BREAKAWAY_FROM_JOB) # noqa

__all__.extend(["CREATE_NEW_CONSOLE", "CREATE_NEW_PROCESS_GROUP",
"STD_INPUT_HANDLE", "STD_OUTPUT_HANDLE",
Expand Down
6 changes: 3 additions & 3 deletions Lib/symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import _symtable
from _symtable import (
USE,
DEF_GLOBAL, DEF_NONLOCAL, DEF_LOCAL,
DEF_PARAM, DEF_TYPE_PARAM,
DEF_FREE_CLASS,
DEF_NONLOCAL, DEF_LOCAL,
DEF_PARAM, DEF_TYPE_PARAM, DEF_FREE_CLASS,
DEF_GLOBAL, # noqa
DEF_IMPORT, DEF_BOUND, DEF_ANNOT,
DEF_COMP_ITER, DEF_COMP_CELL,
SCOPE_OFF, SCOPE_MASK,
Expand Down

0 comments on commit 8347c30

Please sign in to comment.