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.
This commit is contained in:
Victor Stinner 2024-06-13 16:14:50 +02:00 committed by GitHub
parent ca5108a46d
commit 6ae254aaa0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
25 changed files with 40 additions and 36 deletions

View File

@ -16,7 +16,7 @@ else:
_setmode = None _setmode = None
import io 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 valid_seek_flags = {0, 1, 2} # Hardwired values
if hasattr(os, 'SEEK_HOLE') : if hasattr(os, 'SEEK_HOLE') :

View File

@ -355,7 +355,7 @@ def interact(banner=None, readfunc=None, local=None, exitmsg=None, local_exit=Fa
console.raw_input = readfunc console.raw_input = readfunc
else: else:
try: try:
import readline import readline # noqa: F401
except ImportError: except ImportError:
pass pass
console.interact(banner, exitmsg) console.interact(banner, exitmsg)

View File

@ -1129,4 +1129,4 @@ except LookupError:
# package # package
_false = 0 _false = 0
if _false: if _false:
import encodings import encodings # noqa: F401

View File

@ -46,7 +46,8 @@ else:
_collections_abc.MutableSequence.register(deque) _collections_abc.MutableSequence.register(deque)
try: try:
from _collections import _deque_iterator # Expose _deque_iterator to support pickling deque iterators
from _collections import _deque_iterator # noqa: F401
except ImportError: except ImportError:
pass pass

View File

@ -589,7 +589,7 @@ def _check_system_limits():
raise NotImplementedError(_system_limited) raise NotImplementedError(_system_limited)
_system_limits_checked = True _system_limits_checked = True
try: try:
import multiprocessing.synchronize import multiprocessing.synchronize # noqa: F401
except ImportError: except ImportError:
_system_limited = ( _system_limited = (
"This Python build lacks multiprocessing.synchronize, usually due " "This Python build lacks multiprocessing.synchronize, usually due "

View File

@ -53,7 +53,7 @@ def start_color():
try: try:
has_key has_key
except NameError: 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 # Wrapper for the entire curses-based application. Runs a function which
# should be the rest of your curses-based application. If the application # should be the rest of your curses-based application. If the application

View File

@ -1,9 +1,9 @@
try: try:
from _datetime import * from _datetime import *
from _datetime import __doc__ from _datetime import __doc__ # noqa: F401
except ImportError: except ImportError:
from _pydatetime import * from _pydatetime import *
from _pydatetime import __doc__ from _pydatetime import __doc__ # noqa: F401
__all__ = ("date", "datetime", "time", "timedelta", "timezone", "tzinfo", __all__ = ("date", "datetime", "time", "timedelta", "timezone", "tzinfo",
"MINYEAR", "MAXYEAR", "UTC") "MINYEAR", "MAXYEAR", "UTC")

View File

@ -100,9 +100,9 @@ NaN
try: try:
from _decimal import * from _decimal import *
from _decimal import __version__ from _decimal import __version__ # noqa: F401
from _decimal import __libmpdec_version__ from _decimal import __libmpdec_version__ # noqa: F401
except ImportError: except ImportError:
from _pydecimal import * from _pydecimal import *
from _pydecimal import __version__ from _pydecimal import __version__ # noqa: F401
from _pydecimal import __libmpdec_version__ from _pydecimal import __libmpdec_version__ # noqa: F401

View File

@ -187,7 +187,7 @@ except ImportError:
try: try:
# OpenSSL's scrypt requires OpenSSL 1.1+ # OpenSSL's scrypt requires OpenSSL 1.1+
from _hashlib import scrypt from _hashlib import scrypt # noqa: F401
except ImportError: except ImportError:
pass pass

View File

@ -25,7 +25,7 @@ import builtins
import io import io
import os import os
from _lzma import * 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 import _compression

View File

@ -167,7 +167,7 @@ class BaseContext(object):
''' '''
# This is undocumented. In previous versions of multiprocessing # This is undocumented. In previous versions of multiprocessing
# its only effect was to make socket objects inheritable on Windows. # its only effect was to make socket objects inheritable on Windows.
from . import connection from . import connection # noqa: F401
def set_executable(self, executable): def set_executable(self, executable):
'''Sets the path to a python.exe or pythonw.exe binary used to run '''Sets the path to a python.exe or pythonw.exe binary used to run

View File

@ -14,7 +14,7 @@ import weakref
import atexit import atexit
import threading # we want threading to install it's import threading # we want threading to install it's
# cleanup function before multiprocessing does # cleanup function before multiprocessing does
from subprocess import _args_from_interpreter_flags from subprocess import _args_from_interpreter_flags # noqa: F401
from . import process from . import process

View File

@ -12,8 +12,8 @@ __all__ = ["cmp_op", "stack_effect", "hascompare", "opname", "opmap",
import _opcode import _opcode
from _opcode import stack_effect from _opcode import stack_effect
from _opcode_metadata import (_specializations, _specialized_opmap, opmap, from _opcode_metadata import (_specializations, _specialized_opmap, opmap, # noqa: F401
HAVE_ARGUMENT, MIN_INSTRUMENTED_OPCODE) HAVE_ARGUMENT, MIN_INSTRUMENTED_OPCODE) # noqa: F401
EXTENDED_ARG = opmap['EXTENDED_ARG'] EXTENDED_ARG = opmap['EXTENDED_ARG']
opname = ['<%r>' % (op,) for op in range(max(opmap.values()) + 1)] opname = ['<%r>' % (op,) for op in range(max(opmap.values()) + 1)]

View File

@ -415,7 +415,7 @@ try:
except ImportError: except ImportError:
pass pass
else: else:
from _operator import __doc__ from _operator import __doc__ # noqa: F401
# All of these "__func__ = func" assignments have to happen after importing # All of these "__func__ = func" assignments have to happen after importing
# from _operator to make sure they're set to the right function # from _operator to make sure they're set to the right function

View File

@ -546,7 +546,7 @@ def java_ver(release='', vendor='', vminfo=('', '', ''), osinfo=('', '', '')):
warnings._deprecated('java_ver', remove=(3, 15)) warnings._deprecated('java_ver', remove=(3, 15))
# Import the needed APIs # Import the needed APIs
try: try:
import java.lang import java.lang # noqa: F401
except ImportError: except ImportError:
return release, vendor, vminfo, osinfo return release, vendor, vminfo, osinfo

View File

@ -611,7 +611,7 @@ def f8(x):
if __name__ == '__main__': if __name__ == '__main__':
import cmd import cmd
try: try:
import readline import readline # noqa: F401
except ImportError: except ImportError:
pass pass

View File

@ -75,9 +75,12 @@ from collections import deque
from reprlib import Repr from reprlib import Repr
from traceback import format_exception_only 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) plain_pager, tempfile_pager, tty_pager)
# Expose plain() as pydoc.plain()
from _pyrepl.pager import plain # noqa: F401
# --------------------------------------------------------- old names # --------------------------------------------------------- old names

View File

@ -15,7 +15,7 @@
MAGIC = 20230612 MAGIC = 20230612
from _sre import MAXREPEAT, MAXGROUPS from _sre import MAXREPEAT, MAXGROUPS # noqa: F401
# SRE standard exception (access as sre.error) # SRE standard exception (access as sre.error)
# should this really be here? # should this really be here?

View File

@ -486,7 +486,7 @@ def register_readline():
import atexit import atexit
try: try:
import readline import readline
import rlcompleter import rlcompleter # noqa: F401
import _pyrepl.readline import _pyrepl.readline
import _pyrepl.unix_console import _pyrepl.unix_console
except ImportError: except ImportError:
@ -603,7 +603,7 @@ def execsitecustomize():
"""Run custom site specific code, if available.""" """Run custom site specific code, if available."""
try: try:
try: try:
import sitecustomize import sitecustomize # noqa: F401
except ImportError as exc: except ImportError as exc:
if exc.name == 'sitecustomize': if exc.name == 'sitecustomize':
pass pass
@ -623,7 +623,7 @@ def execusercustomize():
"""Run custom user specific code, if available.""" """Run custom user specific code, if available."""
try: try:
try: try:
import usercustomize import usercustomize # noqa: F401
except ImportError as exc: except ImportError as exc:
if exc.name == 'usercustomize': if exc.name == 'usercustomize':
pass pass

View File

@ -117,7 +117,7 @@ def main(*args):
# No SQL provided; start the REPL. # No SQL provided; start the REPL.
console = SqliteInteractiveConsole(con) console = SqliteInteractiveConsole(con)
try: try:
import readline import readline # noqa: F401
except ImportError: except ImportError:
pass pass
console.interact(banner, exitmsg="") console.interact(banner, exitmsg="")

View File

@ -11,5 +11,5 @@ __all__ = [
] ]
from _struct import * from _struct import *
from _struct import _clearcache from _struct import _clearcache # noqa: F401
from _struct import __doc__ from _struct import __doc__ # noqa: F401

View File

@ -3,9 +3,9 @@
import _symtable import _symtable
from _symtable import ( from _symtable import (
USE, USE,
DEF_GLOBAL, DEF_NONLOCAL, DEF_LOCAL, DEF_GLOBAL, # noqa: F401
DEF_PARAM, DEF_TYPE_PARAM, DEF_NONLOCAL, DEF_LOCAL,
DEF_FREE_CLASS, DEF_PARAM, DEF_TYPE_PARAM, DEF_FREE_CLASS,
DEF_IMPORT, DEF_BOUND, DEF_ANNOT, DEF_IMPORT, DEF_BOUND, DEF_ANNOT,
DEF_COMP_ITER, DEF_COMP_CELL, DEF_COMP_ITER, DEF_COMP_CELL,
SCOPE_OFF, SCOPE_MASK, SCOPE_OFF, SCOPE_MASK,

View File

@ -57,9 +57,9 @@ from .result import TestResult
from .case import (addModuleCleanup, TestCase, FunctionTestCase, SkipTest, skip, from .case import (addModuleCleanup, TestCase, FunctionTestCase, SkipTest, skip,
skipIf, skipUnless, expectedFailure, doModuleCleanups, skipIf, skipUnless, expectedFailure, doModuleCleanups,
enterModuleContext) enterModuleContext)
from .suite import BaseTestSuite, TestSuite from .suite import BaseTestSuite, TestSuite # noqa: F401
from .loader import TestLoader, defaultTestLoader from .loader import TestLoader, defaultTestLoader
from .main import TestProgram, main from .main import TestProgram, main # noqa: F401
from .runner import TextTestRunner, TextTestResult from .runner import TextTestRunner, TextTestResult
from .signals import installHandler, registerResult, removeResult, removeHandler from .signals import installHandler, registerResult, removeResult, removeHandler
# IsolatedAsyncioTestCase will be imported lazily. # IsolatedAsyncioTestCase will be imported lazily.

View File

@ -108,7 +108,7 @@ from urllib.response import addinfourl, addclosehook
# check for SSL # check for SSL
try: try:
import ssl import ssl # noqa: F401
except ImportError: except ImportError:
_have_ssl = False _have_ssl = False
else: else:

View File

@ -137,4 +137,4 @@ XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml"
EMPTY_NAMESPACE = None EMPTY_NAMESPACE = None
EMPTY_PREFIX = None EMPTY_PREFIX = None
from .domreg import getDOMImplementation, registerDOMImplementation from .domreg import getDOMImplementation, registerDOMImplementation # noqa: F401