|
1 | 1 | use ruff_db::files::File; |
2 | 2 | use ruff_python_ast::PythonVersion; |
3 | | -use ty_module_resolver::{KnownModule, file_to_module, resolve_module_confident}; |
| 3 | +use ty_module_resolver::{ |
| 4 | + KnownModule, Module, ModuleName, file_to_module, resolve_module_confident, |
| 5 | +}; |
4 | 6 |
|
5 | 7 | use crate::dunder_all::dunder_all_names; |
6 | 8 | use crate::semantic_index::definition::{Definition, DefinitionState}; |
@@ -380,25 +382,29 @@ pub(crate) fn imported_symbol<'db>( |
380 | 382 | /// and should not be used when a symbol is being explicitly imported from the `builtins` module |
381 | 383 | /// (e.g. `from builtins import int`). |
382 | 384 | pub(crate) fn builtins_symbol<'db>(db: &'db dyn Db, symbol: &str) -> PlaceAndQualifiers<'db> { |
383 | | - resolve_module_confident(db, &KnownModule::Builtins.name()) |
384 | | - .and_then(|module| { |
385 | | - let file = module.file(db)?; |
386 | | - Some( |
387 | | - symbol_impl( |
388 | | - db, |
389 | | - global_scope(db, file), |
390 | | - symbol, |
391 | | - RequiresExplicitReExport::Yes, |
392 | | - ConsideredDefinitions::EndOfScope, |
393 | | - ) |
394 | | - .or_fall_back_to(db, || { |
395 | | - // We're looking up in the builtins namespace and not the module, so we should |
396 | | - // do the normal lookup in `types.ModuleType` and not the special one as in |
397 | | - // `imported_symbol`. |
398 | | - module_type_implicit_global_symbol(db, symbol) |
399 | | - }), |
400 | | - ) |
401 | | - }) |
| 385 | + let resolver = |module: Module<'_>| { |
| 386 | + let file = module.file(db)?; |
| 387 | + let found_symbol = symbol_impl( |
| 388 | + db, |
| 389 | + global_scope(db, file), |
| 390 | + symbol, |
| 391 | + RequiresExplicitReExport::Yes, |
| 392 | + ConsideredDefinitions::EndOfScope, |
| 393 | + ) |
| 394 | + .or_fall_back_to(db, || { |
| 395 | + // We're looking up in the builtins namespace and not the module, so we should |
| 396 | + // do the normal lookup in `types.ModuleType` and not the special one as in |
| 397 | + // `imported_symbol`. |
| 398 | + module_type_implicit_global_symbol(db, symbol) |
| 399 | + }); |
| 400 | + // If this symbol is not present in project-level builtins, search in the default ones. |
| 401 | + found_symbol |
| 402 | + .ignore_possibly_undefined() |
| 403 | + .map(|_| found_symbol) |
| 404 | + }; |
| 405 | + resolve_module_confident(db, &ModuleName::new_static("__builtins__").unwrap()) |
| 406 | + .and_then(&resolver) |
| 407 | + .or_else(|| resolve_module_confident(db, &KnownModule::Builtins.name()).and_then(resolver)) |
402 | 408 | .unwrap_or_default() |
403 | 409 | } |
404 | 410 |
|
|
0 commit comments