Skip to main content

Module import_analysis

Module import_analysis 

Source
Expand description

Import layout analysis for Python files.

This module is the single source of truth for import-related operations used across LSP providers (code_action, inlay_hint, and any future consumers). It has no dependency on LSP types such as TextEdit — those belong in the provider layer.

§Design

parse_import_layout is the main entry point. It tries to produce an ImportLayout via the rustpython-parser AST, which correctly handles multiline parenthesised imports, inline comments, and every other edge case that confounds a naive line-scanner. If the file has syntax errors (common during active editing) it falls back to a simpler line-scan that mirrors the behaviour of the original string-based parse_import_groups function.

adapt_type_for_consumer rewrites a fixture’s return-type annotation string to match the consumer file’s existing import style (dotted ↔ short). It is used by both code_action (which also inserts the necessary import statements) and inlay_hint (which only needs the display string).

Structs§

ImportGroup
A contiguous block of module-level import lines (separated from other blocks by blank lines or comment lines), with a classification for the whole group.
ImportLayout
Complete import layout for the top-level import section of a Python file.
ImportedName
One name (or alias) inside a from X import … statement.
ParsedBareImport
A parsed import X or import X as Y statement.
ParsedFromImport
A parsed from X import a, b (or multiline variant) statement.

Enums§

ImportKind
Which isort / import-sort group an import belongs to.
ParseSource
How the ImportLayout was derived — used for testing and diagnostics.

Functions§

adapt_type_for_consumer
Adapt a fixture’s return-type annotation and import specs to the consumer file’s existing import context.
classify_import_statement
Classify an import statement string as ImportKind::Future, ImportKind::Stdlib, or ImportKind::ThirdParty.
find_sorted_insert_position
Find the correct sorted insertion line within an existing import group, so that the result stays isort-sorted (bare before from, alphabetical by module within each sub-category).
import_line_sort_key
Sort key for a full import line, following isort / ruff conventions:
import_sort_key
Sort key for an imported name, stripping any as alias suffix.
parse_import_layout
Parse the import layout of a Python source file.