@@ -535,14 +535,23 @@ struct ModuleNameIngredient<'db> {
535
535
pub ( super ) name : ModuleName ,
536
536
}
537
537
538
+ /// Returns `true` if the module name refers to a standard library module which can't be shadowed
539
+ /// by a first-party module.
540
+ ///
541
+ /// This includes "builtin" modules, which can never be shadowed at runtime either, as well as the
542
+ /// `types` module, which tends to be imported early in Python startup, so can't be consistently
543
+ /// shadowed, and is important to type checking.
544
+ fn is_non_shadowable ( minor_version : u8 , module_name : & str ) -> bool {
545
+ module_name == "types" || ruff_python_stdlib:: sys:: is_builtin_module ( minor_version, module_name)
546
+ }
547
+
538
548
/// Given a module name and a list of search paths in which to lookup modules,
539
549
/// attempt to resolve the module name
540
550
fn resolve_name ( db : & dyn Db , name : & ModuleName ) -> Option < ResolvedName > {
541
551
let program = Program :: get ( db) ;
542
552
let python_version = program. python_version ( db) ;
543
553
let resolver_state = ResolverContext :: new ( db, python_version) ;
544
- let is_builtin_module =
545
- ruff_python_stdlib:: sys:: is_builtin_module ( python_version. minor , name. as_str ( ) ) ;
554
+ let is_non_shadowable = is_non_shadowable ( python_version. minor , name. as_str ( ) ) ;
546
555
547
556
let name = RelaxedModuleName :: new ( name) ;
548
557
let stub_name = name. to_stub_package ( ) ;
@@ -553,7 +562,8 @@ fn resolve_name(db: &dyn Db, name: &ModuleName) -> Option<ResolvedName> {
553
562
// the module name always resolves to the stdlib module,
554
563
// even if there's a module of the same name in the first-party root
555
564
// (which would normally result in the stdlib module being overridden).
556
- if is_builtin_module && !search_path. is_standard_library ( ) {
565
+ // TODO: offer a diagnostic if there is a first-party module of the same name
566
+ if is_non_shadowable && !search_path. is_standard_library ( ) {
557
567
continue ;
558
568
}
559
569
0 commit comments