Skip to content

Commit 05df063

Browse files
authored
gh-120417: Fix "imported but unused" linter warnings (#120461)
Add __all__ to the following modules: importlib.machinery, importlib.util and xml.sax. Add also "# noqa: F401" in collections.abc, subprocess and xml.sax. * Sort __all__; remove collections.abc.__all__; remove private names * Add tests
1 parent ed60ab5 commit 05df063

File tree

7 files changed

+84
-8
lines changed

7 files changed

+84
-8
lines changed

Lib/collections/abc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from _collections_abc import *
2-
from _collections_abc import __all__
3-
from _collections_abc import _CallableGenericAlias
2+
from _collections_abc import __all__ # noqa: F401
3+
from _collections_abc import _CallableGenericAlias # noqa: F401

Lib/importlib/machinery.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@
1919
def all_suffixes():
2020
"""Returns a list of all recognized module suffixes for this process"""
2121
return SOURCE_SUFFIXES + BYTECODE_SUFFIXES + EXTENSION_SUFFIXES
22+
23+
24+
__all__ = ['AppleFrameworkLoader', 'BYTECODE_SUFFIXES', 'BuiltinImporter',
25+
'DEBUG_BYTECODE_SUFFIXES', 'EXTENSION_SUFFIXES',
26+
'ExtensionFileLoader', 'FileFinder', 'FrozenImporter', 'ModuleSpec',
27+
'NamespaceLoader', 'OPTIMIZED_BYTECODE_SUFFIXES', 'PathFinder',
28+
'SOURCE_SUFFIXES', 'SourceFileLoader', 'SourcelessFileLoader',
29+
'WindowsRegistryFinder', 'all_suffixes']

Lib/importlib/util.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,9 @@ def exec_module(self, module):
270270
loader_state['is_loading'] = False
271271
module.__spec__.loader_state = loader_state
272272
module.__class__ = _LazyModule
273+
274+
275+
__all__ = ['LazyLoader', 'Loader', 'MAGIC_NUMBER',
276+
'cache_from_source', 'decode_source', 'find_spec',
277+
'module_from_spec', 'resolve_name', 'source_from_cache',
278+
'source_hash', 'spec_from_file_location', 'spec_from_loader']

Lib/subprocess.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979

8080
if _mswindows:
8181
import _winapi
82-
from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP,
82+
from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP, # noqa: F401
8383
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
8484
STD_ERROR_HANDLE, SW_HIDE,
8585
STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW,

Lib/test/test_importlib/test_api.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import os.path
88
import sys
9+
from test import support
910
from test.support import import_helper
1011
from test.support import os_helper
1112
import types
@@ -437,5 +438,44 @@ def test_everyone_has___spec__(self):
437438
) = test_util.test_both(StartupTests, machinery=machinery)
438439

439440

441+
class TestModuleAll(unittest.TestCase):
442+
def test_machinery(self):
443+
extra = (
444+
# from importlib._bootstrap and importlib._bootstrap_external
445+
'AppleFrameworkLoader',
446+
'BYTECODE_SUFFIXES',
447+
'BuiltinImporter',
448+
'DEBUG_BYTECODE_SUFFIXES',
449+
'EXTENSION_SUFFIXES',
450+
'ExtensionFileLoader',
451+
'FileFinder',
452+
'FrozenImporter',
453+
'ModuleSpec',
454+
'NamespaceLoader',
455+
'OPTIMIZED_BYTECODE_SUFFIXES',
456+
'PathFinder',
457+
'SOURCE_SUFFIXES',
458+
'SourceFileLoader',
459+
'SourcelessFileLoader',
460+
'WindowsRegistryFinder',
461+
)
462+
support.check__all__(self, machinery['Source'], extra=extra)
463+
464+
def test_util(self):
465+
extra = (
466+
# from importlib.abc, importlib._bootstrap
467+
# and importlib._bootstrap_external
468+
'Loader',
469+
'MAGIC_NUMBER',
470+
'cache_from_source',
471+
'decode_source',
472+
'module_from_spec',
473+
'source_from_cache',
474+
'spec_from_file_location',
475+
'spec_from_loader',
476+
)
477+
support.check__all__(self, util['Source'], extra=extra)
478+
479+
440480
if __name__ == '__main__':
441481
unittest.main()

Lib/test/test_sax.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from xml.sax.handler import (feature_namespaces, feature_external_ges,
1717
LexicalHandler)
1818
from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
19+
from xml import sax
1920
from io import BytesIO, StringIO
2021
import codecs
2122
import os.path
@@ -25,7 +26,7 @@
2526
from urllib.error import URLError
2627
import urllib.request
2728
from test.support import os_helper
28-
from test.support import findfile
29+
from test.support import findfile, check__all__
2930
from test.support.os_helper import FakePath, TESTFN
3031

3132

@@ -1557,5 +1558,20 @@ def characters(self, content):
15571558
self.assertEqual(self.char_index, 2)
15581559

15591560

1561+
class TestModuleAll(unittest.TestCase):
1562+
def test_all(self):
1563+
extra = (
1564+
'ContentHandler',
1565+
'ErrorHandler',
1566+
'InputSource',
1567+
'SAXException',
1568+
'SAXNotRecognizedException',
1569+
'SAXNotSupportedException',
1570+
'SAXParseException',
1571+
'SAXReaderNotAvailable',
1572+
)
1573+
check__all__(self, sax, extra=extra)
1574+
1575+
15601576
if __name__ == "__main__":
15611577
unittest.main()

Lib/xml/sax/__init__.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

2222
from .xmlreader import InputSource
2323
from .handler import ContentHandler, ErrorHandler
24-
from ._exceptions import SAXException, SAXNotRecognizedException, \
25-
SAXParseException, SAXNotSupportedException, \
26-
SAXReaderNotAvailable
24+
from ._exceptions import (SAXException, SAXNotRecognizedException,
25+
SAXParseException, SAXNotSupportedException,
26+
SAXReaderNotAvailable)
2727

2828

2929
def parse(source, handler, errorHandler=ErrorHandler()):
@@ -55,7 +55,7 @@ def parseString(string, handler, errorHandler=ErrorHandler()):
5555
# tell modulefinder that importing sax potentially imports expatreader
5656
_false = 0
5757
if _false:
58-
import xml.sax.expatreader
58+
import xml.sax.expatreader # noqa: F401
5959

6060
import os, sys
6161
if not sys.flags.ignore_environment and "PY_SAX_PARSER" in os.environ:
@@ -92,3 +92,9 @@ def make_parser(parser_list=()):
9292
def _create_parser(parser_name):
9393
drv_module = __import__(parser_name,{},{},['create_parser'])
9494
return drv_module.create_parser()
95+
96+
97+
__all__ = ['ContentHandler', 'ErrorHandler', 'InputSource', 'SAXException',
98+
'SAXNotRecognizedException', 'SAXNotSupportedException',
99+
'SAXParseException', 'SAXReaderNotAvailable',
100+
'default_parser_list', 'make_parser', 'parse', 'parseString']

0 commit comments

Comments
 (0)