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

Code before:
            return;
        };

        // Now construct the submodule `.x`
        assert!(
            !submodule.is_empty(),
            "ImportFromSubmoduleDefinitionKind constructed with empty module"
        );

        let Ok(submodule_name) = ModuleName::from_identifier_parts(
            self.db(),
            self.file(),
            import_from.module.as_deref(),
            import_from.level,
        ) else {
            self.add_binding(import_from.into(), definition, |_, _| Type::unknown());
            return;
        };
        let Some(relative_submodule_name) = submodule_name.relative_to(&thispackage_name) else {
            self.add_binding(import_from.into(), definition, |_, _| Type::unknown());
            return;
        };
        let Some(name) = relative_submodule_name.components().next() else {
            self.add_binding(import_from.into(), definition, |_, _| Type::unknown());
            return;
        };
        let full_submodule_name = ModuleName::new(name).map(|final_part| {
            let mut ret = thispackage_name.clone();
            ret.extend(&final_part);
            ret
        });
        // And try to import it
        if let Some(submodule_type) = full_submodule_name
            .as_ref()
            .and_then(|submodule_name| self.module_type_from_name(submodule_name))
        {
            // Success, introduce a binding!
            //
            // We explicitly don't introduce a *declaration* because it's actual ok
            // (and fairly common) to overwrite this import with a function or class
            // and we don't want it to be a type error to do so.
            self.add_binding(import_from.into(), definition, |_, _| submodule_type);
            return;
        }

        // That didn't work, try to produce diagnostics
        self.add_binding(import_from.into(), definition, |_, _| Type::unknown());

        if !self.is_reachable(import_from) {
            return;
        }

Review comment (suggestion):
Nit. To reduce the duplication between the `else` branches
```suggestion
        let Ok(name) = ModuleName::from_identifier_parts(
            self.db(),
            self.file(),
            import_from.module.as_deref(),
            import_from.level,
        ).and_then(|submodule_name| submodule_name.relative_to(&thispackage_name))
         .and_then(|relative_submodule_name| relative_submodule_name.components().next()) else {
            self.add_binding(import_from.into(), definition, |_, _| Type::unknown());
            return;
        };
```

Code after:
        };

        // We have `from whatever.thispackage.x.y ...` or `from .x.y ...`
        // and we want to extract `x` (to ultimately construct `whatever.thispackage.x`):

        // First we normalize to `whatever.thispackage.x.y`
        let Some(final_part) = ModuleName::from_identifier_parts(
            self.db(),
            self.file(),
            import_from.module.as_deref(),
            import_from.level,
        )
        .ok()
        // `whatever.thispackage.x.y` => `x.y`
        .and_then(|submodule_name| submodule_name.relative_to(&thispackage_name))
        // `x.y` => `x`
        .and_then(|relative_submodule_name| {
            relative_submodule_name
                .components()
                .next()
                .and_then(ModuleName::new)
        }) else {
            self.add_binding(import_from.into(), definition, |_, _| Type::unknown());
            return;
        };

        // `x` => `whatever.thispackage.x`
        let mut full_submodule_name = thispackage_name.clone();
        full_submodule_name.extend(&final_part);

        // Try to actually resolve the import `whatever.thispackage.x`
        if let Some(submodule_type) = self.module_type_from_name(&full_submodule_name) {
            // Success, introduce a binding!
            //
            // We explicitly don't introduce a *declaration* because it's actual ok
            // (and fairly common) to overwrite this import with a function or class
            // and we don't want it to be a type error to do so.
            self.add_binding(import_from.into(), definition, |_, _| submodule_type);
            return;
        }

        // That didn't work, try to produce diagnostics
        self.add_binding(import_from.into(), definition, |_, _| Type::unknown());

        if !self.is_reachable(import_from) {
            return;
        }

        let Some(builder) = self
            .context
            .report_lint(&UNRESOLVED_IMPORT, AnyNodeRef::StmtImportFrom(import_from))