Repository: astral-sh/ruff
File: crates/ty_python_semantic/src/semantic_index/builder.rs (Rust)

Code before:
                //
                // * `from .x.y import z` (or `from whatever.thispackage.x.y`)
                // * And we are in an `__init__.py(i)` (hereafter `thispackage`)
                // * And this is the first time we've seen `from .x` in this module
                // * And we're in the global scope
                //
                // We introduce a local definition `x = <module 'thispackage.x'>` that occurs
                // before the `z = ...` declaration the import introduces. This models the fact
                // that the *first* time that you import 'thispackage.x' the python runtime creates
                // `x` as a variable in the global scope of `thispackage`.
                //
                // This is not a perfect simulation of actual runtime behaviour for *various*
                // reasons but it works well for most practical purposes. In particular it's nice
                // that `x` can be freely overwritten, and that we don't assume that an import
                // in one function is visible in another function.
                if let Some(submodule) = &node.module
                    && self.current_scope() == FileScopeId::global()
                    && self.file.is_package(self.db)
                    && let Ok(module_name) = ModuleName::from_identifier_parts(
                        self.db,
                        self.file,
                        node.module.as_deref(),
                        node.level,
                    )
                    && let Ok(thispackage) =
                        ModuleName::from_identifier_parts(self.db, self.file, None, 1)
                    && let Some(relative_submodule) = module_name.relative_to(&thispackage)
                    && let Some(direct_submodule) = relative_submodule.components().next()
                    && !self.seen_submodule_imports.contains(direct_submodule)
                {
                    self.seen_submodule_imports
                        .insert(direct_submodule.to_owned());

                    let direct_submodule_name = Name::new(direct_submodule);
                    let symbol = self.add_symbol(direct_submodule_name);
                    self.add_definition(
                        symbol.into(),
                        ImportFromSubmoduleDefinitionNodeRef { node, submodule },
                    );
                }

                let mut found_star = false;
                for (alias_index, alias) in node.names.iter().enumerate() {
                    if &alias.name == "*" {
                        // The following line maintains the invariant that every AST node that
                        // implements `Into<DefinitionNodeKey>` must have an entry in the
                        // `definitions_by_node` map. Maintaining this invariant ensures that
                        // `SemanticIndex::definitions` can always look up the definitions for a
                        // given AST node without panicking.
                        //
                        // The reason why maintaining this invariant requires special handling here

Review comment (bug):
I wonder if you see the new failing imports because this operation can fail if the path to the next search root contains any non-valid-identifier parts (e.g. looking up `core-db/root` will fail if the only search path is at `/`)

Code after:
                //
                // * `from .x.y import z` (or `from whatever.thispackage.x.y`)
                // * And we are in an `__init__.py(i)` (hereafter `thispackage`)
                // * And this is the first time we've seen `from .x` in this module
                // * And we're in the global scope
                //
                // We introduce a local definition `x = <module 'thispackage.x'>` that occurs
                // before the `z = ...` declaration the import introduces. This models the fact
                // that the *first* time that you import 'thispackage.x' the python runtime creates
                // `x` as a variable in the global scope of `thispackage`.
                //
                // This is not a perfect simulation of actual runtime behaviour for *various*
                // reasons but it works well for most practical purposes. In particular it's nice
                // that `x` can be freely overwritten, and that we don't assume that an import
                // in one function is visible in another function.
                if node.module.is_some()
                    && self.current_scope().is_global()
                    && self.file.is_package(self.db)
                    && let Ok(module_name) = ModuleName::from_identifier_parts(
                        self.db,
                        self.file,
                        node.module.as_deref(),
                        node.level,
                    )
                    && let Ok(thispackage) = ModuleName::package_for_file(self.db, self.file)
                    && let Some(relative_submodule) = module_name.relative_to(&thispackage)
                    && let Some(direct_submodule) = relative_submodule.components().next()
                    && !self.seen_submodule_imports.contains(direct_submodule)
                {
                    self.seen_submodule_imports
                        .insert(direct_submodule.to_owned());

                    let direct_submodule_name = Name::new(direct_submodule);
                    let symbol = self.add_symbol(direct_submodule_name);
                    self.add_definition(
                        symbol.into(),
                        ImportFromSubmoduleDefinitionNodeRef { node },
                    );
                }

                let mut found_star = false;
                for (alias_index, alias) in node.names.iter().enumerate() {
                    if &alias.name == "*" {
                        // The following line maintains the invariant that every AST node that
                        // implements `Into<DefinitionNodeKey>` must have an entry in the
                        // `definitions_by_node` map. Maintaining this invariant ensures that
                        // `SemanticIndex::definitions` can always look up the definitions for a
                        // given AST node without panicking.
                        //
                        // The reason why maintaining this invariant requires special handling here
                        // is that some `Alias` nodes may be associated with 0 definitions: