From 8347c30f185ff3fd4862fb679977bba6cedf1095 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 12 Jun 2024 18:11:56 +0200 Subject: [PATCH] gh-120417: Add #noqa to used imports in the stdlib 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. --- Lib/_pyio.py | 2 +- Lib/code.py | 2 +- Lib/codecs.py | 2 +- Lib/datetime.py | 4 ++-- Lib/decimal.py | 8 ++++---- Lib/hashlib.py | 2 +- Lib/lzma.py | 2 +- Lib/opcode.py | 4 ++-- Lib/operator.py | 2 +- Lib/platform.py | 2 +- Lib/pstats.py | 2 +- Lib/pydoc.py | 5 +++-- Lib/site.py | 12 ++++++------ Lib/ssl.py | 20 ++++++++++---------- Lib/struct.py | 4 ++-- Lib/subprocess.py | 20 ++++++++++---------- Lib/symtable.py | 6 +++--- 17 files changed, 50 insertions(+), 49 deletions(-) diff --git a/Lib/_pyio.py b/Lib/_pyio.py index a3fede699218a1..2abdace8dc950d 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -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') : diff --git a/Lib/code.py b/Lib/code.py index b93902ccf545b3..ef3261128cda16 100644 --- a/Lib/code.py +++ b/Lib/code.py @@ -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) diff --git a/Lib/codecs.py b/Lib/codecs.py index 9b35b6127dd01c..56d3379577181e 100644 --- a/Lib/codecs.py +++ b/Lib/codecs.py @@ -1129,4 +1129,4 @@ def make_encoding_map(decoding_map): # package _false = 0 if _false: - import encodings + import encodings # noqa diff --git a/Lib/datetime.py b/Lib/datetime.py index a33d2d724cb33d..b9e8599f808268 100644 --- a/Lib/datetime.py +++ b/Lib/datetime.py @@ -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") diff --git a/Lib/decimal.py b/Lib/decimal.py index d61e374b9f9998..70a890fd86b74f 100644 --- a/Lib/decimal.py +++ b/Lib/decimal.py @@ -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 diff --git a/Lib/hashlib.py b/Lib/hashlib.py index 1b16441cb60ba7..5c2fa55235f950 100644 --- a/Lib/hashlib.py +++ b/Lib/hashlib.py @@ -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 diff --git a/Lib/lzma.py b/Lib/lzma.py index c1e3d33deb69a1..034805edeee919 100644 --- a/Lib/lzma.py +++ b/Lib/lzma.py @@ -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 diff --git a/Lib/opcode.py b/Lib/opcode.py index 85e37ff53e577f..21ef4aca8bd4e3 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -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)] diff --git a/Lib/operator.py b/Lib/operator.py index 02ccdaa13ddb31..1f685624763c2f 100644 --- a/Lib/operator.py +++ b/Lib/operator.py @@ -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 diff --git a/Lib/platform.py b/Lib/platform.py index a4fd2463f15a6c..ad1f147aebd47b 100644 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -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 diff --git a/Lib/pstats.py b/Lib/pstats.py index 2f054bb4011e7f..08c5321ee4ac2f 100644 --- a/Lib/pstats.py +++ b/Lib/pstats.py @@ -611,7 +611,7 @@ def f8(x): if __name__ == '__main__': import cmd try: - import readline + import readline # noqa except ImportError: pass diff --git a/Lib/pydoc.py b/Lib/pydoc.py index d7579c1cc3dcd1..3b9ac855532d4b 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -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 diff --git a/Lib/site.py b/Lib/site.py index 7eace190f5ab21..045e2b79556847 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -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 @@ -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 @@ -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 diff --git a/Lib/ssl.py b/Lib/ssl.py index cc685c2cc405ab..f430d2f95a1f4f 100644 --- a/Lib/ssl.py +++ b/Lib/ssl.py @@ -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__, @@ -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 diff --git a/Lib/struct.py b/Lib/struct.py index d6bba588636498..b7d2f7b71eca16 100644 --- a/Lib/struct.py +++ b/Lib/struct.py @@ -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 diff --git a/Lib/subprocess.py b/Lib/subprocess.py index b2dcb1454c139e..9b8a7bda331462 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -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", diff --git a/Lib/symtable.py b/Lib/symtable.py index d6ac1f527ba8ba..4e8eb4c2b32bf8 100644 --- a/Lib/symtable.py +++ b/Lib/symtable.py @@ -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,