2424from _metadata import PackageDependencies , get_recursive_requirements , read_metadata
2525from _utils import (
2626 PYTHON_VERSION ,
27+ STDLIB_PATH ,
2728 TESTS_DIR ,
29+ SupportedVersionsDict ,
30+ VersionTuple ,
2831 colored ,
2932 get_gitignore_spec ,
3033 get_mypy_req ,
@@ -332,15 +335,12 @@ def test_third_party_distribution(
332335
333336def test_stdlib (args : TestConfig ) -> TestResult :
334337 files : list [Path ] = []
335- stdlib = Path ("stdlib" )
336- supported_versions = parse_stdlib_versions_file ()
337- for name in os .listdir (stdlib ):
338- if name in ("VERSIONS" , TESTS_DIR ) or name .startswith ("." ):
338+ for file in STDLIB_PATH .iterdir ():
339+ if file .name in ("VERSIONS" , TESTS_DIR ) or file .name .startswith ("." ):
339340 continue
340- module = Path (name ).stem
341- module_min_version , module_max_version = supported_versions [module ]
342- if module_min_version <= tuple (map (int , args .version .split ("." ))) <= module_max_version :
343- add_files (files , (stdlib / name ), args )
341+ add_files (files , file , args )
342+
343+ files = remove_modules_not_in_python_version (files , args .version )
344344
345345 if not files :
346346 return TestResult (MypyResult .SUCCESS , 0 )
@@ -351,6 +351,38 @@ def test_stdlib(args: TestConfig) -> TestResult:
351351 return TestResult (result , len (files ))
352352
353353
354+ def remove_modules_not_in_python_version (paths : list [Path ], py_version : VersionString ) -> list [Path ]:
355+ py_version_tuple = tuple (map (int , py_version .split ("." )))
356+ module_versions = parse_stdlib_versions_file ()
357+ new_paths : list [Path ] = []
358+ for path in paths :
359+ if path .parts [0 ] != "stdlib" or path .suffix != ".pyi" :
360+ continue
361+ module_name = stdlib_module_name_from_path (path )
362+ min_version , max_version = supported_versions_for_module (module_versions , module_name )
363+ if min_version <= py_version_tuple <= max_version :
364+ new_paths .append (path )
365+ return new_paths
366+
367+
368+ def stdlib_module_name_from_path (path : Path ) -> str :
369+ assert path .parts [0 ] == "stdlib"
370+ assert path .suffix == ".pyi"
371+ parts = list (path .parts [1 :- 1 ])
372+ if path .parts [- 1 ] != "__init__.pyi" :
373+ # TODO: Python 3.9+: Use removesuffix.
374+ parts .append (path .parts [- 1 ][:- 4 ])
375+ return "." .join (parts )
376+
377+
378+ def supported_versions_for_module (module_versions : SupportedVersionsDict , module_name : str ) -> tuple [VersionTuple , VersionTuple ]:
379+ while "." in module_name :
380+ if module_name in module_versions :
381+ return module_versions [module_name ]
382+ module_name = "." .join (module_name .split ("." )[:- 1 ])
383+ return module_versions [module_name ]
384+
385+
354386@dataclass
355387class TestSummary :
356388 mypy_result : MypyResult = MypyResult .SUCCESS
0 commit comments