https://github.com/python/cpython/commit/05df063ad80becc1ba6bd07d67b55b5965f32375
commit: 05df063ad80becc1ba6bd07d67b55b5965f32375
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2024-06-14T20:39:50+02:00
summary:

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

files:
M Lib/collections/abc.py
M Lib/importlib/machinery.py
M Lib/importlib/util.py
M Lib/subprocess.py
M Lib/test/test_importlib/test_api.py
M Lib/test/test_sax.py
M Lib/xml/sax/__init__.py

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/importlib/machinery.py b/Lib/importlib/machinery.py
index fbd30b159fb752..6e294d59bfdcb9 100644
--- a/Lib/importlib/machinery.py
+++ b/Lib/importlib/machinery.py
@@ -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']
diff --git a/Lib/importlib/util.py b/Lib/importlib/util.py
index c94a148e4c50e0..7243d052cc27f3 100644
--- a/Lib/importlib/util.py
+++ b/Lib/importlib/util.py
@@ -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']
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/test/test_importlib/test_api.py 
b/Lib/test/test_importlib/test_api.py
index 2a35f3dcb7210c..973237c0791a3e 100644
--- a/Lib/test/test_importlib/test_api.py
+++ b/Lib/test/test_importlib/test_api.py
@@ -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
@@ -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()
diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py
index 9b3014a94a081e..0d0f86c145b499 100644
--- a/Lib/test/test_sax.py
+++ b/Lib/test/test_sax.py
@@ -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
@@ -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
 
 
@@ -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()
diff --git a/Lib/xml/sax/__init__.py b/Lib/xml/sax/__init__.py
index b657310207cfe5..fe4582c6f8b758 100644
--- a/Lib/xml/sax/__init__.py
+++ b/Lib/xml/sax/__init__.py
@@ -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()):
@@ -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:
@@ -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']

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to