@@ -605,6 +605,31 @@ def __init__(self, data_dir: str,
605605 self .fscache = fscache
606606 self .find_module_cache = FindModuleCache (self .search_paths , self .fscache , self .options ,
607607 source_set = self .source_set )
608+ for module in CORE_BUILTIN_MODULES :
609+ if options .use_builtins_fixtures :
610+ continue
611+ if module == "_importlib_modulespec" :
612+ continue
613+ path = self .find_module_cache .find_module (module )
614+ if not isinstance (path , str ):
615+ raise CompileError ([
616+ f"Failed to find builtin module { module } , perhaps typeshed is broken?" ,
617+ ])
618+ if is_typeshed_file (path ):
619+ continue
620+ if is_stub_package_file (path ):
621+ continue
622+ if options .custom_typeshed_dir is not None :
623+ # Check if module lives under custom_typeshed_dir subtree
624+ custom_typeshed_dir = os .path .abspath (options .custom_typeshed_dir )
625+ if os .path .commonpath ((path , custom_typeshed_dir )) == custom_typeshed_dir :
626+ continue
627+
628+ raise CompileError ([
629+ f'mypy: "{ os .path .relpath (path )} " shadows library module "{ module } "' ,
630+ f'note: A user-defined top-level module with name "{ module } " is not supported'
631+ ])
632+
608633 self .metastore = create_metastore (options )
609634
610635 # a mapping from source files to their corresponding shadow files
@@ -2459,15 +2484,6 @@ def find_module_and_diagnose(manager: BuildManager,
24592484 if is_sub_path (result , dir ):
24602485 # Silence errors in site-package dirs and typeshed
24612486 follow_imports = 'silent'
2462- if (id in CORE_BUILTIN_MODULES
2463- and not is_typeshed_file (result )
2464- and not is_stub_package_file (result )
2465- and not options .use_builtins_fixtures
2466- and not options .custom_typeshed_dir ):
2467- raise CompileError ([
2468- f'mypy: "{ os .path .relpath (result )} " shadows library module "{ id } "' ,
2469- f'note: A user-defined top-level module with name "{ id } " is not supported'
2470- ])
24712487 return (result , follow_imports )
24722488 else :
24732489 # Could not find a module. Typically the reason is a
0 commit comments