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

Code before:
                        },
                    );
                }
            }
            ast::Stmt::ImportFrom(node) => {
                self.current_use_def_map_mut()
                    .record_node_reachability(NodeKey::from_node(node));

                // If we see:
                //
                // * `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;

Review comment (suggestion):
Nit:

```suggestion
                    && self.current_scope().is_global()
```

Code after:
                        },
                    );
                }
            }
            ast::Stmt::ImportFrom(node) => {
                self.current_use_def_map_mut()
                    .record_node_reachability(NodeKey::from_node(node));

                // If we see:
                //
                // * `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() {