Skip to content

Commit

Permalink
pythongh-120417: Fix "imported but unused" linter warnings (python#12…
Browse files Browse the repository at this point in the history
…0461)

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
  • Loading branch information
vstinner authored and mrahtz committed Jun 30, 2024
1 parent 1eee4a9 commit 4c42c6d
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Lib/collections/abc.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions Lib/importlib/machinery.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@
def all_suffixes():
"""Returns a list of all recognized module suffixes for this process"""
return SOURCE_SUFFIXES + BYTECODE_SUFFIXES + EXTENSION_SUFFIXES


__all__ = ['AppleFrameworkLoader', 'BYTECODE_SUFFIXES', 'BuiltinImporter',
'DEBUG_BYTECODE_SUFFIXES', 'EXTENSION_SUFFIXES',
'ExtensionFileLoader', 'FileFinder', 'FrozenImporter', 'ModuleSpec',
'NamespaceLoader', 'OPTIMIZED_BYTECODE_SUFFIXES', 'PathFinder',
'SOURCE_SUFFIXES', 'SourceFileLoader', 'SourcelessFileLoader',
'WindowsRegistryFinder', 'all_suffixes']
6 changes: 6 additions & 0 deletions Lib/importlib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,9 @@ def exec_module(self, module):
loader_state['is_loading'] = False
module.__spec__.loader_state = loader_state
module.__class__ = _LazyModule


__all__ = ['LazyLoader', 'Loader', 'MAGIC_NUMBER',
'cache_from_source', 'decode_source', 'find_spec',
'module_from_spec', 'resolve_name', 'source_from_cache',
'source_hash', 'spec_from_file_location', 'spec_from_loader']
2 changes: 1 addition & 1 deletion Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
40 changes: 40 additions & 0 deletions Lib/test/test_importlib/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import os.path
import sys
from test import support
from test.support import import_helper
from test.support import os_helper
import types
Expand Down Expand Up @@ -437,5 +438,44 @@ def test_everyone_has___spec__(self):
) = test_util.test_both(StartupTests, machinery=machinery)


class TestModuleAll(unittest.TestCase):
def test_machinery(self):
extra = (
# from importlib._bootstrap and importlib._bootstrap_external
'AppleFrameworkLoader',
'BYTECODE_SUFFIXES',
'BuiltinImporter',
'DEBUG_BYTECODE_SUFFIXES',
'EXTENSION_SUFFIXES',
'ExtensionFileLoader',
'FileFinder',
'FrozenImporter',
'ModuleSpec',
'NamespaceLoader',
'OPTIMIZED_BYTECODE_SUFFIXES',
'PathFinder',
'SOURCE_SUFFIXES',
'SourceFileLoader',
'SourcelessFileLoader',
'WindowsRegistryFinder',
)
support.check__all__(self, machinery['Source'], extra=extra)

def test_util(self):
extra = (
# from importlib.abc, importlib._bootstrap
# and importlib._bootstrap_external
'Loader',
'MAGIC_NUMBER',
'cache_from_source',
'decode_source',
'module_from_spec',
'source_from_cache',
'spec_from_file_location',
'spec_from_loader',
)
support.check__all__(self, util['Source'], extra=extra)


if __name__ == '__main__':
unittest.main()
18 changes: 17 additions & 1 deletion Lib/test/test_sax.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from xml.sax.handler import (feature_namespaces, feature_external_ges,
LexicalHandler)
from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
from xml import sax
from io import BytesIO, StringIO
import codecs
import os.path
Expand All @@ -25,7 +26,7 @@
from urllib.error import URLError
import urllib.request
from test.support import os_helper
from test.support import findfile
from test.support import findfile, check__all__
from test.support.os_helper import FakePath, TESTFN


Expand Down Expand Up @@ -1557,5 +1558,20 @@ def characters(self, content):
self.assertEqual(self.char_index, 2)


class TestModuleAll(unittest.TestCase):
def test_all(self):
extra = (
'ContentHandler',
'ErrorHandler',
'InputSource',
'SAXException',
'SAXNotRecognizedException',
'SAXNotSupportedException',
'SAXParseException',
'SAXReaderNotAvailable',
)
check__all__(self, sax, extra=extra)


if __name__ == "__main__":
unittest.main()
14 changes: 10 additions & 4 deletions Lib/xml/sax/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

from .xmlreader import InputSource
from .handler import ContentHandler, ErrorHandler
from ._exceptions import SAXException, SAXNotRecognizedException, \
SAXParseException, SAXNotSupportedException, \
SAXReaderNotAvailable
from ._exceptions import (SAXException, SAXNotRecognizedException,
SAXParseException, SAXNotSupportedException,
SAXReaderNotAvailable)


def parse(source, handler, errorHandler=ErrorHandler()):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -92,3 +92,9 @@ def make_parser(parser_list=()):
def _create_parser(parser_name):
drv_module = __import__(parser_name,{},{},['create_parser'])
return drv_module.create_parser()


__all__ = ['ContentHandler', 'ErrorHandler', 'InputSource', 'SAXException',
'SAXNotRecognizedException', 'SAXNotSupportedException',
'SAXParseException', 'SAXReaderNotAvailable',
'default_parser_list', 'make_parser', 'parse', 'parseString']

0 comments on commit 4c42c6d

Please sign in to comment.