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§
- Import
Group - 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.
- Import
Layout - Complete import layout for the top-level import section of a Python file.
- Imported
Name - One name (or alias) inside a
from X import …statement. - Parsed
Bare Import - A parsed
import Xorimport X as Ystatement. - Parsed
From Import - A parsed
from X import a, b(or multiline variant) statement.
Enums§
- Import
Kind - Which isort / import-sort group an import belongs to.
- Parse
Source - How the
ImportLayoutwas 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, orImportKind::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 aliassuffix. - parse_
import_ layout - Parse the import layout of a Python source file.