@@ -657,6 +657,33 @@ def __init__(
657657 self .find_module_cache = FindModuleCache (
658658 self .search_paths , self .fscache , self .options , source_set = self .source_set
659659 )
660+ for module in CORE_BUILTIN_MODULES :
661+ if options .use_builtins_fixtures :
662+ continue
663+ if module == "_importlib_modulespec" :
664+ continue
665+ path = self .find_module_cache .find_module (module )
666+ if not isinstance (path , str ):
667+ raise CompileError (
668+ [f"Failed to find builtin module { module } , perhaps typeshed is broken?" ]
669+ )
670+ if is_typeshed_file (path ):
671+ continue
672+ if is_stub_package_file (path ):
673+ continue
674+ if options .custom_typeshed_dir is not None :
675+ # Check if module lives under custom_typeshed_dir subtree
676+ custom_typeshed_dir = os .path .abspath (options .custom_typeshed_dir )
677+ if os .path .commonpath ((path , custom_typeshed_dir )) == custom_typeshed_dir :
678+ continue
679+
680+ raise CompileError (
681+ [
682+ f'mypy: "{ os .path .relpath (path )} " shadows library module "{ module } "' ,
683+ f'note: A user-defined top-level module with name "{ module } " is not supported' ,
684+ ]
685+ )
686+
660687 self .metastore = create_metastore (options )
661688
662689 # a mapping from source files to their corresponding shadow files
@@ -2583,19 +2610,6 @@ def find_module_and_diagnose(
25832610 if is_sub_path (result , dir ):
25842611 # Silence errors in site-package dirs and typeshed
25852612 follow_imports = "silent"
2586- if (
2587- id in CORE_BUILTIN_MODULES
2588- and not is_typeshed_file (result )
2589- and not is_stub_package_file (result )
2590- and not options .use_builtins_fixtures
2591- and not options .custom_typeshed_dir
2592- ):
2593- raise CompileError (
2594- [
2595- f'mypy: "{ os .path .relpath (result )} " shadows library module "{ id } "' ,
2596- f'note: A user-defined top-level module with name "{ id } " is not supported' ,
2597- ]
2598- )
25992613 return (result , follow_imports )
26002614 else :
26012615 # Could not find a module. Typically the reason is a
0 commit comments