[ty] Add completions for from module import <CURSOR> by BurntSushi · Pull Request #18830 · astral-sh/ruff (original) (raw)

@ntBre ntBre added the ty

Multi-file analysis & type inference

label

Jun 20, 2025

AlexWaygood

sharkdp

sharkdp

sharkdp

MichaReiser

MichaReiser

@BurntSushi

There were two main challenges in this PR.

The first was mostly just figuring out how to get the symbols corresponding to module. It turns out that we do this in a couple of places in ty already, but through different means. In one approach, we use exported_names. In another approach, we get a Type corresponding to the module. We take the latter approach here, which is consistent with how we do completions elsewhere. (I looked into factoring this logic out into its own function, but it ended up being pretty constrained. e.g., There's only one other place where we want to go from ast::StmtImportFrom to a module Type, and that code also wants the module name.)

The second challenge was recognizing the from module import <CURSOR> pattern in the code. I initially started with some fixed token patterns to get a proof of concept working. But I ended up switching to mini state machine over tokens. I looked at the parser for StmtImportFrom to determine what kinds of tokens we can expect.

dhruvmanila

AlexWaygood

BurntSushi added a commit that referenced this pull request

Jun 23, 2025

@BurntSushi

…tions

This also adds a new ModuleName::relative_to public API to help with this.

Kudos to @AlexWaygood for the meat of this patch!

Ref #18830 (comment)

BurntSushi added a commit that referenced this pull request

Jun 23, 2025

@BurntSushi

…tions

This also adds a new ModuleName::relative_to public API to help with this.

Kudos to @AlexWaygood for the meat of this patch!

Ref #18830 (comment)

BurntSushi added a commit that referenced this pull request

Jun 23, 2025

@BurntSushi

…tions

This also adds a new ModuleName::relative_to public API to help with this.

Kudos to @AlexWaygood for the meat of this patch!

Ref #18830 (comment)

BurntSushi added a commit that referenced this pull request

Jun 23, 2025

@BurntSushi

BurntSushi added a commit that referenced this pull request

Jun 24, 2025

@BurntSushi

This tests things like from ...foo import <CURSOR>.

I had previously tested this on an ad hoc basis inside of my editor, so the token state machine already recognizes this pattern.

Ref #18830 (comment)

BurntSushi added a commit that referenced this pull request

Jun 24, 2025

@BurntSushi

This tests things like from ...foo import <CURSOR>.

I had previously tested this on an ad hoc basis inside of my editor, so the token state machine already recognizes this pattern.

Ref #18830 (comment)

BurntSushi added a commit that referenced this pull request

Jun 24, 2025

@BurntSushi

This tests things like from ...foo import <CURSOR>.

I had previously tested this on an ad hoc basis inside of my editor, so the token state machine already recognizes this pattern.

Ref #18830 (comment)

KotlinIsland pushed a commit to KotlinIsland/basedpython that referenced this pull request

May 1, 2026

@BurntSushi

KotlinIsland pushed a commit to KotlinIsland/basedpython that referenced this pull request

May 1, 2026

@BurntSushi

This tests things like from ...foo import <CURSOR>.

I had previously tested this on an ad hoc basis inside of my editor, so the token state machine already recognizes this pattern.

Ref astral-sh/ruff#18830 (comment)

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})