mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	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
This commit is contained in:
		
							parent
							
								
									ed60ab5fab
								
							
						
					
					
						commit
						05df063ad8
					
				
					 7 changed files with 84 additions and 8 deletions
				
			
		|  | @ -1,3 +1,3 @@ | ||||||
| from _collections_abc import * | from _collections_abc import * | ||||||
| from _collections_abc import __all__ | from _collections_abc import __all__  # noqa: F401 | ||||||
| from _collections_abc import _CallableGenericAlias | from _collections_abc import _CallableGenericAlias  # noqa: F401 | ||||||
|  |  | ||||||
|  | @ -19,3 +19,11 @@ | ||||||
| def all_suffixes(): | def all_suffixes(): | ||||||
|     """Returns a list of all recognized module suffixes for this process""" |     """Returns a list of all recognized module suffixes for this process""" | ||||||
|     return SOURCE_SUFFIXES + BYTECODE_SUFFIXES + EXTENSION_SUFFIXES |     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'] | ||||||
|  |  | ||||||
|  | @ -270,3 +270,9 @@ def exec_module(self, module): | ||||||
|         loader_state['is_loading'] = False |         loader_state['is_loading'] = False | ||||||
|         module.__spec__.loader_state = loader_state |         module.__spec__.loader_state = loader_state | ||||||
|         module.__class__ = _LazyModule |         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'] | ||||||
|  |  | ||||||
|  | @ -79,7 +79,7 @@ | ||||||
| 
 | 
 | ||||||
| if _mswindows: | if _mswindows: | ||||||
|     import _winapi |     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_INPUT_HANDLE, STD_OUTPUT_HANDLE, | ||||||
|                          STD_ERROR_HANDLE, SW_HIDE, |                          STD_ERROR_HANDLE, SW_HIDE, | ||||||
|                          STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW, |                          STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW, | ||||||
|  |  | ||||||
|  | @ -6,6 +6,7 @@ | ||||||
| 
 | 
 | ||||||
| import os.path | import os.path | ||||||
| import sys | import sys | ||||||
|  | from test import support | ||||||
| from test.support import import_helper | from test.support import import_helper | ||||||
| from test.support import os_helper | from test.support import os_helper | ||||||
| import types | import types | ||||||
|  | @ -437,5 +438,44 @@ def test_everyone_has___spec__(self): | ||||||
|  ) = test_util.test_both(StartupTests, machinery=machinery) |  ) = 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__': | if __name__ == '__main__': | ||||||
|     unittest.main() |     unittest.main() | ||||||
|  |  | ||||||
|  | @ -16,6 +16,7 @@ | ||||||
| from xml.sax.handler import (feature_namespaces, feature_external_ges, | from xml.sax.handler import (feature_namespaces, feature_external_ges, | ||||||
|                              LexicalHandler) |                              LexicalHandler) | ||||||
| from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl | from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl | ||||||
|  | from xml import sax | ||||||
| from io import BytesIO, StringIO | from io import BytesIO, StringIO | ||||||
| import codecs | import codecs | ||||||
| import os.path | import os.path | ||||||
|  | @ -25,7 +26,7 @@ | ||||||
| from urllib.error import URLError | from urllib.error import URLError | ||||||
| import urllib.request | import urllib.request | ||||||
| from test.support import os_helper | 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 | from test.support.os_helper import FakePath, TESTFN | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -1557,5 +1558,20 @@ def characters(self, content): | ||||||
|         self.assertEqual(self.char_index, 2) |         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__": | if __name__ == "__main__": | ||||||
|     unittest.main() |     unittest.main() | ||||||
|  |  | ||||||
|  | @ -21,9 +21,9 @@ | ||||||
| 
 | 
 | ||||||
| from .xmlreader import InputSource | from .xmlreader import InputSource | ||||||
| from .handler import ContentHandler, ErrorHandler | from .handler import ContentHandler, ErrorHandler | ||||||
| from ._exceptions import SAXException, SAXNotRecognizedException, \ | from ._exceptions import (SAXException, SAXNotRecognizedException, | ||||||
|                         SAXParseException, SAXNotSupportedException, \ |                           SAXParseException, SAXNotSupportedException, | ||||||
|                         SAXReaderNotAvailable |                           SAXReaderNotAvailable) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def parse(source, handler, errorHandler=ErrorHandler()): | def parse(source, handler, errorHandler=ErrorHandler()): | ||||||
|  | @ -55,7 +55,7 @@ def parseString(string, handler, errorHandler=ErrorHandler()): | ||||||
| # tell modulefinder that importing sax potentially imports expatreader | # tell modulefinder that importing sax potentially imports expatreader | ||||||
| _false = 0 | _false = 0 | ||||||
| if _false: | if _false: | ||||||
|     import xml.sax.expatreader |     import xml.sax.expatreader    # noqa: F401 | ||||||
| 
 | 
 | ||||||
| import os, sys | import os, sys | ||||||
| if not sys.flags.ignore_environment and "PY_SAX_PARSER" in os.environ: | 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): | def _create_parser(parser_name): | ||||||
|     drv_module = __import__(parser_name,{},{},['create_parser']) |     drv_module = __import__(parser_name,{},{},['create_parser']) | ||||||
|     return drv_module.create_parser() |     return drv_module.create_parser() | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | __all__ = ['ContentHandler', 'ErrorHandler', 'InputSource', 'SAXException', | ||||||
|  |            'SAXNotRecognizedException', 'SAXNotSupportedException', | ||||||
|  |            'SAXParseException', 'SAXReaderNotAvailable', | ||||||
|  |            'default_parser_list', 'make_parser', 'parse', 'parseString'] | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Victor Stinner
						Victor Stinner