From 6ae254aaa0a5a3985a52d1ab387a2b68c001bd96 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 13 Jun 2024 16:14:50 +0200 Subject: [PATCH] gh-120417: Add #noqa to used imports in the stdlib (#120421) 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/_pyio.py | 2 +- Lib/code.py | 2 +- Lib/codecs.py | 2 +- Lib/collections/__init__.py | 3 ++- Lib/concurrent/futures/process.py | 2 +- Lib/curses/__init__.py | 2 +- Lib/datetime.py | 4 ++-- Lib/decimal.py | 8 ++++---- Lib/hashlib.py | 2 +- Lib/lzma.py | 2 +- Lib/multiprocessing/context.py | 2 +- Lib/multiprocessing/util.py | 2 +- Lib/opcode.py | 4 ++-- Lib/operator.py | 2 +- Lib/platform.py | 2 +- Lib/pstats.py | 2 +- Lib/pydoc.py | 5 ++++- Lib/re/_constants.py | 2 +- Lib/site.py | 6 +++--- Lib/sqlite3/__main__.py | 2 +- Lib/struct.py | 4 ++-- Lib/symtable.py | 6 +++--- Lib/unittest/__init__.py | 4 ++-- Lib/urllib/request.py | 2 +- Lib/xml/dom/__init__.py | 2 +- 25 files changed, 40 insertions(+), 36 deletions(-) diff --git a/Lib/_pyio.py b/Lib/_pyio.py index a3fede69921..7d298e1674b 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -16,7 +16,7 @@ else: _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: F401 valid_seek_flags = {0, 1, 2} # Hardwired values if hasattr(os, 'SEEK_HOLE') : diff --git a/Lib/code.py b/Lib/code.py index b93902ccf54..a55fced0704 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: F401 except ImportError: pass console.interact(banner, exitmsg) diff --git a/Lib/codecs.py b/Lib/codecs.py index 9b35b6127dd..a887e5d4c94 100644 --- a/Lib/codecs.py +++ b/Lib/codecs.py @@ -1129,4 +1129,4 @@ except LookupError: # package _false = 0 if _false: - import encodings + import encodings # noqa: F401 diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index a17100e6c02..b47e728484c 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -46,7 +46,8 @@ else: _collections_abc.MutableSequence.register(deque) try: - from _collections import _deque_iterator + # Expose _deque_iterator to support pickling deque iterators + from _collections import _deque_iterator # noqa: F401 except ImportError: pass diff --git a/Lib/concurrent/futures/process.py b/Lib/concurrent/futures/process.py index bb4892ebdfe..7092b4757b5 100644 --- a/Lib/concurrent/futures/process.py +++ b/Lib/concurrent/futures/process.py @@ -589,7 +589,7 @@ def _check_system_limits(): raise NotImplementedError(_system_limited) _system_limits_checked = True try: - import multiprocessing.synchronize + import multiprocessing.synchronize # noqa: F401 except ImportError: _system_limited = ( "This Python build lacks multiprocessing.synchronize, usually due " diff --git a/Lib/curses/__init__.py b/Lib/curses/__init__.py index 69270bfcd2b..6165fe6c987 100644 --- a/Lib/curses/__init__.py +++ b/Lib/curses/__init__.py @@ -53,7 +53,7 @@ def start_color(): try: has_key except NameError: - from .has_key import has_key + from .has_key import has_key # noqa: F401 # Wrapper for the entire curses-based application. Runs a function which # should be the rest of your curses-based application. If the application diff --git a/Lib/datetime.py b/Lib/datetime.py index a33d2d724cb..b4f7bd045c7 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: F401 except ImportError: from _pydatetime import * - from _pydatetime import __doc__ + from _pydatetime import __doc__ # noqa: F401 __all__ = ("date", "datetime", "time", "timedelta", "timezone", "tzinfo", "MINYEAR", "MAXYEAR", "UTC") diff --git a/Lib/decimal.py b/Lib/decimal.py index d61e374b9f9..13a0dcb77f1 100644 --- a/Lib/decimal.py +++ b/Lib/decimal.py @@ -100,9 +100,9 @@ NaN try: from _decimal import * - from _decimal import __version__ - from _decimal import __libmpdec_version__ + from _decimal import __version__ # noqa: F401 + from _decimal import __libmpdec_version__ # noqa: F401 except ImportError: from _pydecimal import * - from _pydecimal import __version__ - from _pydecimal import __libmpdec_version__ + from _pydecimal import __version__ # noqa: F401 + from _pydecimal import __libmpdec_version__ # noqa: F401 diff --git a/Lib/hashlib.py b/Lib/hashlib.py index 1b16441cb60..da0577023cf 100644 --- a/Lib/hashlib.py +++ b/Lib/hashlib.py @@ -187,7 +187,7 @@ except ImportError: try: # OpenSSL's scrypt requires OpenSSL 1.1+ - from _hashlib import scrypt + from _hashlib import scrypt # noqa: F401 except ImportError: pass diff --git a/Lib/lzma.py b/Lib/lzma.py index c1e3d33deb6..946066aa0fb 100644 --- a/Lib/lzma.py +++ b/Lib/lzma.py @@ -25,7 +25,7 @@ import builtins 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: F401 import _compression diff --git a/Lib/multiprocessing/context.py b/Lib/multiprocessing/context.py index de8a264829d..ddcc7e79009 100644 --- a/Lib/multiprocessing/context.py +++ b/Lib/multiprocessing/context.py @@ -167,7 +167,7 @@ class BaseContext(object): ''' # This is undocumented. In previous versions of multiprocessing # its only effect was to make socket objects inheritable on Windows. - from . import connection + from . import connection # noqa: F401 def set_executable(self, executable): '''Sets the path to a python.exe or pythonw.exe binary used to run diff --git a/Lib/multiprocessing/util.py b/Lib/multiprocessing/util.py index 75dde02d88c..4f471fbde71 100644 --- a/Lib/multiprocessing/util.py +++ b/Lib/multiprocessing/util.py @@ -14,7 +14,7 @@ import weakref import atexit import threading # we want threading to install it's # cleanup function before multiprocessing does -from subprocess import _args_from_interpreter_flags +from subprocess import _args_from_interpreter_flags # noqa: F401 from . import process diff --git a/Lib/opcode.py b/Lib/opcode.py index 85e37ff53e5..85c0834c698 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -12,8 +12,8 @@ __all__ = ["cmp_op", "stack_effect", "hascompare", "opname", "opmap", 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: F401 + HAVE_ARGUMENT, MIN_INSTRUMENTED_OPCODE) # noqa: F401 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 02ccdaa13dd..6d2a762bc95 100644 --- a/Lib/operator.py +++ b/Lib/operator.py @@ -415,7 +415,7 @@ try: except ImportError: pass else: - from _operator import __doc__ + from _operator import __doc__ # noqa: F401 # 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 a4fd2463f15..d6322c9d99d 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: F401 except ImportError: return release, vendor, vminfo, osinfo diff --git a/Lib/pstats.py b/Lib/pstats.py index 2f054bb4011..a174a545456 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: F401 except ImportError: pass diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 278e4846ebb..be5cd9a80db 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -75,9 +75,12 @@ from collections import deque from reprlib import Repr from traceback import format_exception_only -from _pyrepl.pager import (get_pager, plain, pipe_pager, +from _pyrepl.pager import (get_pager, pipe_pager, plain_pager, tempfile_pager, tty_pager) +# Expose plain() as pydoc.plain() +from _pyrepl.pager import plain # noqa: F401 + # --------------------------------------------------------- old names diff --git a/Lib/re/_constants.py b/Lib/re/_constants.py index 9c3c294ba44..4cb88c96d92 100644 --- a/Lib/re/_constants.py +++ b/Lib/re/_constants.py @@ -15,7 +15,7 @@ MAGIC = 20230612 -from _sre import MAXREPEAT, MAXGROUPS +from _sre import MAXREPEAT, MAXGROUPS # noqa: F401 # SRE standard exception (access as sre.error) # should this really be here? diff --git a/Lib/site.py b/Lib/site.py index 7eace190f5a..9381f6f510e 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -486,7 +486,7 @@ def register_readline(): import atexit try: import readline - import rlcompleter + import rlcompleter # noqa: F401 import _pyrepl.readline import _pyrepl.unix_console except ImportError: @@ -603,7 +603,7 @@ def execsitecustomize(): """Run custom site specific code, if available.""" try: try: - import sitecustomize + import sitecustomize # noqa: F401 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: F401 except ImportError as exc: if exc.name == 'usercustomize': pass diff --git a/Lib/sqlite3/__main__.py b/Lib/sqlite3/__main__.py index b93b84384a0..d9423c25e34 100644 --- a/Lib/sqlite3/__main__.py +++ b/Lib/sqlite3/__main__.py @@ -117,7 +117,7 @@ def main(*args): # No SQL provided; start the REPL. console = SqliteInteractiveConsole(con) try: - import readline + import readline # noqa: F401 except ImportError: pass console.interact(banner, exitmsg="") diff --git a/Lib/struct.py b/Lib/struct.py index d6bba588636..ff98e8c4cb3 100644 --- a/Lib/struct.py +++ b/Lib/struct.py @@ -11,5 +11,5 @@ __all__ = [ ] from _struct import * -from _struct import _clearcache -from _struct import __doc__ +from _struct import _clearcache # noqa: F401 +from _struct import __doc__ # noqa: F401 diff --git a/Lib/symtable.py b/Lib/symtable.py index d6ac1f527ba..f8ba3496439 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_GLOBAL, # noqa: F401 + DEF_NONLOCAL, DEF_LOCAL, + DEF_PARAM, DEF_TYPE_PARAM, DEF_FREE_CLASS, DEF_IMPORT, DEF_BOUND, DEF_ANNOT, DEF_COMP_ITER, DEF_COMP_CELL, SCOPE_OFF, SCOPE_MASK, diff --git a/Lib/unittest/__init__.py b/Lib/unittest/__init__.py index f1f6c911ef1..324e5d038ae 100644 --- a/Lib/unittest/__init__.py +++ b/Lib/unittest/__init__.py @@ -57,9 +57,9 @@ from .result import TestResult from .case import (addModuleCleanup, TestCase, FunctionTestCase, SkipTest, skip, skipIf, skipUnless, expectedFailure, doModuleCleanups, enterModuleContext) -from .suite import BaseTestSuite, TestSuite +from .suite import BaseTestSuite, TestSuite # noqa: F401 from .loader import TestLoader, defaultTestLoader -from .main import TestProgram, main +from .main import TestProgram, main # noqa: F401 from .runner import TextTestRunner, TextTestResult from .signals import installHandler, registerResult, removeResult, removeHandler # IsolatedAsyncioTestCase will be imported lazily. diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index ac6719ce854..58b0cb574a7 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -108,7 +108,7 @@ from urllib.response import addinfourl, addclosehook # check for SSL try: - import ssl + import ssl # noqa: F401 except ImportError: _have_ssl = False else: diff --git a/Lib/xml/dom/__init__.py b/Lib/xml/dom/__init__.py index 97cf9a64299..dd7fb996afd 100644 --- a/Lib/xml/dom/__init__.py +++ b/Lib/xml/dom/__init__.py @@ -137,4 +137,4 @@ XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml" EMPTY_NAMESPACE = None EMPTY_PREFIX = None -from .domreg import getDOMImplementation, registerDOMImplementation +from .domreg import getDOMImplementation, registerDOMImplementation # noqa: F401