Repository: astral-sh/ruff
File: crates/ty_ide/src/importer.rs (Rust)

Code before:

impl<'ast> AstImportKind<'ast> {
    /// Returns whether this import satisfies the given request.
    ///
    /// If it does, then this returns *how* the import satisfies
    /// the request.
    fn satisfies<'importer>(
        &'importer self,
        db: &'_ dyn Db,
        importing_file: File,
        request: &ImportRequest<'_>,
    ) -> Option<ImportResponseKind<'ast>> {
        match *self {
            AstImportKind::Import(ast) => {
                if request.force_style && !matches!(request.style, ImportStyle::Import) {
                    return None;
                }
                let alias = ast
                    .names
                    .iter()
                    .find(|alias| alias.name.as_str() == request.module)?;
                Some(ImportResponseKind::Qualified { ast, alias })
            }
            AstImportKind::ImportFrom(ast) => {
                // If the request is for a module itself,
                // then it can never be satisfies by a `from ... import ...`
                // statement.
                let member = request.member?;

                if request.force_style && !matches!(request.style, ImportStyle::ImportFrom) {
                    return None;
                }

                let module = ModuleName::from_import_statement(db, importing_file, ast).ok()?;
                if module.as_str() != request.module {
                    return None;
                }
                let kind = ast
                    .names
                    .iter()
                    .find(|alias| alias.name.as_str() == "*" || alias.name.as_str() == member)
                    .map(|alias| ImportResponseKind::Unqualified { ast, alias })
                    .unwrap_or_else(|| ImportResponseKind::Partial(ast));
                Some(kind)
            }
        }
    }
}

/// A request to import a module into the global scope of a Python module.
#[derive(Debug)]

Review comment (suggestion):
```suggestion
                // then it can never be satisfied by a `from ... import ...`
```

Also: this statement isn't true in several senses, most notably `from collections import abc`. I assume you mean something more nuanced.

Code after:

impl<'ast> AstImportKind<'ast> {
    /// Returns whether this import satisfies the given request.
    ///
    /// If it does, then this returns *how* the import satisfies
    /// the request.
    fn satisfies<'importer>(
        &'importer self,
        db: &'_ dyn Db,
        importing_file: File,
        request: &ImportRequest<'_>,
    ) -> Option<ImportResponseKind<'ast>> {
        match *self {
            AstImportKind::Import(ast) => {
                if request.force_style && !matches!(request.style, ImportStyle::Import) {
                    return None;
                }
                let alias = ast
                    .names
                    .iter()
                    .find(|alias| alias.name.as_str() == request.module)?;
                Some(ImportResponseKind::Qualified { ast, alias })
            }
            AstImportKind::ImportFrom(ast) => {
                // If the request is for a module itself, then we
                // assume that it can never be satisfies by a
                // `from ... import ...` statement. For example, a
                // `request for collections.abc` needs an
                // `import collections.abc`. Now, there could be a
                // `from collections import abc`, and we could
                // plausibly consider that a match and return a
                // symbol text of `abc`. But it's not clear if that's
                // the right choice or not.
                let member = request.member?;

                if request.force_style && !matches!(request.style, ImportStyle::ImportFrom) {
                    return None;
                }

                let module = ModuleName::from_import_statement(db, importing_file, ast).ok()?;
                if module.as_str() != request.module {
                    return None;
                }
                let kind = ast
                    .names
                    .iter()
                    .find(|alias| alias.name.as_str() == "*" || alias.name.as_str() == member)
                    .map(|alias| ImportResponseKind::Unqualified { ast, alias })
                    .unwrap_or_else(|| ImportResponseKind::Partial(ast));
                Some(kind)
            }