Skip to content

Conversation

@Gankra
Copy link
Contributor

@Gankra Gankra commented Nov 21, 2025

It turns out all of our import handling completely ignored the level (number of relative .'s) in a from ..x.y import z statement. It was nice seeing how much my understanding of ty has improved -- previously this would have all been opaque to me but now it was just, completely glaring and blatant.

Fixing this required refactoring all the import code to take the importing file into consideration. I ended up refactoring a bunch of code to pass around/require SemanticModel more, as it's the natural API for resolving this kind of import (it actually had an API for this that was just... dead code, whoops!).

@Gankra Gankra added the server Related to the LSP server label Nov 21, 2025
@Gankra Gankra added the ty Multi-file analysis & type inference label Nov 21, 2025
@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 21, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 21, 2025

mypy_primer results

No ecosystem changes detected ✅

No memory usage changes detected ✅

fn definitions_for_expression<'db>(
db: &'db dyn crate::Db,
file: ruff_db::files::File,
model: &SemanticModel<'db>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort of funny that SemanticModel survived to this day. I introduced it in one of the very earliest prototypes of a type aware linter back when ty was barely able to infer ints. I'm not sure it's the right API but good to see that it's not complete rubbish and is useful in cases like this :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah between this PR and the string annotations one I am Fully embracing it as an actually useful abstraction!

let covering_node = covering_node(parsed.syntax().into(), token.range())
.find_first(|node| node.is_identifier() || node.is_expression())
.find_first(|node| {
node.is_identifier() || node.is_expression() || node.is_stmt_import_from()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it makes sense to change this to node.is_stmt. I don't think we ever want to go beyond the closest statement (my, now proven incorrect, assumption was that expression is a sufficient boundary but turns out it isn't)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I'll think about it

@AlexWaygood AlexWaygood removed their request for review November 22, 2025 15:09
@Gankra Gankra merged commit 859f9ec into main Nov 22, 2025
41 checks passed
@Gankra Gankra deleted the gankra/modty branch November 22, 2025 16:06
dcreager added a commit that referenced this pull request Nov 24, 2025
* origin/main: (27 commits)
  [ty] Add hint about resolved Python version when a user attempts to import a member added on a newer version (#21615)
  Use release commit for actions/checkout (#21610)
  [ty] Add failing mdtest for known `Protocol` panic (#21594)
  [`parser`] Fix panic when parsing IPython escape command expressions (#21480)
  Fix cargo shear in CI (#21609)
  Update actions/checkout digest to c2d88d3 (#21601)
  Update dependency ruff to v0.14.6 (#21603)
  Update astral-sh/setup-uv action to v7.1.4 (#21602)
  Update Rust crate clap to v4.5.53 (#21604)
  Update taiki-e/install-action action to v2.62.56 (#21608)
  Update Rust crate hashbrown to v0.16.1 (#21605)
  Update Rust crate indexmap to v2.12.1 (#21606)
  Update Rust crate syn to v2.0.111 (#21607)
  [ty] Check method definitions on subclasses for Liskov violations (#21436)
  [ty] Fix panic for unclosed string literal in type annotation position (#21592)
  [ty] Fix rendering of unused suppression diagnostic (#21580)
  [ty] Improve lsp handling of hover/goto on imports (#21572)
  [ty] Improve diagnostics when a submodule is not available as an attribute on a module-literal type (#21561)
  [ty] Improve concise diagnostics for invalid exceptions when a user catches a tuple of objects (#21578)
  [ty] upgrade salsa (#21575)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

server Related to the LSP server ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Modules in imports don't have a "type"

3 participants