From fc384b15bf5be19ce1c491f7631137433508de7d Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 13 Jun 2024 16:22:35 +0200 Subject: [PATCH] gh-120417: Add more #noqa to used imports in the stdlib Tools such as ruff can ignore "imported but unused" warnings if a line ends with "# noqa: F401". It avoids the temptation to remove an import which is used effectively. --- Lib/collections/abc.py | 4 ++-- Lib/ctypes/__init__.py | 18 +++++++++--------- Lib/importlib/machinery.py | 24 ++++++++++++------------ Lib/importlib/util.py | 14 +++++++------- Lib/ssl.py | 20 ++++++++++---------- Lib/subprocess.py | 2 +- Lib/xml/sax/__init__.py | 10 +++++----- 7 files changed, 46 insertions(+), 46 deletions(-) diff --git a/Lib/collections/abc.py b/Lib/collections/abc.py index 86ca8b8a8414b3..bff76291634604 100644 --- a/Lib/collections/abc.py +++ b/Lib/collections/abc.py @@ -1,3 +1,3 @@ from _collections_abc import * -from _collections_abc import __all__ -from _collections_abc import _CallableGenericAlias +from _collections_abc import __all__ # noqa: F401 +from _collections_abc import _CallableGenericAlias # noqa: F401 diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index b7ee46d664ab08..117f34ff048e03 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -5,12 +5,12 @@ __version__ = "1.1.0" -from _ctypes import Union, Structure, Array -from _ctypes import _Pointer +from _ctypes import Union, Structure, Array # noqa: F401 +from _ctypes import _Pointer # noqa: F401 from _ctypes import CFuncPtr as _CFuncPtr from _ctypes import __version__ as _ctypes_version from _ctypes import RTLD_LOCAL, RTLD_GLOBAL -from _ctypes import ArgumentError +from _ctypes import ArgumentError # noqa: F401 from _ctypes import SIZEOF_TIME_T from struct import calcsize as _calcsize @@ -138,8 +138,8 @@ class WinFunctionType(_CFuncPtr): elif _os.name == "posix": from _ctypes import dlopen as _dlopen -from _ctypes import sizeof, byref, addressof, alignment, resize -from _ctypes import get_errno, set_errno +from _ctypes import sizeof, byref, addressof, alignment, resize # noqa: F401 +from _ctypes import get_errno, set_errno # noqa: F401 from _ctypes import _SimpleCData def _check_size(typ, typecode=None): @@ -252,7 +252,7 @@ class c_void_p(_SimpleCData): class c_bool(_SimpleCData): _type_ = "?" -from _ctypes import POINTER, pointer, _pointer_type_cache +from _ctypes import POINTER, pointer, _pointer_type_cache # noqa: F401 class c_wchar_p(_SimpleCData): _type_ = "Z" @@ -492,7 +492,7 @@ def LoadLibrary(self, name): oledll = LibraryLoader(OleDLL) GetLastError = windll.kernel32.GetLastError - from _ctypes import get_last_error, set_last_error + from _ctypes import get_last_error, set_last_error # noqa: F401 def WinError(code=None, descr=None): if code is None: @@ -568,8 +568,8 @@ def DllCanUnloadNow(): return 0 # S_OK return ccom.DllCanUnloadNow() -from ctypes._endian import BigEndianStructure, LittleEndianStructure -from ctypes._endian import BigEndianUnion, LittleEndianUnion +from ctypes._endian import BigEndianStructure, LittleEndianStructure # noqa: F401 +from ctypes._endian import BigEndianUnion, LittleEndianUnion # noqa: F401 # Fill in specifically-sized types c_int8 = c_byte diff --git a/Lib/importlib/machinery.py b/Lib/importlib/machinery.py index fbd30b159fb752..86d0c35c5caad3 100644 --- a/Lib/importlib/machinery.py +++ b/Lib/importlib/machinery.py @@ -1,19 +1,19 @@ """The machinery of importlib: finders, loaders, hooks, etc.""" -from ._bootstrap import ModuleSpec -from ._bootstrap import BuiltinImporter -from ._bootstrap import FrozenImporter -from ._bootstrap_external import (SOURCE_SUFFIXES, DEBUG_BYTECODE_SUFFIXES, +from ._bootstrap import ModuleSpec # noqa: F401 +from ._bootstrap import BuiltinImporter # noqa: F401 +from ._bootstrap import FrozenImporter # noqa: F401 +from ._bootstrap_external import (SOURCE_SUFFIXES, DEBUG_BYTECODE_SUFFIXES, # noqa: F401 OPTIMIZED_BYTECODE_SUFFIXES, BYTECODE_SUFFIXES, EXTENSION_SUFFIXES) -from ._bootstrap_external import WindowsRegistryFinder -from ._bootstrap_external import PathFinder -from ._bootstrap_external import FileFinder -from ._bootstrap_external import SourceFileLoader -from ._bootstrap_external import SourcelessFileLoader -from ._bootstrap_external import ExtensionFileLoader -from ._bootstrap_external import AppleFrameworkLoader -from ._bootstrap_external import NamespaceLoader +from ._bootstrap_external import WindowsRegistryFinder # noqa: F401 +from ._bootstrap_external import PathFinder # noqa: F401 +from ._bootstrap_external import FileFinder # noqa: F401 +from ._bootstrap_external import SourceFileLoader # noqa: F401 +from ._bootstrap_external import SourcelessFileLoader # noqa: F401 +from ._bootstrap_external import ExtensionFileLoader # noqa: F401 +from ._bootstrap_external import AppleFrameworkLoader # noqa: F401 +from ._bootstrap_external import NamespaceLoader # noqa: F401 def all_suffixes(): diff --git a/Lib/importlib/util.py b/Lib/importlib/util.py index c94a148e4c50e0..8156c0088f9e03 100644 --- a/Lib/importlib/util.py +++ b/Lib/importlib/util.py @@ -1,15 +1,15 @@ """Utility code for constructing importers, etc.""" from ._abc import Loader -from ._bootstrap import module_from_spec +from ._bootstrap import module_from_spec # noqa: F401 from ._bootstrap import _resolve_name -from ._bootstrap import spec_from_loader +from ._bootstrap import spec_from_loader # noqa: F401 from ._bootstrap import _find_spec -from ._bootstrap_external import MAGIC_NUMBER +from ._bootstrap_external import MAGIC_NUMBER # noqa: F401 from ._bootstrap_external import _RAW_MAGIC_NUMBER -from ._bootstrap_external import cache_from_source -from ._bootstrap_external import decode_source -from ._bootstrap_external import source_from_cache -from ._bootstrap_external import spec_from_file_location +from ._bootstrap_external import cache_from_source # noqa: F401 +from ._bootstrap_external import decode_source # noqa: F401 +from ._bootstrap_external import source_from_cache # noqa: F401 +from ._bootstrap_external import spec_from_file_location # noqa: F401 import _imp import sys diff --git a/Lib/ssl.py b/Lib/ssl.py index cc685c2cc405ab..8a78641b20a5ac 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: F401 +from _ssl import _SSLContext, MemoryBIO, SSLSession # noqa: F401 from _ssl import ( - SSLError, SSLZeroReturnError, SSLWantReadError, SSLWantWriteError, - SSLSyscallError, SSLEOFError, SSLCertVerificationError + SSLError, SSLZeroReturnError, SSLWantReadError, SSLWantWriteError, # noqa: F401 + SSLSyscallError, SSLEOFError, SSLCertVerificationError # noqa: F401 ) 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: F401 try: - from _ssl import RAND_egd + from _ssl import RAND_egd # noqa: F401 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: F401 + HAS_TLSv1_1, HAS_TLSv1_2, HAS_TLSv1_3, HAS_PSK # noqa: F401 ) -from _ssl import _DEFAULT_CIPHERS, _OPENSSL_API_VERSION +from _ssl import _DEFAULT_CIPHERS, _OPENSSL_API_VERSION # noqa: F401 _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: F401 from socket import socket, SOCK_STREAM, create_connection from socket import SOL_SOCKET, SO_TYPE, _GLOBAL_DEFAULT_TIMEOUT diff --git a/Lib/subprocess.py b/Lib/subprocess.py index b2dcb1454c139e..bc08878db313df 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -79,7 +79,7 @@ if _mswindows: import _winapi - from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP, + from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP, # noqa: F401 STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE, SW_HIDE, STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW, diff --git a/Lib/xml/sax/__init__.py b/Lib/xml/sax/__init__.py index b657310207cfe5..f4181974a05df9 100644 --- a/Lib/xml/sax/__init__.py +++ b/Lib/xml/sax/__init__.py @@ -20,10 +20,10 @@ """ from .xmlreader import InputSource -from .handler import ContentHandler, ErrorHandler -from ._exceptions import SAXException, SAXNotRecognizedException, \ - SAXParseException, SAXNotSupportedException, \ - SAXReaderNotAvailable +from .handler import ContentHandler, ErrorHandler # noqa: F401 +from ._exceptions import (SAXException, SAXNotRecognizedException, # noqa: F401 + SAXParseException, SAXNotSupportedException, # noqa: F401 + SAXReaderNotAvailable) # noqa: F401 def parse(source, handler, errorHandler=ErrorHandler()): @@ -55,7 +55,7 @@ def parseString(string, handler, errorHandler=ErrorHandler()): # tell modulefinder that importing sax potentially imports expatreader _false = 0 if _false: - import xml.sax.expatreader + import xml.sax.expatreader # noqa: F401 import os, sys if not sys.flags.ignore_environment and "PY_SAX_PARSER" in os.environ: